Rearrange Variables in an equation

290 ビュー (過去 30 日間)
Mech Princess
Mech Princess 2012 年 7 月 20 日
コメント済み: Star Strider 2023 年 1 月 29 日
I declare the variables using sym or syms then i have an equation a+2*b=1; I want MATLAB to give me a=1-2*b; or alternatively b=(1-a)/2;
is there a way to do this?
Also, There maybe more than 2 variables Equation maybe linear or non-linear Equation can change at each iteration. Thanks

採用された回答

Star Strider
Star Strider 2012 年 7 月 20 日
編集済み: Star Strider 2012 年 7 月 20 日
This is one way:
syms a b
eqn = (a + 2*b == 1)
v_a = solve(eqn, a)
v_b = solve(eqn, b)
producing this output:
v_a =
1 - 2*b
v_b =
1/2 - a/2
I suggest you avoid expressions such as:
[a b] = solve(eqn, a, b)
even though it is valid syntax, because instead of solving the equation for ‘a’ and then solving it independently for ‘b’, it solves it for ‘a’ first and then solves the resulting equation for ‘b’ in terms of an introduced variable ‘z’. That produces a multi-line warning in red (that I will not reproduce here), and the results:
a =
1 - 2*z
b =
z
that does not make sense to me. I consider this to be a bug, and I would prefer it to be an option rather than the default, but that is not my decision.
  3 件のコメント
Mech Princess
Mech Princess 2012 年 7 月 20 日
Sorry, one more issue regarding this. My variables alpha1, alpha2 and alpha3 are defined by sym and not syms. I need to do this as the no of alpha's is determined by the input data size. This case 3.
data = [-1 0 1;-1 +1 -1]'; alpha = sym('alpha',[1 3]); %output "[ alpha1, alpha2, alpha3]" st = 'alpha*data(:,2)=0' %output "alpha2 - alpha1 - alpha3 = 0" but I dont get this. instead, its something else v_2 = solve(st,alpha(1)) %doesnt work because the previous line is wrong
Do you have any suggestions for this? Thanks
Star Strider
Star Strider 2012 年 7 月 20 日
This:
data = [-1 0 1;-1 +1 -1]';
alpha = sym('alpha',[1 3]);
advct = alpha * data(:,2)
v_2 = solve(advct == 0, alpha(1))
gives me these:
advct =
alpha2 - alpha1 - alpha3
v_2 =
alpha2 - alpha3
Is that the result you were hoping for?

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

その他の回答 (2 件)

Nitesh khanna
Nitesh khanna 2020 年 8 月 1 日
how to replace the variables a, b, c, d by variables w, x, y, z for; in eqn
eqn=a+b+c+d==9;
will this command work
eqn=subs(eqn, [a,b,c,d],[w,x,y,z])

Bill Tubbs
Bill Tubbs 2023 年 1 月 29 日
As explained here by Paul, you can use the isolate function for this:
>> syms a b
>> eqn1 = b == (1 - a) / 2;
>> isolate(eqn1, a)
ans =
a == 1 - 2*b
>> isolate(eqn1, b)
ans =
b == 0.5000 - 0.5000*a
  2 件のコメント
Walter Roberson
Walter Roberson 2023 年 1 月 29 日
.. Though mind you, that was not an option back in 2012 !
Star Strider
Star Strider 2023 年 1 月 29 日
Definitely!
The isolate function was introduced in R2017a, 5 years after this thread.
I’ve used isolate extensively in my Answers since it was introduced, when its use was appropriate.

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

カテゴリ

Help Center および File ExchangeEquation Solving についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by