My plots are getting overidded by other plots. How do I fix this issue?
Show older comments
This is used to determine the Fourier Series representation
clear
clc
%Making the original plot and defining points
N = 1024;
dt = 2/(N-1);
t = 0:dt:2;
ft = zeros(1,length(t));
ft(1:N/2-1)= 2;
ft(N/2:end)= -2;
%Plotting points
figure
plot(t,ft,LineWidth=2)
title("Original Plot f(t) v/s t")
xlabel("t")
ylabel("f(t)")
grid
xlim([t(1)-1,t(end)+1])
%Finding fourier series using the below function
FourierSeries_Calculator(ft,t,dt,100)
function FourierSeries_Calculator(ft,t,dt,n)
arguments
ft (1,:) double
t (1,:) double
dt (1,1)
n (1,1) {mustBeNumeric,mustBePositive,mustBeGreaterThan(n,20)}
end
%Used to calculate the fundamental time period
T0 = t(end)-t(1);
w0 = 2*pi/T0;
%Finding dc value,phase and magnitude components
[a,w,phase] = deal(zeros(1,2*n+1));
for m = -n:n
a(m+n+1) = sum(ft.*exp(-1i*m*w0.*t))*dt/T0;
w(m+n+1) = m*w0;
phase(m+n+1) = angle(a(m+n+1));
end
%Plotting Phase and Magnitude spectrum
figure
stem(w,abs(a))
title("Magnitude Spectrum")
xlabel("w0")
ylabel("|a_n|")
grid
figure
stem(w,phase)
title("Phase Spectrum")
xlabel("w_0")
ylabel("Phase (in radians)")
grid
FourierSeries_Plot(a,t,w0,n)
end
function FourierSeries_Plot(a,t,w0,n)
arguments
a (1,:)
t (1,:)
w0 (1,1) {mustBePositive}
n (1,1) {mustBePositive}
end
%An animated plot, once the for loop is completed, the plot is cleared and
%the Final fourier Series representation is displayed
FourierS = 0;
figure
for m = -n:n
FourierS = FourierS + a(n+m+1)*(exp(1i*w0*m.*t));
plot(t,real(FourierS),LineWidth=2)
pause(0.01)
end
clf
plot(t,real(FourierS),LineWidth=2)
title("Fourier Series Representation")
xlabel("t")
grid
end
%OUTPUT

%This same figure appears for the first 3 figures

%Only the last figure shows the correct output
Accepted Answer
More Answers (0)
Categories
Find more on 2-D and 3-D Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!






