Use workspace variable in equation solver

1 回表示 (過去 30 日間)
Ahmed Abdulla
Ahmed Abdulla 2022 年 8 月 9 日
回答済み: Walter Roberson 2022 年 8 月 9 日
I have the equation x+y=25 and currently I have y as a predfined variable=5. so the equation becomes x+5=25. However I would like to change y from my script and the equation to automatically change based on my y value rather than me modifying my equations each time to edit the y value.
I currently do it this way:
y=5;
syms x
eqn = x+5=25;
S = solve(eqn,x);

回答 (2 件)

Rik
Rik 2022 年 8 月 9 日
If you don't want to use a function to wrap the input, you can use an anonymous function instead.
y=5;
syms x
eqn =@(y) x+y==25;
S = solve(eqn(y),x)
S = 
20

Walter Roberson
Walter Roberson 2022 年 8 月 9 日
syms x y
eqn = x + y == 25
eqn = 
y = 5
y = 5
subs(eqn)
ans = 
y = 9
y = 9
subs(eqn)
ans = 

カテゴリ

Help Center および File ExchangeSymbolic Math Toolbox についてさらに検索

製品


リリース

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by