Symbolic array summation in MATLAB

2 次查看(过去 30 天)
I am trying to output the following symbolic expression in MATLAB: How can I genereate this?
10*L(1) + 20*L(2) + 30*L(3) + 10*L(4) + 20*L(5) + 30*L(6) + 10*L(7) + 20*L(8) + 30*L(9);
This is my code:
syms J L(k) k
F = J*L(k)
for p = 1:3
P = [10 20 30]';
fff=subs(F,J,P(p))
ss=symsum(fff,k,1,9)
end
The outputted answers are:
10*L(1) + 10*L(2) + 10*L(3) + 10*L(4) + 10*L(5) + 10*L(6) + 10*L(7) + 10*L(8) + 10*L(9)
20*L(1) + 20*L(2) + 20*L(3) + 20*L(4) + 20*L(5) + 20*L(6) + 20*L(7) + 20*L(8) + 20*L(9)
30*L(1) + 30*L(2) + 30*L(3) + 30*L(4) + 30*L(5) + 30*L(6) + 30*L(7) + 30*L(8) + 30*L(9)

采纳的回答

Jyothis Gireesh
Jyothis Gireesh 2019-10-10
Each iteration of the for loop in the above code creates a new symbolic function "ss" which creates three different expressions. Also, in each iteration of the loop, since the value of “p” is constant, J is substituted by the same value.
You may use the following code instead to implement the summation
clear;
syms L(k)
P = repmat([10 20 30],1,3);
ss(k) = sum(P.*subs(L(k),k,1:9));
Hope this helps!!

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Symbolic Math Toolbox 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by