How can I sum an expression over the indices of an array?
Show older comments
A simplification of the type of code I require for a more complex situation is:
A=[1*x, 2*x, 3*x, 4*x];
for y=2:length(A)
syms t
symsum(A(y)-A(t-1), t, 2, y)
end
However, this is resulting in the error:
Error using sym/subsindex (line 1558)
Indexing input must be numeric, logical or ':'.
Error in example (line 5)
symsum(A(y)-A(t-1), t, 2, y)
I was expecting the results below:
l = 2 gives A(2) - A(1) = x
l = 3 gives (A(3) - A(1)) + (A(3) - A(2))= 3*x
l = 4 gives (A(4) - A(1)) + (A(4) - A(2)) + (A(4) - A(3))= 6*x
The more complex situation for which I am using this code is:
syms n qq
for y=2:length(t)
for u=1:length(x)
Del_sig(y,u)=materials(material_barrier).gamma*(embSurf(u)-surf(u));
for v=1:length(z)
sumOverTime = symsum((Del_sig(qq,u)-Del_sig(qq-1,u))/materials(layers(1,1)).E*(d-z(v)-(4*d/(n^2*pi^2))*(1-cos(n*pi))*(cos(n*pi*z(u)/(2*d))-cos(n*pi/2))*exp(-n^2*pi^2*c_v*(t(y)-t(qq-1))/(4*d^2))),qq,2,y);
rho1(v,u)=symsum(sumOverTime,n,1,Inf);
end
end
end
I need to compute a sum over qq in terms of n, and then a sum over n. All other variables have been defined earlier; c_v , d, materials(material_barrier).gamma and materials(layers(1,1)).E are scalars; t, x, z, embSurf and surf are arrays.
Any help would be much appreciated.
Answers (1)
Azzi Abdelmalek
on 4 Jul 2015
Edited: Azzi Abdelmalek
on 4 Jul 2015
A=[1, 2, 3, 4];
syms x out
for y=2:numel(A)
out(y-1)= sum(A(y)-A(y-1:-1:1))*x
end
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!