Why do two equivalent functions not produce same output

3 views (last 30 days)
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 Comments
Walter Roberson
Walter Roberson on 10 Dec 2024
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 on 11 Dec 2024
Ah! Thank you so much for the correction. As you can tell I'm still learning haha

Sign in to comment.

Accepted Answer

Epsilon
Epsilon on 10 Dec 2024
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 Comment
Ryder
Ryder on 10 Dec 2024
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!

Sign in to comment.

More Answers (0)

Products


Release

R2024b

Community Treasure Hunt

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

Start Hunting!