Figure 2.5:

Truncated trigonometric Fourier series approximation to f(x)=x, using K=5,10, 50. The wiggles get finer as K increases.

Code for Figure 2.5

Text of the GNU GPL.

main.m


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
% plot for Trig Fourier Series of f(x)=x.
% (after JBR solution)
%MDG 11/25/12
nxs = 301; % number of points
x = linspace(-pi, pi, nxs); %points for plotting
nterms = [5, 10, 50]; % number of terms for sums
u = zeros(length(nterms), nxs);
for i = 1:length(nterms)
  for k = 1:nterms(i);
              sign = (-1)^k;
        un0 = -2*sign/k;
        u(i,:) = u(i,:)+ un0*sin(k*(x));
  end
end
plot(x,u)

table = [x' u'];
save Fourierx.dat table