% This code is based on Example 23.1 and demonstrates the Gibbs % phenomenon occuring whenever the set of continuous functions is used % to approximate a discontinuous one. close all % closes all windows opened during the previous runs clear all % clears the memory from the results of previous runs clc % clears screen % The following part of the code produces a large scale picture. m=100; % 2*m is the number of terms in the truncated Fourier x=linspace(-10,10,200); k=(-m:m)'; kk=repmat(k,1,length(x)); f=sinh(pi)/pi*sum((-1).^kk./(1+i*kk).*exp(i*k*x)); subplot(2,1,1) plot(x,real(f)) xlabel('x') pause % Now zoom into the region near the discontinuity: dx=10/m; x1=linspace(pi-dx,pi+dx); n=length(x1); ileft=find(x1<=pi); irght=find(x1>pi); g(1:n/2)=exp(-x1(ileft)); g(n/2+1:n)=exp(2*pi-x1(irght)); k=(-m:m)'; kk=repmat(k,1,n); f1=sinh(pi)/pi*sum((-1).^kk./(1+i*kk).*exp(i*k*x1)); subplot(2,1,2) % Observe the overshoot. It plot(x1,real(f1),x1,g),axis('tight') % does not disappear no matter xlabel('x') % how big m is. This is the % Gibbs phenomenon. ovsht=max(real(f1)-max(g)) % Use different values of m and see that % ovsht, the magnitude of the overshoot, does % not decrease as m increases, while the % width of the region where the overshoot is % felt does decrease.