您好,欢迎访问三七文档
当前位置:首页 > 办公文档 > 求职简历 > Matlab数据拟合
数据拟合用Matlab进行数据拟合1.多项式曲线拟合:polyfit.y0=polyval(p,x0)p=polyfit(x,y,m)其中,x,y为已知数据点向量,分别表示横,纵坐标,m为拟合多项式的次数,结果返回m次拟合多项式系数,从高次到低次存放在向量p中.可求得多项式在x0处的值y0.例1已知观测数据点如表所示xy0-0.4470.11.9780.23.280.36.160.47.080.57.340.67.660.79.560.89.480.99.3111.2分别用3次和6次多项式曲线拟合这些数据点.x=0:0.1:1y=[-0.447,1.978,3.28,6.16,7.08,7.34,7.66,9.56,9.48,9.3,11.2]plot(x,y,'k.','markersize',25)axis([01.3-216])p3=polyfit(x,y,3)p6=polyfit(x,y,6)编写Matlab程序如下:t=0:0.1:1.2s=polyval(p3,t)s1=polyval(p6,t)holdonplot(t,s,'r-','linewidth',2)plot(t,s,'b--','linewidth',2)gridx=0:0.1:1y=[-0.447,1.978,3.28,6.16,7.08,7.34,7.66,9.56,9.48,9.3,11.2]plot(x,y,'k.','markersize',25)axis([01.3-216])p3=polyfit(x,y,3)p6=polyfit(x,y,6)例2用切削机床进行金属品加工时,为了适当地调整机床,需要测定刀具的磨损速度.在一定的时间测量刀具的厚度,得数据如表所示:切削时间t/h030.0129.1228.4328.1428.0527.7627.5727.2827.0刀具厚度y/cm切削时间t/h926.81026.51126.31226.11325.71425.31524.81624.0刀具厚度y/cm解:描出散点图,在命令窗口输入:t=[0:1:16]y=[30.029.128.428.128.027.727.527.227.026.826.526.326.125.725.324.824.0]plot(t,y,'*')解:描出散点图,在命令窗口输入:t=[0:1:16]y=[30.029.128.428.128.027.727.527.227.026.826.526.326.125.725.324.824.0]plot(t,y,'*')a=-0.301229.3804holdonplot(t,y1),holdoffa=polyfit(t,y,1)y1=-0.3012*t+29.3804例2用切削机床进行金属品加工时,为了适当地调整机床,需要测定刀具的磨损速度.在一定的时间测量刀具的厚度,得数据如表所示:切削时间t/h030.0129.1228.4328.1428.0527.7627.5727.2827.0刀具厚度y/cm切削时间t/h926.81026.51126.31226.11325.71425.31524.81624.0刀具厚度y/cm拟合曲线为:y=-0.3012t+29.3804例3一个15.4cm×30.48cm的混凝土柱在加压实验中的应力-应变关系测试点的数据如表所示1.552/N/m2//N/m65001033.103102.47610001032.465102.93615001031.953103.03620001031.51710已知应力-应变关系可以用一条指数曲线来描述,即假设21kke式中,表示应力,单位是N/m2;表示应变.2.89623751031.21910已知应力-应变关系可以用一条指数曲线来描述,即假设21kke式中,表示应力,单位是N/m2;表示应变.解选取指数函数作拟合时,在拟合前需作变量代换,化为k1,k2的线性函数.于是,12lnlnkk令0211ln,,lnzakak即01zaa在命令窗口输入:x=[500*1.0e-61000*1.0e-61500*1.0e-62000*1.0e-62375*1.0e-6]y=[3.103*1.0e+32.465*1.0e+31.953*1.0e+31.517*1.0e+31.219*1.0e+3]z=log(y)a=polyfit(x,z,1)k1=exp(8.3009)w=[1.552.472.933.032.89]plot(x,w,'*')y1=exp(8.3009)*x.*exp(-494.5209*x)plot(x,w,'*',x,y1,'r-')已知应力-应变关系可以用一条指数曲线来描述,即假设21kke式中,表示应力,单位是N/m2;表示应变.拟合曲线为:3-494.52094.027510e0211ln,,ln,zakak01zaa0211-494.5209,ln8.3009,akak3124.027510,494.5209kk令则求得于是在实际应用中常见的拟合曲线有:01yaxa直线101nnnyaxaxa多项式一般n=2,3,不宜过高.01ayax双曲线(一支)bxyae指数曲线2.非线性曲线拟合:lsqcurvefit.功能:x=lsqcurvefit(fun,x0,xdata,ydata)[x,resnorm]=lsqcurvefit(fun,x0,xdata,ydata)根据给定的数据xdata,ydata(对应点的横,纵坐标),按函数文件fun给定的函数,以x0为初值作最小二乘拟合,返回函数fun中的系数向量x和残差的平方和resnorm.例4已知观测数据点如表所示xy03.10.13.270.23.810.34.50.45.180.560.67.050.78.560.89.690.911.25113.17求三个参数a,b,c的值,使得曲线f(x)=aex+bx2+cx3与已知数据点在最小二乘意义上充分接近.首先编写存储拟合函数的函数文件.functionf=nihehanshu(x,xdata)f=x(1)*exp(xdata)+x(2)*xdata.^2+x(3)*xdata.^3保存为文件nihehanshu.m例4已知观测数据点如表所示xy03.10.13.270.23.810.34.50.45.180.560.67.050.78.560.89.690.911.25113.17求三个参数a,b,c的值,使得曲线f(x)=aex+bx2+cx3与已知数据点在最小二乘意义上充分接近.编写下面的程序调用拟合函数.xdata=0:0.1:1;ydata=[3.1,3.27,3.81,4.5,5.18,6,7.05,8.56,9.69,11.25,13.17];x0=[0,0,0];[x,resnorm]=lsqcurvefit(@nihehanshu,x0,xdata,ydata)编写下面的程序调用拟合函数.xdata=0:0.1:1;ydata=[3.1,3.27,3.81,4.5,5.18,6,7.05,8.56,9.69,11.25,13.17];x0=[0,0,0];[x,resnorm]=lsqcurvefit(@nihehanshu,x0,xdata,ydata)程序运行后显示x=3.00224.03040.9404resnorm=0.0912例4已知观测数据点如表所示xy03.10.13.270.23.810.34.50.45.180.560.67.050.78.560.89.690.911.25113.17求三个参数a,b,c的值,使得曲线f(x)=aex+bx2+cx3与已知数据点在最小二乘意义上充分接近.说明:最小二乘意义上的最佳拟合函数为f(x)=3ex+4.03x2+0.94x3.此时的残差是:0.0912.f(x)=3ex+4.03x2+0.94x3.拟合函数为:练习:1.已知观测数据点如表所示xy03.10.13.270.23.810.34.50.45.180.560.67.050.78.560.89.690.911.25113.17求用三次多项式进行拟合的曲线方程.2.已知观测数据点如表所示xy1.617.72.7491.313.14.1189.43.6110.82.334.50.644.9409.13652.436.9求a,b,c的值,使得曲线f(x)=aex+bsinx+clnx与已知数据点在最小二乘意义上充分接近.插值问题已知n+1个节点,,1,0(),(njyxjj其中jx互不相同,不妨设01),naxxxb求任一插值点)(*jxx处的插值.*y节点可视为由)(xgy产生,g表达式复杂,甚至无表达式0x1xnx0y1y*x*y(,)(0,1,)jjxyjn1.分段线性插值xjxj-1xj+1x0xn实用插值方法机翼下轮廓线2.三次样条插值细木条:样条输入:节点x0,y0,插值点x(均为数组,长度自定义);输出:插值y(与x同长度数组).1.分段线性插值:已有程序y=interp1(x0,y0,x)y=interp1(x0,y0,x,’linear’)2.三次样条插值:已有程序y=interp1(x0,y0,x,’spline’)或y=spline(x0,y0,x)用Matlab作插值计算例5对在[-1,1]上,用n=20的等距分点进行分段线性插值,绘制f(x)及插值函数的图形.2119fxx解在命令窗口输入:x=-1:0.1:1y=1./(1+9*x.^2)xi=-1:0.1:1yi=interp1(x,y,xi)plot(x,y,'r-',xi,yi,'*')例6对在[-5,5]上,用n=11个等距分点作分段线性插值和三次样条插值,用m=21个插值点作图,比较结果.211yx解在命令窗口输入:n=11,m=21x=-5:10/(m-1):5y=1./(1+x.^2)z=0*xx0=-5:10/(n-1):5y0=1./(1+x0.^2)y1=interp1(x0,y0,x)y2=interp1(x0,y0,x,'spline')[x'y'y1'y2']plot(x,z,'r',x,y,'k:',x,y1,'b',x,y2,'g')gtext('Piece.-linear.'),gtext('Spline'),gtext('y=1/(1+x^2)')01.00001.00001.00000.50000.80000.75000.82051.00000.50000.50000.50001.50000.30770.35000.29732.00000.20000.20000.20002.50000.13790.15000.14013.00000.10000.10000.10003.50000.07550.07940.07454.00000.05880.05880.05884.50000.04710.04860.04845.00000.03850.03850.0385例6对在[-5,5]上,用n=11个等距分点作分段线性插值和三次样条插值,用m=21个插值点作图,比较结果.211yxxyy1y2解在命令窗口输入:例7在一天24h内,从零点开始每间隔2h测得的环境温度为12,9,9,10,18,24,28,27,25,20,18,15,13C(单位:)推测在每1s时的温度.并描绘温度曲线.t=0:2:24T=[129910182428272520181513]plot(t,T,'*')ti=0:1/3600:24T1i=interp1(t,T,ti)plot(t,T,'*',ti,T1i,'r-')T2i=interp1(t,T,ti,'spline'
本文标题:Matlab数据拟合
链接地址:https://www.777doc.com/doc-1873985 .html