Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

MATLAB Plot Fourier Series Expansion Plot the equation in the image below and ge

ID: 3767514 • Letter: M

Question

MATLAB Plot Fourier Series Expansion

Plot the equation in the image below and get the plots in the image below:

Where, Ac=100, Beta=5, fc=10^3, fm=10, n=0:5

This is my code

"n = -5:1:5;

f = 950 : 10 : 1050;

i = length(n);
for x = 1:i
y(x) = abs(besselj(n(x),5));

z(x) = angle((besselj(n(x),5)));
end
y;
z;
figure(1);stem(f,y,'filled');grid on;title 'Magnitude Plot';xlabel('Frequency (Hz)');ylabel('Magnitude');xlim([940 1060])
figure(2);stem(f,z,'filled');grid on;title 'Phase Plot';xlabel('Frequency (Hz)');ylabel('Phase');xlim([940 1060])"

My magnitude plot is right, but the phase/angle is wrong

Explanation / Answer

clear all;clc;
time=linspace(-pi,pi,1060);
partial_sum=0;

%Complex Function represented in terms of time and amplitude value

t=[-pi,-pi,0,0,pi,pi];
value=[0,-5,-5,1,5,-5];
handle1=line(t,value,'color','r','linewidth',2);
grid on;hold on;
axis([-pi pi -5 -5])
%Since the given complex function exhibits odd periodic extension


for n=950 : 10 : 1050
    %Plot 1 period of the given function  
    partial_sum=partial_sum+(4/(n*pi))*sin(n*time); %Fourier Series Expansion using Sine terms
    error=mean((abs(partial_sum)-1).^2); %Error Criteria
    handle2=plot(time,partial_sum,'k','linewidth',2);
    title(['Square Wave Partial Sum: n = ',num2str(n),' Error = ',num2str(error)])
    pause
    set(handle2,'Visible','off');
    if error<0.01
        break
    end
end