Why do two equivalent functions not produce same output

4 ビュー (過去 30 日間)
Ryder
Ryder 2024 年 12 月 10 日
コメント済み: Ryder 2024 年 12 月 11 日
I've been working on a constrained optimization problem which is attempting to optimize quality loss for an input file size. While creating some functions, I realized that I can apply some mathetical manipulation to simplify my functions to be more manageable (especially once I start using real image data).
Here is a simplified version of my original code, with function 'f' being unecessarily complicated.
syms k t l
I = [1, 2, 3];
Q = [3,2,1];
b = 5;
n = length(I);
f = sum((I-((I) ./ (k * Q))).^2); %using the symbolic k within a sum creates a messy function
f = f/n;
g = (3.295)*n*(2.71^(-1*t*b))-20;
F = f - l*g;
f0 = diff(F, k);
f1 = diff(F, t);
f2 = diff(F, l);
sols = solve([f0;f1;f2], [k,t,l])
Which is able to generate solutions for smaller lengths of I and Q.
So instead, I tried defining the function as:
f = (sum(I.^2) -2/k*sum((I.^2)./Q) + sum((I./Q).^2)/k)/n;
But in this case I receive an empty solution set:
sols =
struct with fields:
k: [0x1 sym]
t: [0x1 sym]
l: [0x1 sym]
What's frustrating is that when I sub in various values of k into the functions, they yield the same result, meaning they should be equivalent:
f = sum((I-((I) ./ (k * Q))).^2);
f = f/n;
eval(subs(f, k, 1)) = 0.4815
And
f = (sum(I.^2) -2/k*sum((I.^2)./Q) + sum((I./Q).^2)/k)/n;
eval(subs(f, k, 1)) = 0.4815
Although I realize that subbing in numbers isn't the best way to prove that the functions are equivalent, I derived the second function by mathematically expanding the binomial inside the sum, so it should be mathematically sound.
I really need something similar to the second function to work as, when dealing with images, the function becomes impossible to process.
I apologize for the poorly written code as I am rather new to MatLab, but I need to figure this out and am on a time crunch so I'm quite desperate; ANY help would be greatly appreciated.
THANK YOU!
  2 件のコメント
Walter Roberson
Walter Roberson 2024 年 12 月 10 日
eval(subs(f, k, 1))
Mathworks does not document any definition of the result of eval() of a symbolic expression.
In practice, it is equivalent to
eval(char(subs(f, k, 1)))
However, char() of a symbolic expression gives a result that is not exactly MATLAB expression and is not internal MuPAD code. char() of symbolic expressions generates something that is intended to be human readable instead of being mechanically processed. char() of a symbolic expression produces some expressions that have parameters in a different order than the corresponding MATLAB code, and char() of a symbolic expression produces expressions that do not have valid syntax for MATLAB or for MuPAD.
Therefore you should never eval() a symbolic expression. You should subs() as necessary, and then finally double() the result.
Ryder
Ryder 2024 年 12 月 11 日
Ah! Thank you so much for the correction. As you can tell I'm still learning haha

サインインしてコメントする。

採用された回答

Epsilon
Epsilon 2024 年 12 月 10 日
Hi Ryder,
It might seem like the expanded version of the equation is correct and produces the same value on substituting k with 1 in both the forms. When we replace k with 2 the values don’t match confirming that they are indeed not equivalent.
The correct replacement for the expanded form will be:
f = sum(I.^2) - (2/k) * sum((I.^2)./Q) + (1/k^2) * sum((I.^2)./Q.^2)/n;
syms k t l
I = [1, 2, 3];
Q = [3,2,1];
b = 5;
n = length(I);
%correct expanded form
f = sum(I.^2) - (2/k) * sum((I.^2)./Q) + (1/k^2) * sum((I.^2)./Q.^2)/n;
g = (3.295)*n*(2.71^(-1*t*b))-20;
F = f - l*g;
f0 = diff(F, k);
f1 = diff(F, t);
f2 = diff(F, l);
sols = solve([f0;f1;f2], [k,t,l])
sols = struct with fields:
k: 91/306 t: -log(4000/1977)/(5*log(271/100)) l: 0
Hope it helps!
  1 件のコメント
Ryder
Ryder 2024 年 12 月 10 日
Hi Epsilon,
I think you forgot to put the "/n" in full parentheses at the end. Other than that you solved my problem. I feel pretty silly for forgetting to square the k haha.
The corrected notation would be:
f = (sum(I.^2) -2/k*sum((I.^2)./Q) + sum((I./Q).^2)/k^2)/n;
Thank you!

サインインしてコメントする。

その他の回答 (0 件)

製品


リリース

R2024b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by