MATLAB Answers

how can i perform a Fourier series on this function?

93 views (last 30 days)
clear all;
close all;
clc;
syms n t1 t2 T f0 t;%t2=T;
func=(f0/t2-t1)*(t-t1);
n=1:10;
syms t t1 T;%T=t2;
w = (2*pi)/T;
a0 = (2/T)*int(func,t,0,T);
an = (2/T)*int(func*cos(n*w*t),t,0, T);
bn = (2/T)*int(func*sin(n*w*t),t, 0,T);
f = a0/2 + dot(an,cos(n*w*t)) + dot (bn, sin(n*w*t));
ezplot(func);
hold on;
grid on;
plot(f);

More Answers (2)

Abraham Boayue
Abraham Boayue on 19 Mar 2018
Hey Mahammed, instead of calculating the Fourier series like you did in matlab, why don't you calculate the coefficients by hand? What is the expression for f(x) and the values of f0 and t1?

Abraham Boayue
Abraham Boayue on 20 Mar 2018
Mohammed, if you calculate the coefficients of the Fourier series of f(t), you will get a0 and an equal to zero. This is because f(t) is an odd function, only bn has value. The following code implements your equation.
clear variables
close all
N = 1000;
%%1. Generate and plot f(x)
T = 4;
t1 = 2;
t2 = T;
fo = 20;
t = -T:2*T/(N-1):T;
f = (fo/(t2-t1))*(t-t1);
figure
plot(t,f,'linewidth',1.5,'color','m')
grid;
a = title('f(x)');
set(a,'fontsize',14);
a = ylabel('y');
set(a,'Fontsize',14);
a = xlabel('x (-4 4]');
xlim([-4 4]);
set(a,'Fontsize',14);
%%Define variable for F series
to = -16;
tf = 16;
t2 = to:(tf-to)/(N-1):tf;
F = zeros(1,N);
F1 = F;
F2 = F1;
% This is just a vectorized version of the for loop. It's a faster way
% of coding the F-series.
m = 1:N;
bn = (T^2./(pi*m)).*(-1).^(m+1);
F1000 = bn*sin((pi/T)*m'*t2); % 1000th Harmonic
% Gererate the F-series with a for loop, flexible.
% 5th Harmonic
for n = 1:5
an = 0;
bn = (T^2/(n*pi))*(-1)^(n+1);
w = (pi*n)/T;
F = F + bn*sin(w*t2);
end
% 15th Harmonic
for n = 1:15
an = 0;
bn = (T^2/(n*pi))*(-1)^(n+1);
w = (pi*n)/T;
F1 = F1 +bn*sin(w*t2);
end
% 25th Harmonic
for n = 1:25
bn = (T^2/(n*pi))*(-1)^(n+1);
w = (pi*n)/T;
F2 = F2 + bn*sin(w*t2);
end
figure
plot(t2,F,'linewidth',1.5,'color','r')
hold on
plot(t2,F1,'linewidth',1.9,'color','g')
plot(t2,F2,'linewidth',1.5,'color','m')
plot(t2,F1000,'linewidth',1.8,'color','b')
grid;
a = title('Fourier Series of f(x) 5th, 15th , 25th and 1000th Harmonics');
legend('F5','F15','F25','F1000');
set(a,'fontsize',14);
a = ylabel('y');
set(a,'Fontsize',14);
a = xlabel('t [-16 16]');
xlim([to tf]);
set(a,'Fontsize',14);

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!