Graph of first 4 partial sums for Fourier series

fourier seriessequences-and-series

I have the following Fourier series

$$f(x)=\sum_{n=1}^∞ a_n\cos\frac{nπx}{2}$$
$$a_n = \frac{20}{nπ}\Bigl(\cos\frac{nπ}{2}-1\Bigr)-\frac{10}{nπ}\sin\frac{nπ}{2}$$
$$a_0=12.5$$

so my task is to graph a single (1) period and first 4 partial sums.
$$S_0=\frac{a_0}{2}$$
$$S_1=\frac{a_0}{2}+a_1\cos\frac{πx}{2}$$
$$S_2=\frac{a_0}{2}+a_1\cos\frac{πx}{2}+a_2\cosπx$$
$$S_3=\frac{a_0}{2}+a_1\cos\frac{πx}{2}+a_2\cosπx+a_3\cos\frac{3πx}{2}$$

$$-2≤x≤2$$

I input values in excel and get following graph
enter image description here

Relevant excel document in link. (sheet(1))
https://docs.google.com/spreadsheets/d/e/2PACX-1vR8dEBE3RNVJvoKdp5HU_GLp69R6Wd1In4XDFGTAiTu4mTJFcKhwe5E9dc1QpQKcI8SRm1wo1khkajc/pubhtml

In short, my question is – does given graphs look correct for given partial sums?

Best Answer

I can see two problems.

First off, your Fourier coefficients as given are only half as big as they should be to match your Series1 data. Perhaps you used the even nature of the target function so you only integrated over half the interval but then forgot to multiply by $2$ to take into account the half of the integral you didn't do.

Second, your value for $a_3$ is very high. I didn't actually look at your spreadsheet; here's my Matlab program:

% fouriertest.m

npts = 300;
x = linspace(-2,2,npts);
xgap = [-2 -1 -1 -1 0 0 0 1 1 1 2];
ygap = [20 20 NaN 10 0 NaN 0 10 NaN 20 20];
y0 = 25/2*ones(size(x));
y1 = (-20/pi-40/pi^2)*cos(pi/2*x);
y2 = -20/pi^2*cos(pi*x);
y3 = (20/3/pi-40/pi^2)*cos(3*pi/2*x);
plot(xgap,ygap,'b-',x,y0,'r-',x,y0+y1,'g-',x,y0+y1+y2,'m-', ...
    x,y0+y1+y2+y3,'c-');
title('Partial Sums of Fourier Series');
xlabel('x');
ylabel('y');
legend('Series1','S_0','S_1','S_2','S_3','Location','southeast');

And its output: untitled.png

BTW, your expression for the Fourier series has a typo which I an gonna fix.