how to write this summation?

4 visualizzazioni (ultimi 30 giorni)
work wolf
work wolf il 24 Feb 2018
Commentato: work wolf il 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 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 Commento
Guillaume
Guillaume il 24 Feb 2018
Modificato: Guillaume il 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.

Accedi per commentare.

Risposte (2)

Guillaume
Guillaume il 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 Commenti
Guillaume
Guillaume il 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
work wolf
work wolf il 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?

Accedi per commentare.


javad ebrahimi
javad ebrahimi il 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
  2 Commenti
Guillaume
Guillaume il 24 Feb 2018
Modificato: Guillaume il 24 Feb 2018
Your second for loop should go from 1 to i to comply with the equation:
for i = 1:10
for j = 1:i
...
work wolf
work wolf il 25 Feb 2018
Thanks.

Accedi per commentare.

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by