How to solve symbolic system of equations?

Hello,
I am dynamically generating a system of symbolic linear equations that I trying to figure out how to solve. For example,
syms a b c
Eqs = [3*a + 4*b + 7*c + 11; 5*a + 3*b + 3*c + 4; 7*a + 13*b + 5*c + 9];
Since these equations are dynamically generated based on user parameters, the variables names can change and so forth, so that is why I need to do this symbolically.
If you do:
rref(Eqs)
you get ans =[1; 0; 0] which is weird.
If anyone knows how to solve this, please let me know. I would appreciate it. If anyone can let me know how to parse these symbolic expressions, I would appreciate it too. How to separate and isolate different variables.
Thanks, Ali

 採用された回答

Kenneth Eaton
Kenneth Eaton 2011 年 1 月 26 日

0 投票

Easy, use the SOLVE function:
>> S = solve(Eqs)
S =
a: [1x1 sym]
b: [1x1 sym]
c: [1x1 sym]
And you can convert the symbolic results in these fields to numeric values using the functions SUBS or DOUBLE:
>> subs(S.a)
ans =
0.2773
Or you could convert all the fields to numeric values and place them in a vector with one call to STRUCTFUN:
>> structfun(@subs,S)
ans =
0.2773 % The value for a
-0.2455 % The value for b
-1.5500 % The value for c

5 件のコメント

Ali
Ali 2011 年 1 月 26 日
Thanks Kenneth that worked out well.
However, I do have an issue. Some of my equations that are generated end up being constants.
E.g.:
Eqs =
[3*a + 4*b + 7*c + 11;
5*a + 3*b + 3*c + 4;
7*a + 13*b + 5*c + 9;
15 ]
solve(Eqs) gives:
Warning: 4 equations in 3 variables.
Warning: Explicit solution could not be found.
Is there an easy way of testing for constants in a vector of symbolic expressions?
I can use the "double" function, but if the element is not a constant value, then it errors and quits the script.
Any thoughts?
Thanks
Ali
Ali 2011 年 1 月 26 日
Hi,
Or, is there any way of solving the eqs in Eqs without going through a lot of steps? I was thinking, I might need to delete the rows that had constant values.
Thanks
Kenneth Eaton
Kenneth Eaton 2011 年 1 月 26 日
Hi Ali,
One way you can remove equations with constant values (i.e. that contain no symbolic variables) is to loop over the equations and check to see if the output from the function SYMVAR is empty. If it is, that equation has no symbolic variables in it and should be removed:
for iEquation = 1:numel(Eqs)
if isempty(symvar(Eqs(iEquation)))
Eqs(iEquation) = [];
end
end
Ali
Ali 2011 年 1 月 26 日
Hi Kenneth,
Thanks again. That was a big help.
Now here is another issue that I am having. Sometimes, I have more equations than I have unknowns. I end up with an overly-constrained system.
I might end up with the following:
Eqs = [3*a + 4*b + 7*c + 11;
5*a + 3*b + 3*c + 4;
7*a + 13*b + 5*c + 9;
3*a + 17*b + 13*c + 5];
Is there any way of solving these equations without involving the use of the optimization toolbox and "linprog(...)??" linprog requires explicit "Aeq*x = Beq" type parameter specifications.
Which begs the question, is it possible to parse these symbolic expressions and separate value from variable?
Sorry for the long post. I would appreciate any help you can give me.
Thanks,
Ali
Walter Roberson
Walter Roberson 2011 年 1 月 27 日
You can use symvar() and coeff() to extract the variables and their coefficients from multinomial systems.

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

その他の回答 (1 件)

Paulo Silva
Paulo Silva 2011 年 1 月 27 日

0 投票

%'3*a + 4*b + 7*c + 11=0' -> '3*a + 4*b + 7*c = -11'
%'5*a + 3*b + 3*c + 4=0' -> '5*a + 3*b + 3*c = -4'
%'7*a + 13*b + 5*c + 9=0' -> '7*a + 13*b + 5*c = -9'
A=[3 4 7
5 3 3
7 13 5];
B=[-11
-4
-9];
X=linsolve(A,B); or X=A\B , same results
a=X(1);
b=X(2);
c=X(3);

カテゴリ

ヘルプ センター および File ExchangeGet Started with Symbolic Math Toolbox についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by