Text: EXERCISE #1: FOURIER SERIES - I
The Fourier series for a square wave f(x) = {S(x/2) if 0 <= x < 2, else 0} is given by f(x) = (1/n) * sin(n * x), where n is the number of harmonics.
First, write a user-defined function in MATLAB to accept the number of harmonics, n, as input and produce the Fourier series waveform up to and including the n harmonics using the following code segment:
function [x, f] = FS.sqN
x = linspace(0, 10);
f = 0.5;
for i = 1:2:N
b = 2 / (pi * i);
f = f + b * sin(i * x);
end
Next, write a script in MATLAB to test this function by generating and plotting on the same figure using subplot commands the following cases:
1. N = 5
2. N = 10
3. N = 20
4. N = 50
Label all the axes and assign a title to each subplot.