最小二乘法曲线拟合

上传人:博****1 文档编号:494233295 上传时间:2023-12-23 格式:DOCX 页数:5 大小:18.56KB
返回 下载 相关 举报
最小二乘法曲线拟合_第1页
第1页 / 共5页
最小二乘法曲线拟合_第2页
第2页 / 共5页
最小二乘法曲线拟合_第3页
第3页 / 共5页
最小二乘法曲线拟合_第4页
第4页 / 共5页
最小二乘法曲线拟合_第5页
第5页 / 共5页
亲,该文档总共5页,全部预览完了,如果喜欢就下载吧!
资源描述

《最小二乘法曲线拟合》由会员分享,可在线阅读,更多相关《最小二乘法曲线拟合(5页珍藏版)》请在金锄头文库上搜索。

1、曲线拟合(curve-fitting):工程实践中,用测量到的一些离散的数据(x , y ),i二0,l,2,.m求一个近似的函数申(x)来拟合这组数据,要求所得的拟合曲 i i线能最好的反映数据的基本趋势(即使申(x)最好地逼近f G),而不必满足插值 原则。因此没必要取申(X ) = y,只要使6=9 (x ) - y尽可能地小)。iiiii原理:给定数据点(x , y ),i = 0,l,2,.m。求近似曲线申(x)。并且使得近似曲线与f G)的偏差最i i小。近似曲线在该点处的偏差6 =9(x )- y,i=1,2,.,m。iii常见的曲线拟合方法:1使偏差绝对值之和最小2.使偏差绝对

2、值最大的最小3使偏差平方和最小i=j按偏差平方和最小的原则选取拟合曲线,并且采取二项式方程为拟合曲线的方法,称为 最小二乘法。推导过程:1. 设拟合多项式为:2. 各点到这条曲线的距离之和,即偏差平方和如下:3问题转化为求待定系数a . a对等式右边求a偏导数,因而我们得到了:0 ki4、把这些等式化简并表示成矩阵的形式,就可以得到下面的矩阵:5. 将这个范德蒙得矩阵化简后可得到:6.也就是说X*A=Y,那么A = (X*X)-1*X*Y,便得到了系数矩阵A,同时,我们也 就得到了拟合曲线。MATLAB 实现:MATLAB提供了 polyfit ()函数命令进行最小二乘曲线拟合。调用格式:p=

3、polyfit(x,y,n)p,s= polyfit(x,y,n)p,s,mu=polyfit(x,y,n)x,y为数据点,n为多项式阶数,返回p为幂次从高到低的多项式系数向量p。x 必须是单调的。矩阵s包括R (对x进行QR分解的三角元素)、df(自由度)、 normr (残差)用于生成预测值的误差估计。p,s,mu=polyfit(x,y,n)在拟合过程中,首先对x进行数据标准化处理,以在拟合 中消除量纲等影响,mu包含标准化处理过程中使用的x的均值和标准差。polyval()为多项式曲线求值函数,调用格式: y=polyval(p,x)y,DELTA=polyval(p,x,s) y=p

4、olyval(p,x)为返回对应自变量x在给定系数P的多项式的值。 y,DELTA=polyval(p,x,s)使用polyfit函数的选项输出s得出误差估计Y DELTA。 它假设polyfit函数数据输入的误差是独立正态的,并且方差为常数。则Y DELTA 将至少包含50%的预测值。如下给定数据的拟合曲线:x=0.5,1.0,1.5,2.0,2.5,3.0, y=1.75,2.45,3.81,4.80,7.00,8.60。解:MATLAB程序如下:x=0.5,1.0,1.5,2.0,2.5,3.0; y=1.75,2.45,3.81,4.80,7.00,8.60;p=polyfit(x,y

5、,2) x1=0.5:0.05:3.0;y1=polyval(p,x1); plot(x,y,*r,x1,y1,-b) 运行结果如图1 计算结果为:p =0.5614 0.8287 1.1560即所得多项式为 y=0.5614xT+0.08287x+1.15560图1最小二乘法曲线拟合示例对比检验拟合的有效性:例:在0,n区间上对正弦函数进行拟合,然后在0,2n 区间画出图形,比较拟合 区间和非拟合区间的图形,考察拟合的有效性。在MATLAB中输入如下代码:clearx=0:0.1:pi; y=sin(x);p,mu=polyfit(x,y,9) x1=0:0.1:2*pi;y1=sin(x1

6、);%实际曲线 y2=polyval(p,x1);%根据由区间0到pi上进行拟合得到的多项式计算0到2pi上 的函数值,%需要注意的是polyval ()返回的函数值在pi到2pi上并 没有进行拟合plot(x1,y2,k*,x1,y1,k-)运行结果:p =0.00000.0000-0.00030.00020.00800.0002-0.16680.0000 1.0000 0.0000mu =R: 10x10 doubledf: 22normr: 1.6178e-07MATLAB的最优化工具箱还提供了 lsqcurvefit ()函数命令进行最小二乘曲线 拟合(Solve nonlinear

7、curve-fitting (data-fitting) problems in least-squares sense)。调用格式:x = lsqcurvefit(fun,xO,xdata,ydata)x = lsqcurvefit(fun,xO,xdata,ydata,lb,ub)x = lsqcurvefit(fun,xO,xdata,ydata,lb,ub,options)x = lsqcurvefit(problem)x,resnorm = lsqcurvefit(.)x,resnorm,residual = lsqcurvefit(.)x,resnorm,residual,exit

8、flag = lsqcurvefit(.)x,resnorm,residual,exitflag,output = lsqcurvefit(.) x,resnorm,residual,exitflag,output,lambda = lsqcurvefit(.) x,resnorm,residual,exitflag,output,lambda,jacobian= x0为初始解向量;xdata,ydata为满足关系ydata=F(x, xdata)的数据; lb、ub为解向量的下界和上界,若没有指定界,则lb= ,ub=; options为指定的优化参数; fun 为拟合函数,其定义方式为:x

9、 = lsqcurvefit(myfun,xO,xdata,ydata), 其中 myfun 已定义为 function F = myfun(x,xdata)F =%计算x处拟合函数值fun的用法与前面相同; resnorm=sum (fun(x,xdata)-ydata).人2),即在 x 处残差的平方和; residual=fun(x,xdata)-ydata,即在 x 处的残差; exitflag为终止迭代的条件; output为输出的优化信息; lambda为解x处的Lagrange乘子; jacobian为解x处拟合函数fun的jacobian矩阵。例:lsqcurvefit()优化

10、程序Data =.0.00005.89550.10003.56390.20002.51730.30001.97900.40001.89900.50001.39380.60001.13590.70001.00960.80001.03430.90000.84351.00000.68561.10000.61001.20000.53921.30000.39461.40000.39031.50000.54741.60000.34591.70000.13701.80000.22111.90000.17042.00000.2636;t = Data(:,1);y = Data(:,2);% axis(0 2

11、 -0.5 6)plot(t,y,ro)title(Data points)%We would like to fit the function y = c(l)*exp(-lam(l)*t) + c(2)*exp(-lam(2)*t) to the data %The lsqcurvefit function solves this type of problem easily.%To begin, define the parameters in terms of one variable x:%x(1) = c(1)%x(2) = lam(1)%x(3) = c(2)%x(4) = la

12、m(2)%Then define the curve as a function of the parameters x and the data t:F = (x,xdata)x(1)*exp(-x(2)*xdata) + x(3)*exp(-x(4)*xdata);x0 = 1 1 1 0;x,resnorm,exitflag,output = lsqcurvefit(F,xO,t,y)hold onplot(t,F(x,t)hold offFsumsquares = (x)sum(F(x,t) - y).A2);opts = optimset(LargeScale,off);xunc,r

13、essquared,eflag,outputu=.fminunc(Fsumsquares,xO,opts)fprintf( There were %d iterations using fminunc, .and %d using lsqcurvefit.n, .outputu.iterations,output.iterations)fprintf( There were %d function evaluations using fminunc, .and %d using lsqcurvefit., .outputu.funcCount,output.funcCount)type fit

14、vectorx02 = 1 0;F2 = (x,t) fitvector(x,t,y);x2,resnorm2,exitflag2,output2 = lsqcurvefit(F2,xO2,t,y) fprintf( There were %d function evaluations using the 2-d .formulation, and %d using the 4-d formulation., . output2.funcCount,output.funcCount)xObad = 5 1 1 0;xbad,resnormbad,exitflagbad,outputbad=.lsqcurvefit(F,xObad,t,y)hold onplot(t,F(xbad,t),g) legend(Data,Global fit,Bad local fit,Location,NE) hold offfprintf( The residual norm at the good ending point is %f, . and the residual norm at the bad ending point is %f., . resnorm,resnormbad)displayEndOfDemoMessage(mfilename)拟合效果如下:小二乘拟合:

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 学术论文 > 其它学术论文

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号