Plotting a series for n>=1

2 Ansichten (letzte 30 Tage)
Sultan Al-Hammadi
Sultan Al-Hammadi am 1 Jun. 2019
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)

Antworten (1)

Jos (10584)
Jos (10584) am 1 Jun. 2019
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 Kommentar
Sultan Al-Hammadi
Sultan Al-Hammadi am 1 Jun. 2019
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).

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Graphics finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by