SYMS expression not being replaced with values
Show older comments
Dear MATLAB team.
I am working with syms and apparently some substitutions are not working properly. According to code below, I am comparing the ratio p_11/p_22 against 1. In that way, result1 should be equal to result2 and result1 - result2 = 0, but command line says a different thing (please check output below code).
Could you please let me know what am I missing?
syms c_11 c_22 eta lambda epsilon x_hat gamma L11 L_hat_11
%Define Lii and Lij terms
L11 = (1 - lambda)*((x_hat)^2) + lambda*(x_hat - (epsilon/4));
L12 = x_hat - L11;
L22 = (1 - lambda)*((1 - x_hat)^2) + (lambda)*((1 - x_hat) - (epsilon/4) );
L21 = (1 - x_hat) - L22;
%Define L_hat_ii and L_hat_ij terms
L_hat_22 = 1 - 2*x_hat + lambda*(x_hat - 1/2) + L_hat_11;
L_hat_21 = (1 - lambda)*x_hat + lambda/2 - L_hat_11;
L_hat_11 = (1 - lambda)*(x_hat^2) + ( (1/2)*lambda*x_hat );
L_hat_12 = x_hat - L_hat_11;
%Define p_11 and p_22:
p_11 = (eta*c_11*L11)/(L_hat_11 - L11*(1 - eta) + eta*gamma*L_hat_11);
p_22 = (eta*c_11*L22)/(L_hat_22 - L22*(1 - eta) + eta*gamma*L_hat_22);
%Obtain numerator and denominator of p_11/p_22 using numden():
[numerator , denominator] = numden(p_11/p_22);
result1 = simplify(expand((subs(numerator - denominator,gamma,0))));
%Obtain numerator and denominator of p_11/p_22 by hand:
numerator = L11*((eta - 1)*L22 + (1 + gamma*eta)*L_hat_22);
denominator = L22*((eta - 1)*L11 + (1 + gamma*eta)*L_hat_11);
result2 = simplify(expand((subs(numerator - denominator,gamma,0))));
%command line output:
>> expand(result1 - result2)
ans =
15*lambda^2*x_hat^2 + (15*lambda^2*x_hat^3)/2 - 15*lambda^2*x_hat^4 - (15*epsilon*lambda)/4 + (15*lambda*x_hat)/2 + 15*L_hat_11*x_hat^2 + (15*epsilon*lambda^2)/8 - (45*lambda*x_hat^2)/2 - (15*lambda^2*x_hat)/2 - (15*lambda*x_hat^3)/2 + 30*lambda*x_hat^4 - 15*x_hat^4 + (15*epsilon*lambda*x_hat^2)/4 - (15*epsilon*lambda^2*x_hat)/8 - (15*epsilon*lambda^2*x_hat^2)/4 - (15*L_hat_11*epsilon*lambda)/4 + 15*L_hat_11*lambda*x_hat + (15*epsilon*lambda*x_hat)/2 - 15*L_hat_11*lambda*x_hat^2
Answers (1)
Steven Lord
on 22 Mar 2019
0 votes
Do any of your expressions involve division by 0 if you substitute lambda = 0? Running your code in the Live Editor, I don't believe they do.
Are result1 and result2 the same if lambda is equal to 0? They don't look like it to me.
1 Comment
Renzo Clavijo
on 25 Mar 2019
Categories
Find more on Programming in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!