Symbolic array summation in MATLAB

조회 수: 3 (최근 30일)
Emipel
Emipel 2019년 10월 7일
답변: Jyothis Gireesh 2019년 10월 10일
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에서 Logical에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by