# how to write this summation?

30 views (last 30 days)
work wolf on 24 Feb 2018
Commented: work wolf on 26 Feb 2018
how to write this summation in matlab Edit
if k=1,..n, and stored at each iteration of k ,i.e I tried to use the solution in symbolic
clear;clc
h=0.1; x=0:h:2;n=10;m=3;
w = @(i,j,k) (-1)^(i+j)*cos((j*pi/h)*(x-x(k)));
syms I J
J=1:m;
for k=1:n
f(k)=symsum(symsum(w(I,J,k), J, 1, I), I, 1, m);
end
but the Error
Error in MuPAD command: A square matrix is expected.
[(Dom::Matrix(Dom::ExpressionField()))::exp]
Error in sym/privBinaryOp (line 1693)
Error in sym/mpower (line 189)
B = privBinaryOp(A, p, 'symobj::mpower');
Error in @(i,j,k)(-1)^(i+j)*cos((j*pi/h)*(x-x(k)))
Moreover, I used a alternate codes with loop condition but i don't know if it's right
clear;clc
h=0.1; x=0:h:2;n=10;m=3;
w = @(i,j,k) (-1)^(i+j)*cos((j*pi/h)*(x-x(k)));
for k=1:n;
for i = 1:m;
for j = 1:i;
f(k)= sum(sum(w(i,j,k) ) );
end
end
end
Guillaume on 24 Feb 2018
Is xk a constant or are you missing a k index in your summation?
As it is your expression can be reduced to a single sum from i = 1:2:m of cos(i*pi/h*(x-xk) as all the other terms cancel out.

Guillaume on 24 Feb 2018
I don't think your equation is right. The way to implement that summation properly would be:
[ii, jj] = ndgrid(1:m);
hh = pi*(x-xk)/h;
result = sum(sum(triu((-1).^(ii+jj) .* cos(jj*hh))))
However, because most of the terms cancel out, it could be simplified to:
hh = pi*(x-xk)/h;
result = sum(cos((1:2:m)*hh))
work wolf on 26 Feb 2018
Really, many thanks. Please, can you show code to comply with the equation above without reduce terms in symbolic and for loop way?

javad ebrahimi on 24 Feb 2018
clc
clear
p=0;
h=2;
x=2;
xk=3;
for i=1:10
for j=1:10
p=p+(-1)^(i+j)*cos((j*pi/h)*(x-xk));
end
end
##### 2 CommentsShowHide 1 older comment
work wolf on 25 Feb 2018
Thanks.