Substituting a variable in an equation

Hi,
I have a set of equations in which certain variable and values are to be substituted. But when I use the subs function, it doesn't seem to work.
syms r Y L0 L1 L2 p1 p2 x y
eqn1 = L0+L1*cos(x)+p2*cos(x+y) == r*sin(x+y);
eqn2 = L1*sin(x)+p2*sin(x+y) == Y-r*cos(x+y);
Now, x and y are given by another set of equations
x = acos((r^2-p1^2)/(r^2+p1^2));
y = acos((r^2-p2^2)/(r^2+p2^2));
I also have other set of equations for my dependent variables.
Y = r;
p1 = L0;
p2 = L1 - L0;
I need to substitute these values back into my equations and so I use subs function and it works as expected.
eqn3 = subs(eqn1);
eqn4 = subs(eqn2);
But the problem is, I need to simplify my main equations with x+y = pi/2. This is just one case and the value will change for other cases, so I can't simplify my main equations directly. I use subs function once again for this substitution process.
eqn1=subs(eqn1,x+y,pi/2);
eqn2=subs(eqn2,x+y,pi/2);
Since, the subs function replaces x and y with their corresponding equations, it is pointless to use it here, so I am using the last set of subs function before my x and y substitution and my final code looks like as below.
syms r Y L0 L1 L2 p1 p2 x y
eqn1 = L0+L1*cos(x)+p2*cos(x+y) == r*sin(x+y);
eqn2 = L1*sin(x)+p2*sin(x+y) == Y-r*cos(x+y);
x = acos((r^2-p1^2)/(r^2+p1^2));
y = acos((r^2-p2^2)/(r^2+p2^2));
Y = r;
p1 = L0;
p2 = L1 - L0;
eqn1=subs(eqn1,x+y,pi/2);
eqn2=subs(eqn2,x+y,pi/2);
eqn3 = subs(eqn1);
eqn4 = subs(eqn2);
But still, the x+y simplification doesn't seem to work and the entire equation is substituted with the values of x and y. How do I fix this?
Thanks,
Abinav

 Respuesta aceptada

Walter Roberson
Walter Roberson el 8 de Jul. de 2020
eqn1 = subs(eqn1, str2sym('x+y'), pi/2);
eqn2 = subs(eqn2, str2sym('x+y'), pi/2);
The reason for this is that at the point you have assigned x and y as variables, so x+y at that point is not the same as the x+y that was defined in eqn1 and eqn2.

2 comentarios

Abinav
Abinav el 8 de Jul. de 2020
Yes, if I substitute before the variable declaration it works.
But is there an other way to do this in this particular order? This is just part of the code and I have other blocks of code inbetween to find the values.
The code I posted is after the variable declaration, replacing you current
eqn1=subs(eqn1,x+y,pi/2);
eqn2=subs(eqn2,x+y,pi/2);
lines.
Another way of writing it would be
eqn1=subs(eqn1,sym('x')+sym('y'),pi/2);
eqn2=subs(eqn2,sym('x')+sym('y'),pi/2);
Remember that when you created
eqn1 = L0+L1*cos(x)+p2*cos(x+y) == r*sin(x+y);
that that captured the values of x and y as they were at that time, just like if you had numeric x and y
x = 3; y = 11;
eqn1 = L0+L1*cos(x)+p2*cos(x+y) == r*sin(x+y);
then you would not expect that changing the numeric x and y values would have any effect on eqn1.
The values of syms x y that were captured are references into the symbolic engine to symbolic variable x and y . When you then assign new values to x and y, the x and y at the MATLAB level no longer refer to the same location in the symbolic engine, but eqn1's references into the symbolic engine do not get updated. Not until you eqn1 = subs(eqn1) -- but you do not try to do that until after you try to subs(eqn1,x+y,pi/2) using the x and y that are no longer just the symbolic variable x and y and are instead long expressions. Those long expressions are not part of eqn1 which still has plain symbolic-x plus symbolic-y . Long-complicated x + long-complicated y has some meaning, but it does not happen to match any expression present when eqn1 was constructed.

Iniciar sesión para comentar.

Más respuestas (0)

Productos

Versión

R2020a

Preguntada:

el 8 de Jul. de 2020

Comentada:

el 8 de Jul. de 2020

Community Treasure Hunt

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

Start Hunting!

Translated by