How can i get the fourier series of any given function
10 次查看(过去 30 天)
显示 更早的评论
Since there is no direct way of finding/computing the fourier seris of a function, e.g. cos(x), how would I do this?
0 个评论
采纳的回答
Friedel Hartmann
2021-11-26
clear
syms x f(x) k fh(x) p(x) ph(x) % a0 = a0 Formel /2
iTerme = 8;
sco = zeros(1,iTerme);
cco = zeros(1,iTerme);
% T = 2*pi, omega = 1
f(x) = piecewise(x <= pi/4, -1/(2*pi) * x, pi/4 < x,-1/(2*pi) * x + 1); % Function
for k=1:iTerme
sco(k) = 1/pi * int(sin(k * x) * (f(x)),[0,2*pi]); % coefficients
cco(k) = 1/pi * int(cos(k * x) * (f(x)),[0,2*pi]);
end
cnum = 1:iTerme; % Table with coefficients
Tabelle.num = cnum';
Tabelle.scof = sco';
Tabelle.ccof = cco';
T = struct2table(Tabelle)
fplot(f(x),[0,2*pi]), title('Original'), yline(0);
figure
a0G = vpa(1/2 * 2/(2*pi) * int(f(x),[0,2*pi]),3)
fh(x) = 1/(2*pi) * int(f(x),[0,2*pi]); % 'Mittelwert' 1. Term ist a0
for k = 1:iTerme
fh(x) = fh(x) + sco(k) * sin(k * x) + cco(k) * cos(k * x);
end
fplot(fh(x),[0,2*pi]), title('Fourier series'), yline(0);
vpa(fh(0),3)
p(x) = x/(2*pi); % 2nd Function
for k=1:iTerme
sco(k) = 1/pi * int(sin(k * x) * (p(x)),[0,2*pi]);
cco(k) = 1/pi * int(cos(k * x) * (p(x)),[0,2*pi]);
end
cnum = 1:iTerme;
Tabelle.num = cnum';
Tabelle.scof = sco';
Tabelle.ccof = cco';
T = struct2table(Tabelle)
fplot(p(x),[0,2*pi]), title('Original'), yline(0);
figure
a0L = vpa(1/2 * 2/(2*pi) * int(p(x),[0,2*pi]),3)
ph(x) = 1/pi * int(p(x),[0,2*pi]) * 1/(2); % 'Mittelwert' 1. Term ist a0/2
for k = 1:iTerme
ph(x) = ph(x) + sco(k) * sin(k * x) + cco(k) * cos(k * x);
end
fplot(ph(x),[0,2*pi]), title('Fourier series'), yline(0);
1 个评论
Friedel Hartmann
2021-11-27
The series starts with a constant term, the average value, called 'Mittelwert' The routine first calculates the average value, assigns this number to ph(x) and then begins the loop for k = 1:iTerme etc. and adds the following sin and cos functions.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Numerical Integration and Differential Equations 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!