making a substitution

8 views (last 30 days)
yasser
yasser on 20 Dec 2011
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

Accepted Answer

Walter Roberson
Walter Roberson on 20 Dec 2011
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 .
  3 Comments
Walter Roberson
Walter Roberson on 20 Dec 2011
If the x is already known to be isolated, then
simplify( subs( sym('y-2'), 'y', TheYValue )
should give the reduced value with the expression evaluated after the substitution.
Either way, with solve() or simplify(), once you are down to a single symbolic expression involving no unknowns, you can use double() to convert the symbolic expression to MATLAB double precision.
Image Analyst
Image Analyst on 21 Dec 2011
Yes, Walter is a dear. We see "my dear" so often that I'm convinced that it must be something taught (incorrectly) in English language classes - that English-speaking people like to be called "my dear". Sometimes the 70 year old lady serving food at the cafeteria might say that, but not another engineer who'd probably more likely say "You're awesome Walter!".

Sign in to comment.

More Answers (1)

Image Analyst
Image Analyst on 20 Dec 2011
x=y-2
There, that was simple enough.
  1 Comment
yasser
yasser on 20 Dec 2011
first thx for ur concern,
and sorry fo not explaining it, but the value of 'y' i got from solving another multiple equations,so to use ur method i have to write the equation(x=y-2)in matlab , because x is defined by sym.
thx again.

Sign in to comment.

Tags

Community Treasure Hunt

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

Start Hunting!