Plotting a series for n>=1
3 views (last 30 days)
Show older comments
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)
0 Comments
Answers (1)
Jos (10584)
on 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.-')
See Also
Categories
Find more on Annotations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!