gibbs phenomena in matlab

The Gibbs phenomenon refers to the oscillations that occur near discontinuities when approximating a function using a Fourier series. Here is how you can observe the Gibbs phenomenon in MATLAB:

  1. Define a square wave function:
main.m
T = 1;  % period of the square wave
t = linspace(0, 4*T, 1000);  % time vector
square_wave = @(t) sign(sin(2*pi*t/T));
119 chars
4 lines
  1. Plot the square wave:
main.m
figure;
plot(t, square_wave(t));
title('Square Wave');
xlabel('Time');
ylabel('Amplitude');
92 chars
6 lines
  1. Approximate the square wave using a Fourier series:
main.m
n = 50;  % number of terms in the Fourier series
approximation = zeros(size(t));
for k = 1:n
    coefficient = 4/pi * sin(2*pi*k*t/T) / k;
    approximation = approximation + coefficient;
end
192 chars
7 lines
  1. Plot the approximation:
main.m
figure;
plot(t, approximation);
title('Approximation using Fourier series');
xlabel('Time');
ylabel('Amplitude');
114 chars
6 lines
  1. Plot the difference between the square wave and the approximation:
main.m
figure;
plot(t, square_wave(t) - approximation);
title('Gibbs Phenomenon');
xlabel('Time');
ylabel('Difference');
114 chars
6 lines

When you run this code, you will observe that the Fourier series approximation of the square wave exhibits oscillations near the discontinuities. These oscillations are known as the Gibbs phenomenon.

gistlibby LogSnag