本帖由Matlab技术论坛原创,原帖参见 http://www.matlabsky.com/thread-249-1-3.html
MATLAB和maple语言均未直接提供求解fourier级数的系数的直接函数,好我们自己动手丰衣足食,下面提供了一个代码,大家可以参照下给个例子说明下:将函数y=x*(x-pi)*(x-2*pi),在(0,2*pi)的范围内傅里叶级数展开syms x
fx=x*(x-pi)*(x-2*pi);
[an,bn,f]=fseries(fx,x,12,0,2*pi)%前12项展开
latex(f)%将f转换成latex代码
an =
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
bn =
[ -12,3/2,-4/9,3/16,-12/125,1/18,-12/343, 3/128,-4/243, 3/250, -12/1331, 1/144]
f =
12*sin(x)+3/2*sin(2*x)+4/9*sin(3*x)+3/16*sin(4*x)+12/125*sin(5*x)+1/18*sin(6*x)+12/343*sin(7*x)+3/128*sin(8*x)+4/243*sin(9*x)+3/250*sin(10*x)+12/1331*sin(11*x)+1/144*sin(12*x)
ans =
12,sin left( x right) +3/2,sin left( 2,x right) +4/9,sinleft( 3,x right) +3/16,sin left( 4,x right) +{frac{12}{125}},sin left( 5,x right) +1/18,sin left( 6,xright) +{frac {12}{343}},sin left( 7,x right) +{frac{3}{128}},sin left( 8,x right) +{frac {4}{243}},sin left(9,x right) +{frac {3}{250}},sin left( 10,x right) +{frac{12}{1331}},sin left( 11,x right) +{frac {1}{144}},sinleft( 12,x right)
function [an,bn,f]=fseries(fx,x,n,a,b)
%傅里叶级数展开
%
%an为fourier余弦项系数
%bn为fourier正弦项系数
%f为展开表达式
%f为给定函数
%x为自变量
%n为展开系数
%a,b为x的区间,默认为[-pi,pi]
%
% by dynamic of Matlab技术论坛
% see also http://www.matlabsky.com
% contact me matlabsky@gmail.com
% 2009-08-27 10:37:25if nargin==3
a=-pi;
b=pi;
end
l=(b-a)/2;
if a+b
fx=subs(fx,x,x+l+a);
end
an=int(fx,x,-l,l)/l;
bn=[];
f=an/2;
for ii=1:n
ann=int(fx*cos(ii*pi*x/l),x,-l,l)/l;
bnn=int(fx*sin(ii*pi*x/l),x,-l,l)/l;
an=[an,ann];
bn=[bn,bnn];
f=f+ann*cos(ii*pi*x/l)+bnn*sin(ii*pi*x/l);
end
if a+b
f=subs(f,x,x-l-a);
end
disp('MatlabSky--打造最优、专业和权威的Matlab技术交流平台!更多信息参见:<ahref="matlab:web http://www.matlabsky.cn">http://www.matlabsky.cn</a>')