Plotting a series for n>=1

조회 수: 2 (최근 30일)
Sultan Al-Hammadi
Sultan Al-Hammadi 2019년 6월 1일
댓글: Sultan Al-Hammadi 2019년 6월 1일
I have tried plotting the following function but I got a weird plot (really different than expected). I am trying for now to plot the function for a large n (finite). Any help would be great.
the function: f(x)= (1/3) - (8/pi^(2))*{infinite sum from n= 1 of:} [ ( (cos((n*pi)/2) - (2/(n*pi))*sin((n*pi)/2)) /n^(2) ) * cos((n*pi*x)/pi) ]
My code is:
N = 200;
x = linspace(-3*pi, 3*pi, 200);
[X, n] = meshgrid(x, 2:N);
y = (1/3) - (8/pi^(2))*sum((((cos((n*pi)/2)-(2./(n.*pi)).*sin((n*pi)/2))./n.^(2)).*cos((n.*X)/2)),1);
plot(x, y)

답변 (1개)

Jos (10584)
Jos (10584) 2019년 6월 1일
I suggest you avoid meshgrid here. Another tip is to rewrite your function to a somewat simpler form, so you do not loose track of of the opening and closing brackets.
N = 200 ;
n = 1:N % you wrote 2:N in your code??
m = (1:N)*(pi/2) ; % simpler form
sumfun = @(x) sum((cos(m) - (1./m).*sin(m)./ (n.^2) ) .* cos(n.*x))
x = linspace(-3*pi, 3*pi, 200);
y = (1/3) - (8/(pi^2)) * arrayfun(@(z) sumfun(z), x) ; % apply to each value of x
plot(x,y,'b.-')
  댓글 수: 1
Sultan Al-Hammadi
Sultan Al-Hammadi 2019년 6월 1일
there might be something wrong with you code as it should look like a periodic function: equals 1-x^2 from -1 to 1 and equals zero from (-2 to -1) & (1 to 2).

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Annotations에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by