Sum the solution of solve function
显示 更早的评论
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
采纳的回答
更多回答(2 个)
Bjorn Gustavsson
2012-2-23
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?
类别
在 帮助中心 和 File Exchange 中查找有关 Common Operations 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!