Solve a variable-size array of linear eqns

Hi,
I need to solve some (between 2 and 20, say) linear equations. Up til now I calculate the expressions in a loop, saving them in an array, e.g.
6
3 + 2*Y1
10 +0.5*Y1 - 3*Y2
and then I manually run:
A = solve('Y1=6','Y2=3+2*Y1','Y3=10+0.5*Y1-3*Y2','Y1','Y2','Y3')
I want to be able to save any number of these expressions and pass an array of them as input to the 'solve' function, so something like:
A = solve(Y)
Is this possible?
Thanks

 採用された回答

Andrew Newell
Andrew Newell 2011 年 6 月 22 日

0 投票

It would be much faster to express this as a matrix equation, e.g.,
M = [1 0 0; -2 1 0; -0.5 3 1];
b = [6; 3; 10];
y = M\b % solve M*y = b
Or, if you prefer a symbolic solution,
M = sym([1 0 0; -2 1 0; -0.5 3 1]);
b = sym([6; 3; 10]);
y = M\b

1 件のコメント

eCon
eCon 2011 年 6 月 22 日
Thanks!
Embarrassed I didn't see this myself. I had to rework some of the earlier code to get it into a format where I could use your solution, but it's much easier than the route I was taking.

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeMathematics についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by