Symbolic array summation in MATLAB

3 vues (au cours des 30 derniers jours)
Emipel
Emipel le 7 Oct 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)

Réponse acceptée

Jyothis Gireesh
Jyothis Gireesh le 10 Oct 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!!

Plus de réponses (0)

Catégories

En savoir plus sur Logical dans Help Center et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by