making a substitution

11 ビュー (過去 30 日間)
yasser
yasser 2011 年 12 月 20 日
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

採用された回答

Walter Roberson
Walter Roberson 2011 年 12 月 20 日
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 件のコメント
Walter Roberson
Walter Roberson 2011 年 12 月 20 日
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 2011 年 12 月 21 日
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!".

サインインしてコメントする。

その他の回答 (1 件)

Image Analyst
Image Analyst 2011 年 12 月 20 日
x=y-2
There, that was simple enough.
  1 件のコメント
yasser
yasser 2011 年 12 月 20 日
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.

サインインしてコメントする。

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by