How to plot correctly a Fourier series?

조회 수: 4 (최근 30일)
Szalka Gergo
Szalka Gergo 2021년 5월 11일
편집: Aghamarsh Varanasi 2021년 5월 26일
Hi,
I would like to solve this problem related with Fourier series:
I have successfully determined a0, an, and bn. Now I would like to compute first 5,10,15 terms with MATLAB.
My (semi-finished) code:
clear;
max = 5; % first 5 terms
syms t
y = piecewise(-3<t<0,-6,0<t<3,-2);
I = [-3,3];
fplot(y, I) % plot original func.
hold on;
syms x n;
fun1 = -6;
fun2 = -2;
fun3 = fun1 * cos((n * pi) / 3 * x);
fun4 = fun2 * cos((n * pi) / 3 * x);
fun5 = fun1 * sin((n * pi) / 3 * x);
fun6 = fun2 * sin((n * pi) / 3 * x);
a0 = (1/3) * int(fun1,x,[-3 0]) + (1/3) * int(fun2,x,[0 3]);
an = (1/3) * int(fun3,x,[-3 0]) + (1/3) * int(fun4,x,[0 3]);
bn = (1/3) * int(fun5,x,[-3 0]) + (1/3) * int(fun6,x,[0 3]);
c = 0;
AN = [];
for i = 1:max
c= c+1;
AN = [AN subs(an,n,c)];
i = i+1;
end
c = 0;
BN = [];
for i = 1:max
c= c+1;
BN = [BN subs(bn,n,c)];
i = i+1;
end
an and bn values are stored in AN and BN vectors. It seems that this code compute terms correctly, but I don't know how to plot this function.

답변 (1개)

Aghamarsh Varanasi
Aghamarsh Varanasi 2021년 5월 26일
편집: Aghamarsh Varanasi 2021년 5월 26일
Hi,
You can create a 'fourier series' with the coefficients a0, AN and BN arrays and use fplot to plot this symbolic function.
For example, the last part of your code can be modified as follows
AN = [];
BN = [];
fourier_series = 0;
for i = 1:max
AN = [AN subs(an,n,i)];
BN = [BN subs(bn,n,i)];
fourier_series = fourier_series + (AN(i) * cos(i*t) + BN(i) * sin(i*t));
end
fplot(t, (a0/2 + fourier_series), [-3,3])
Hope this helps

카테고리

Help CenterFile Exchange에서 MATLAB에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by