Starting with the template provided in SUBOPTIMAL_FOURIER_TEMPLATE.M, create an
ID: 2079063 • Letter: S
Question
Starting with the template provided in SUBOPTIMAL_FOURIER_TEMPLATE.M, create an m-file to compute the Fourier Series approximation of a square wave. Name the file: suboptimal_fourier_LastName.m
%
% suboptimal_fourier_template.m
%
% EE 5003
% [INSERT NAME]
% [INSERT DATE]
%
% Produce a Fourier Series approximation of an even square wave but do so
% using FOR loops and IF-ELSE statements in a suboptimal, non-vectorized
% program structure.
clear all
close all
tic
% Specify frequency of the square wave in Hz and radians
freq = 1;
omega = 2*pi*freq;
% Specify start time, time increment and end time of simulation
tstart = -2; tstep = 0.001; tstop = 2;
% Specify the total number of terms in the approximation
N = 100;
% Loop over the number of terms to compute the coefficients
% [loop variable is n]
for %%FILL IN LOOP VARIABLE INFO
% If n is odd, then check two other conditions
if rem(n,2) > 0
% IF n is 3, 7, 11, ..., then coefficient is -4/(n*pi)
if rem((n-1),4) > 1
a(n) = %%INSERT EXPRESSION FOR COEFFICIENT HERE
% Else n is 1, 5, 9, ..., and coefficient is 4/(n*pi)
else
a(n) = %%INSERT EXPRESSION FOR COEFFICIENT HERE
end
% ELSE n is even so the coefficient is zero
else
a(n) = 0;
end
end
% End loop over the number of terms
% Loop over time to compute the square wave approximation at each time
% point
k = 0;
for t = %%INSERT START:STEP:STOP VALUES HERE
k = k + 1;
f(k) = 0;
% Loop over the total number of terms in the approximation
for n = 1:N,
f(k) = f(k) + %%INSERT EXPRESSION FOR n-th FOURIER TERM AT TIME t
time(k) = t;
end
end
% End loop over time
toc
% Plot the results
plot(time,f)
title(strcat('Fourier Series Approximation with N = ',num2str(N),' terms'))
ylabel('function value')
xlabel('time (seconds)')
Explanation / Answer
clc
tic
N=10;
f = 1;
w = 2*pi*f;
for n=1:1:N
if rem(n,2)==0
a(n)=0
else
if rem(n-1,4)< 1
a(n)=4/n*pi;
else
a(n)=-4/n*pi;
end
end
end
n=3;
k = 0;
for t = -2:0.001:2
k = k + 1;
f(k) = 0;
% Loop over the total number of terms in the approximation
for n = 1:N,
f(k) = f(k) + a(n)*cos(n*w*t)
time(k) = t;
end
end
% End loop over time
toc
% Plot the results
plot(time,f)
title(strcat('Fourier Series Approximation with N = ',num2str(N),' terms'))
ylabel('function value')
xlabel('time (seconds)')
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.