MATLAB: Making a substitution

solve

if i have this simple equation x=y-2 and I already know the value of y ,(y=scalar), how can i substitute in the above equation and get the value of x?? thx in advance

Best Answer

solve( subs( sym('x=y-2'), 'y', TheYValue), 'x')
Yes, this is a bit longer than what you might expect. If you were to use
subs( sym('x=y-2'), 'y', TheYValue )
then you would get something of the form
x = SomeValue
with SomeValue possibly being an expression that could be resolved further, as subs() does not evaluate the expression or try to simplify it after the substitution is done, and subs() will leave the expression in the form of an equation rather than trying to isolate x. For example if the expression was 5*x^2 = y - 2 then subs() would just leave it as 5*x^2 = SomeValue .