how to write this summation?
4 views (last 30 days)
Show older comments
how to write this summation in matlab
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/170748/image.png)
Edit
if k=1,..n, and stored at each iteration of k ,i.e
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/170749/image.png)
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 using mupadmex
Error in MuPAD command: A square matrix is expected.
[(Dom::Matrix(Dom::ExpressionField()))::exp]
Error in sym/privBinaryOp (line 1693)
Csym = mupadmex(op,args{1}.s, args{2}.s, varargin{:});
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
1 Comment
Answers (2)
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))
4 Comments
Guillaume
on 26 Feb 2018
It's not difficult to calculate fk(x) for each x and k, but for a given k, your summation can be greatly simplified, to the point that it looks like something is missing.
For a given k, look at all the terms generated when j = 1, which happens for all i from 1 to m. Let's call hh = pi*(x-xk)/h, which depends neither on j or i. The terms are:
j = 1; i= 1:m
(-1)^2*cos(hh) + (-1)^3*cos(hh) + (-1)^4*cos(hh) + ... + (-1)^(m+1)*cos(hh)
It's basically:
cos(hh)-cos(hh)+cos(hh)+ ... + (-1)^(m+1)*cos(hh)
If m is even that sum reduces to 0, otherwise it reduces to cos(hh). The same happens will all the other j values, the sum reduces either to cos(j*hh) or 0.
Therefore:
fk(x) = sum(cos(j*pi/h*(x-xk))), for j = 2:2:m, if m is even
fk(x) = sum(cos(j*pi/h*(x-xk))), for h = 1:2:m, if m is odd
javad ebrahimi
on 24 Feb 2018
Hi work wolf this code can help you
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
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!