Symbolic array summation in MATLAB

5 Ansichten (letzte 30 Tage)
Emipel
Emipel am 7 Okt. 2019
Beantwortet: Jyothis Gireesh am 10 Okt. 2019
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)

Akzeptierte Antwort

Jyothis Gireesh
Jyothis Gireesh am 10 Okt. 2019
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!!

Weitere Antworten (0)

Kategorien

Mehr zu Symbolic Math Toolbox finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by