Sum the solution of solve function
Show older comments
Dear friends,
When solving a system of equations by using S=solve()
I got this kind of result
S =
z11: [1x1 sym]
z12: [1x1 sym]
z13: [1x1 sym]
z21: [1x1 sym]
z22: [1x1 sym]
z23: [1x1 sym]
z31: [1x1 sym]
z32: [1x1 sym]
z33: [1x1 sym]
I would like to find the sum of S.
However it cant be done with sum(S) because: Undefined function or method 'sum' for input arguments of type 'struct'
I can do that manually by first, displaying the solutions, S=[S.z11 S.z12 S.z13 S.z21 S.z22 S.z23 S.z31 S.z32 S.z33] after that, I can finally use sum(S) to get the sum of S.
However, I found this is not practical when I have a huge number of solutions in S.
Is there any other way to get the sum of S? Any help will be greatly appreciated.
Thank you very much.
Sincerely,
Leonardo
Accepted Answer
More Answers (2)
Leonardo
on 23 Feb 2012
Bjorn Gustavsson
on 23 Feb 2012
I can't get Walter's solution to work in my version of matlab (7.8.0.347 (R2009a)). There S is a sym object, and then this works:
S = solve('(x-x1)*(x-x2)*(x-x3)=0');
sum(S)
ans =
x1 + x2 + x3
What versions are you using?
Categories
Find more on Common Operations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!