making a substitution
8 views (last 30 days)
Show older comments
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
0 Comments
Accepted Answer
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
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
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!".
More Answers (1)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!