How to plot correctly a Fourier series?
1 回表示 (過去 30 日間)
古いコメントを表示
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.
0 件のコメント
回答 (1 件)
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