Solving System of Equations Symbolically

I have a more complex system of equations than the one below, but this one has the same issue.
I want to solve for a variable in terms of other variables.
Why does this not work to solve for c?
clc
syms a b c d e
eqn1= a==b+d;
eqn2= a==e+c;
eqnarray=[eqn1 eqn2];
solve(eqnarray,c)
Clearly the answer is c=a-e -> c=b+d-e
I'm unsure as to why I'm having so much trouble with this, I've solved systems of equations before symbolically, though I've never either had free variables or overdetermined systems.
I probably could have solved my full system of equations by hand already, so I'm feeling a little slow today.

 採用された回答

Star Strider
Star Strider 2019 年 3 月 10 日

0 投票

Subtract them:
syms a b c d e
eqn1= a==b+d;
eqn2= a==e+c;
eqnarray=[eqn1 - eqn2];
cs = solve(eqnarray,c)
producing:
cs =
b + d - e

3 件のコメント

Michal Walko
Michal Walko 2019 年 3 月 10 日
編集済み: Michal Walko 2019 年 3 月 10 日
Okay, that may work for this case, but what if my system of equations was not defined purely in terms of a single variable on the LHS such as below?
syms a b c d e
eqn1= b==a-d;
eqn2= a==e+c;
eqnarray=[eqn1 eqn2];
cs = solve(eqnarray,c)
EDIT: the subtraction thing works in this case again- why is this? I've never seen anything of this format in the documentation
Thanks!
Star Strider
Star Strider 2019 年 3 月 10 日
編集済み: Star Strider 2019 年 3 月 10 日
My approach works in this situation as well:
syms a b c d e
eqn1= b==a-d;
eqn2= a==e+c;
eqnarray=[eqn1 - eqn2];
cs = solve(eqnarray,c)
producing:
cs =
2*a - b - d - e
EDIT —
Subtracting them works by creating one algebraic equation from the pair of algebraic equations. So long as they have at least one variable in common (as they do here), you can solve for the variable of interest.
Michal Walko
Michal Walko 2019 年 3 月 10 日
編集済み: Michal Walko 2019 年 3 月 10 日
Edited my post before I saw your reply.
Thank you for your help, in some situations MATLAB accepts the system of equations purely as an array of form
equations=[first second third fourth]
and gives me a solution if I want to solve for a variable inside.
Why does that not work in this case and do you know if this subtraction method has a name in the MATLAB documentation?

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

その他の回答 (0 件)

カテゴリ

製品

リリース

R2018b

質問済み:

2019 年 3 月 10 日

編集済み:

2019 年 3 月 10 日

Community Treasure Hunt

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

Start Hunting!

Translated by