System of Linear algebraic eqn. to matrix form

3 ビュー (過去 30 日間)
Nunthadech Rodcheuy
Nunthadech Rodcheuy 2012 年 6 月 3 日
回答済み: Kevin O 2013 年 12 月 6 日
I would like to convert system of algebraic equation to Matrix form
I knew that in Mathematica "CoefficientsArray[expression,{x,y,z}]" will help me pull out those coefficients of unknowns and it can also arranges in order.
For ex. unknowns are x, y, z
eqn : 2x+3y=0
>>> then in Mathematica i can do [2 3 0] or even [3 0 2], my point is, it can manage the order . >>>However, so far in MATLAB that i knew, the cmd is "coeffs(expr)", i got [2 3] and that's it no 0 for z
Does anyone know how to assign an order as well as include 0 coefficient in the out put array?
Thk

採用された回答

Nunthadech Rodcheuy
Nunthadech Rodcheuy 2012 年 6 月 5 日
I've got a sol. for those who are in the same situation
try using >>>subs()
for ex.
[subs(f,{x,y,z},{0,0,1}) subs(f,{x,y,z},{0,1,0}) subs(f,{x,y,z},{1,0,0})]
result is
ans = [0 3 2]

その他の回答 (2 件)

Walter Roberson
Walter Roberson 2012 年 6 月 3 日
You can specify the indeterminents for ceoffs
  1 件のコメント
Nunthadech Rodcheuy
Nunthadech Rodcheuy 2012 年 6 月 3 日
Could you please give an sample code?
for ex. input 2*x+3*y+0*z output [3 0 2]
i've tried specify in the following way but it's not work for me , out put is still an equation.
syms x y z
>> coeffs(2*x+3*y,[x y z])
ans =
2*x+3*y
Anyway, thk a lot, i really appreciate your suggestion

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


Kevin O
Kevin O 2013 年 12 月 6 日
For anyone using versions earlier than 2013, which has the equationstoMatrix function, I used this block of code to create a 6X6 coefficient matrix from a vector of equations and variables. It isn't pretty, but that's probably why they added the new function:
% for i=1:6
K(i,:)=[subs(EQ(i),[a1 a2 a3 a4 a5 a6],[1 0 0 0 0 0]) ...
subs(EQ(i),[a1 a2 a3 a4 a5 a6],[0 1 0 0 0 0]) ...
subs(EQ(i),[a1 a2 a3 a4 a5 a6],[0 0 1 0 0 0]) ...
subs(EQ(i),[a1 a2 a3 a4 a5 a6],[0 0 0 1 0 0]) ...
subs(EQ(i),[a1 a2 a3 a4 a5 a6],[0 0 0 0 1 0]) ...
subs(EQ(i),[a1 a2 a3 a4 a5 a6],[0 0 0 0 0 1])];
end

カテゴリ

Help Center および File ExchangeOrdinary Differential Equations についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by