How to plot Fourier series for function in a right way?
4 次查看(过去 30 天)
显示 更早的评论
if true
% code
a=[0,pi/2,pi/2,pi];
b=[0,-1,0,0];
plot(a,b)
hold on
n=50;
ao=-pi/4;
T=[0,pi,0,1];
for i=1:length(T)
t=linspace(a(i),b(i));
end
suma=0;
for i=1:n
bn=cos(pi*i)/2/i;
an=(1-cos(pi*i))/4/(i)^2;
suma=suma+(bn.*sin(2.*i.*t))+(an.*cos(2.*i.*t));
end
series=ao/2+suma;
plot(t,series)
hold off
end
0 个评论
采纳的回答
Torsten
2018-11-5
编辑:Torsten
2018-11-5
f=@(x)-2/pi*x.*(x>=0 & x<=pi/2)-2*(x/pi+1).*(x>=-pi & x<=-pi/2);
n=50;
k=0:n;
a=1/pi*(integral(@(x)f(x).*cos(k*x),-pi,-pi/2,'ArrayValued',true)+integral(@(x)f(x).*cos(k*x),0,pi/2,'ArrayValued',true));
k=1:n;
b=1/pi*(integral(@(x)f(x).*sin(k*x),-pi,-pi/2,'ArrayValued',true)+integral(@(x)f(x).*sin(k*x),0,pi/2,'ArrayValued',true));
ffun=@(x)a(1)/2+sum(a(2:n+1).*cos((1:n)*x)+b(1:n).*sin((1:n)*x));
x=linspace(0,pi,200);
fx=arrayfun(@(x)ffun(x),x);
plot(x,fx,x,f(x))
0 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Surface and Mesh Plots 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!