运行了下你的代码,语法上并没有问题,如下
检测代码:
clcclear all
close all
syms x
f1 = (exp(x)+exp(-x))/2;
r = taylor(f1,x,5,0);
disp(r);
输出效果
x^4/24 + x^2/2 + 1
请检查程序上下文,并设置断点单步执行检查中间变量的值是否与预期吻合
我的是2010b,没有这个函数,不过你定义:
----------------------------------------------------------------------------
function
[a0,an,bn]=fseries(f,l,k)
%f为一个周期内的分段函数(分两段,对分两段以上的函数可类似处理)
%2l为周期
%k为近似展开项数,k≥1
%计算傅里叶系数
syms
x
n
L
a0=1/Lint(f(1),-L,0)+1/Lint(f(2),0,L);
an=1/Lint(f(1)cos(npix/L),-L,0)+1/Lint(f(2)cos(npix/L),0,L);
bn=1/Lint(f(1)sin(npix/L),-L,0)+1/Lint(f(2)sin(npix/L),0,L);
%选择作图步长p
if
k>=4
p=fix(k/4);
else
p=1;
end
%绘制4个近似子图
for
i=1:4
f=a0/2+symsum(ancos(npix/L)+bnsin(npix/L),n,1,ip);
f=subs(f,L,l);
subplot(2,2,i),ezplot(f,[-3l,3l]),title(['n=',int2str(ip)])
end
----------------------------------------------------------------------------
测试用例:
输入:
f=sym('[x,0]');
[a0,an,bn]=fseries(f,pi,32)
输出:
a0
=
-1/2L
an
=
-L(cos(npi)+sin(npi)npi-1)/n^2/pi^2
bn
=
-L(-sin(npi)+cos(npi)npi)/n^2/pi^2
这相当于加窗对 x 做滑动平均,挺常见的需求,可以试试下面的代码:
x = (1:20)';nx = length(x);
% 相邻多少元素取平均
nwin = 9;
% 把 x 折叠成想要的样子。
ncol = nx - nwin + 1;
colindex = 0:(ncol-1);
rowindex = (1:nwin)';
xin = zeros(nwin, ncol, 'like', x);
xin(:) = x(bsxfun(@plus, colindex, rowindex)) % 没加分号,可以看看 xin 折叠成了什么样。
% 把头尾没算的元素插入回 y 里。
padding = fix(nwin/2) - 1;
y = [
x(1:1+padding);
mean(xin)';
x(end-padding:end)
];
也可以用自带的 filter 来处理滑动平均。这种方法灵活性差点,不过对于你的需求正合适。:
x = (1:20)';nwin = 9;
yout = filter(ones(1, nwin)/nwin, 1, x);
padding = fix(nwin/2) - 1;
y = [
x(1:1+padding);
yout(nwin:end);
x(end-padding:end)
]
依照精度来判别!!!
比方说要算1/(n^2)的和
先确定精度=0001,再来计算
基本方式如下:
clc;clear;
s=0;
e=1;
n=1;
while e>=0001
ss=s;
s=s+1/(n^2)
n=n+1;
e=s-ss;
end
e
这个程序很简单,那么我就不解释了,希望能够帮助到你
syms x
f=25634e+02x^9+25652e+04x^8+11126e+06x^7+27445e+07x^6+42420e+08x^5+42604e+09x^4+27806e+10x^3+11376e+11x^2+26484e+11x^1+26747e+11;
[an,bn,f]=fseries(f,x,9)
an =
[ (534940000000pi + 75840000000pi^3 + 1704160000pi^5 + (54890000pi^7)/7 + (51304pi^9)/9)/pi, - 27841162880pi^2 - 312101856pi^4 - 410432pi^6 - 287993022720, 8114511920pi^2 + 81257616pi^4 + 102608pi^6 + 101588232120, - (900600920960pi^2)/243 - (982273952pi^4)/27 - (410432pi^6)/9 - 35057038158080/729, ((1769650692405pi)/64 + (16836435865pi^3)/8 + (41032827pi^5)/2 + 25652pi^7)/pi, - (4227534952576pi^2)/3125 - (8216261856pi^4)/625 - (410432pi^6)/25 - 1396634790284544/78125, (228828370640pi^2)/243 + (246645872pi^4)/27 + (102608pi^6)/9 + 9100145814680/729, - (11644479251840pi^2)/16807 - (2302917408pi^4)/343 - (410432pi^6)/49 - 7577990404488960/823543, ((115674711229905pi)/16384 + (271842923365pi^3)/512 + (164535327pi^5)/32 + 6413pi^7)/pi, - (74362229720960pi^2)/177147 - (8886433952pi^4)/2187 - (410432pi^6)/81 - 26720932500558080/4782969]
bn =
[ ((1433935166592pi)/5 + (202410805568pi^3)/5 + (18912298608pi^5)/25 + (54707176pi^7)/25 + (12817pi^9)/25)/pi, -((4524504922239pi)/20 + (257431692587pi^3)/10 + (20628307413pi^5)/50 + (27699647pi^7)/25 + (12817pi^9)/50)/pi, ((200007980924032pi)/1215 + (7256209537984pi^3)/405 + (62852615504pi^5)/225 + (55527464pi^7)/75 + (12817pi^9)/75)/pi, -((1303604357887239pi)/10240 + (17458814037587pi^3)/1280 + (337025962413pi^5)/1600 + (111144647pi^7)/200 + (12817pi^9)/100)/pi, (4291895990965568pi^2)/390625 + (13197877258608pi^4)/78125 + (1389827176pi^6)/3125 + (12817pi^8)/125 + 1008779874054206592/9765625, -((421596615384719pi)/4860 + (7444184615281pi^3)/810 + (63435384719pi^5)/450 + (27802183pi^7)/75 + (12817pi^9)/150)/pi, ((307600766143943808pi)/4117715 + (663474242676032pi^3)/84035 + (1036954330992pi^5)/8575 + (389278168pi^7)/1225 + (1831pi^9)/25)/pi, -((343730544769747239pi)/5242880 + (1133513343417587pi^3)/163840 + (5420416582413pi^5)/51200 + (444924647pi^7)/1600 + (12817pi^9)/200)/pi, ((1396563410616444032pi)/23914845 + (5452466891777984pi^3)/885735 + (5147022055504pi^5)/54675 + (500567464pi^7)/2025 + (12817pi^9)/225)/pi]
f =
cos(6x)((228828370640pi^2)/243 + (246645872pi^4)/27 + (102608pi^6)/9 + 9100145814680/729) - cos(7x)((11644479251840pi^2)/16807 + (2302917408pi^4)/343 + (410432pi^6)/49 + 7577990404488960/823543) - cos(x)(27841162880pi^2 + 312101856pi^4 + 410432pi^6 + 287993022720) - cos(3x)((900600920960pi^2)/243 + (982273952pi^4)/27 + (410432pi^6)/9 + 35057038158080/729) + cos(2x)(8114511920pi^2 + 81257616pi^4 + 102608pi^6 + 101588232120) - cos(5x)((4227534952576pi^2)/3125 + (8216261856pi^4)/625 + (410432pi^6)/25 + 1396634790284544/78125) + sin(5x)((4291895990965568pi^2)/390625 + (13197877258608pi^4)/78125 + (1389827176pi^6)/3125 + (12817pi^8)/125 + 1008779874054206592/9765625) - cos(9x)((74362229720960pi^2)/177147 + (8886433952pi^4)/2187 + (410432pi^6)/81 + 26720932500558080/4782969) + (534940000000pi + 75840000000pi^3 + 1704160000pi^5 + (54890000pi^7)/7 + (51304pi^9)/9)/(2pi) - (sin(4x)((1303604357887239pi)/10240 + (17458814037587pi^3)/1280 + (337025962413pi^5)/1600 + (111144647pi^7)/200 + (12817pi^9)/100))/pi - (sin(6x)((421596615384719pi)/4860 + (7444184615281pi^3)/810 + (63435384719pi^5)/450 + (27802183pi^7)/75 + (12817pi^9)/150))/pi + (sin(9x)((1396563410616444032pi)/23914845 + (5452466891777984pi^3)/885735 + (5147022055504pi^5)/54675 + (500567464pi^7)/2025 + (12817pi^9)/225))/pi - (sin(8x)((343730544769747239pi)/5242880 + (1133513343417587pi^3)/163840 + (5420416582413pi^5)/51200 + (444924647pi^7)/1600 + (12817pi^9)/200))/pi + (cos(8x)((115674711229905pi)/16384 + (271842923365pi^3)/512 + (164535327pi^5)/32 + 6413pi^7))/pi + (cos(4x)((1769650692405pi)/64 + (16836435865pi^3)/8 + (41032827pi^5)/2 + 25652pi^7))/pi + (sin(7x)((307600766143943808pi)/4117715 + (663474242676032pi^3)/84035 + (1036954330992pi^5)/8575 + (389278168pi^7)/1225 + (1831pi^9)/25))/pi + (sin(x)((1433935166592pi)/5 + (202410805568pi^3)/5 + (18912298608pi^5)/25 + (54707176pi^7)/25 + (12817pi^9)/25))/pi + (sin(3x)((200007980924032pi)/1215 + (7256209537984pi^3)/405 + (62852615504pi^5)/225 + (55527464pi^7)/75 + (12817pi^9)/75))/pi - (sin(2x)((4524504922239pi)/20 + (257431692587pi^3)/10 + (20628307413pi^5)/50 + (27699647pi^7)/25 + (12817pi^9)/50))/pi
>> an=vpa(an,6)
an =
[ 14570410^12, -5935710^11, 18968910^11, -88255310^10, 50445110^10, -3252510^10, 22677910^10, -16701810^10, 12807410^10, -10130410^10]
>> bn=vpa(bn,6)
bn =
[ 76212810^11, -52155510^11, 36936910^11, -28297710^11, 22862310^11, -19154210^11, 1647110^11, -14442410^11, 12856110^11]
用Matlab对室外温度波进行傅里叶级数拟合可以这样来处理。
1、首先,建立自定义函数,有关傅里叶级数的函数表达式
function y=func(a,n,x) %a——拟合系数,n——阶数,x——自变量
y=傅里叶级数的函数表达式
end
2、必须有x、y对应的数据(十组以上)
3、用拟合函数nlinfit()求拟合系数
[a,r]=nlinfit(x,y,@(a)func(a,n,x),a0) %a——拟合系数,r——残差,a0——初值
当r约等于0,可以认为拟合是合理的。
fsin=@(n,x) pi/4sign(x)sin(nx);
fcos=@(n,x) pi/4sign(x)cos(nx);
N=20;
Fsin=zeros(1,N+1);
Fcos=zeros(1,N+1);
for n=0:N
Fsin(n+1)=quad(@(x)fsin(n,x),-pi,pi,1e-8)/pi;
Fcos(n+1)=quad(@(x)fcos(n,x),-pi,pi,1e-8)/pi;
end
subplot(211),stem(0:N,Fsin);title('sin');
subplot(212),stem(0:N,Fcos);title('cos');
原来的信号可以表示为[-pi,pi]内的 pi/4sign(x)
反正就是根据傅立叶展开的公式,求原来函数和sinkx,coskx的乘积的积分
上述程序里Fsin 就是 bk
Fcos 就是 ak
最后
以上就是关于有关matlab中的泰勒级数用法。全部的内容,包括:有关matlab中的泰勒级数用法。、matlab傅里叶级数展开fseries函数系统不认识、matlab级数求和代码等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)