曲线拟合的最小二乘法实验

上传人:j****9 文档编号:45500721 上传时间:2018-06-17 格式:DOC 页数:9 大小:74.55KB
返回 下载 相关 举报
曲线拟合的最小二乘法实验_第1页
第1页 / 共9页
曲线拟合的最小二乘法实验_第2页
第2页 / 共9页
曲线拟合的最小二乘法实验_第3页
第3页 / 共9页
曲线拟合的最小二乘法实验_第4页
第4页 / 共9页
曲线拟合的最小二乘法实验_第5页
第5页 / 共9页
点击查看更多>>
资源描述

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

1、Lab04曲线拟合的最小二乘法实验曲线拟合的最小二乘法实验【实验实验目的和要求目的和要求】1让学生体验曲线拟合的最小二乘法,加深对曲线拟合的最小二乘法的理解;2掌握函数 ployfit 和函数 lsqcurvefit 功能和使用方法,分别用这两个函数进行多项式拟合和非多项式拟合。【实验实验内容内容】1在 Matlab 命令窗口,用 help 命令查询函数 polyfit 和函数 lsqcurvefit 功能和使用方法。 2用多项式y=x3-6x2+5x-3,产生一组数据(xi,yi)(i=1,2,n),再 在yi上添加随机干扰(可用 rand 产生(0,1)均匀分布随机数,或用 randn 产

2、生 N(0,1)均匀分布随机数),然后对xi和添加了随机干扰的 yi用 Matlab 提供的函数 ployfit 用 3 次多项式拟合,将结果与原系数比 较。再作 2 或 4 次多项式拟合,分析所得结果。 3用电压V=10 伏的电池给电容器充电,电容器上t时刻的电压为,其中V0是电容器的初始电压,是充电常数。t eVVVtv)()(0 对于下面的一组t,v数据,用 Matlab 提供的函数 lsqcurvefit 确定V0和 。 t(秒)0.51234579 v(伏)6.366.487.268.228.668.999.439.63【实验仪实验仪器与器与软软件件】1CPU 主频在 1GHz 以上

3、,内存在 128Mb 以上的 PC; 2Matlab 6.0 及以上版本。实验讲评实验讲评:实验实验成成绩绩: 评阅教师:200年 月 日问题及算法分析:问题及算法分析: 1、利用 help 命令,在 MATLAB 中查找 polyfit 和 lsqcurvefit 函数的用法。 2、在一组数据(xi,yi)(i=1,2,n)上,对yi上添加随机干扰,运用多项 式拟合函数,对数据进行拟合(分别用 2 次,3 次,4 次拟合),分析拟合 的效果。 3、根据 t 和 V 的关系画散点图,再根据给定的函数运用最小二乘拟合函数,确 定其相应参数。第一题:(1) help polyfitPOLYFIT

4、Fit polynomial to data.P = POLYFIT(X,Y,N) finds the coefficients of a polynomial P(X) ofdegree N that fits the data Y best in a least-squares sense. P is arow vector of length N+1 containing the polynomial coefficients indescending powers, P(1)*XN + P(2)*X(N-1) +.+ P(N)*X + P(N+1).P,S = POLYFIT(X,Y,

5、N) returns the polynomial coefficients P and astructure S for use with POLYVAL to obtain error estimates forpredictions. S contains fields for the triangular factor (R) from a QRdecomposition of the Vandermonde matrix of X, the degrees of freedom(df), and the norm of the residuals (normr). If the da

6、ta Y are random,an estimate of the covariance matrix of P is (Rinv*Rinv)*normr2/df,where Rinv is the inverse of R.P,S,MU = POLYFIT(X,Y,N) finds the coefficients of a polynomial inXHAT = (X-MU(1)/MU(2) where MU(1) = MEAN(X) and MU(2) = STD(X). Thiscentering and scaling transformation improves the num

7、erical propertiesof both the polynomial and the fitting algorithm.Warning messages result if N is = length(X), if X has repeated, ornearly repeated, points, or if X might need centering and scaling.Class support for inputs X,Y:float: double, singleSee also poly, polyval, roots.Reference page in Help

8、 browserdoc polyfit(2) help lsqcurvefitLSQCURVEFIT solves non-linear least squares problems.LSQCURVEFIT attempts to solve problems of the form:min sum (FUN(X,XDATA)-YDATA).2 where X, XDATA, YDATA and the values X returned by FUN can be vectors or matrices.X=LSQCURVEFIT(FUN,X0,XDATA,YDATA) starts at

9、X0 and finds coefficients Xto best fit the nonlinear functions in FUN to the data YDATA (in the least-squares sense). FUN accepts inputs X and XDATA and returns avector (or matrix) of function values F, where F is the same size asYDATA, evaluated at X and XDATA. NOTE: FUN should return FUN(X,XDATA)a

10、nd not the sum-of-squares sum(FUN(X,XDATA)-YDATA).2).(FUN(X,XDATA)-YDATA) is squared and summed implicitly in thealgorithm.) X=LSQCURVEFIT(FUN,X0,XDATA,YDATA,LB,UB) defines a set of lower andupper bounds on the design variables, X, so that the solution is in therange LB 第二题:三次线性拟合clear allx=0:0.5:5;

11、y=x.3-6*x.2+5*x-3;y1=y;for i=1:length(y)y1(i)=y1(i)+rand;enda=polyfit(x,y1,3);b=polyval(a,x);plot(x,y,*,x,b),aa =1.0121 -6.1033 5.1933 -2.478200.511.522.533.544.55-18-16-14-12-10-8-6-4-20 二次线性拟合clear allx=0:0.5:20;y=x.3-6*x.2+5*x-3;y1=y;for i=1:length(y)y1(i)=y1(i)+rand;enda=polyfit(x,y1,2);b=polyva

12、l(a,x);plot(x,y,*,x,b),a a =23.9982 -232.0179 367.975602468101214161820-10000100020003000400050006000 四次线性拟合clear allx=0:0.5:20;y=x.3-6*x.2+5*x-3;y1=y;for j=1:length(y)y1(j)=y1(j)+rand;enda=polyfit(x,y1,4);b=polyval(a,x);plot(x,y,*,x,b),aa =-0.0001 1.0038 -6.0561 5.2890 -2.824902468101214161820-1000

13、0100020003000400050006000第三题:拟合曲线为:f(x)= 3* 112()axaaa e定义函数:定义函数: function f=fun(a,x)f=a(1)-(a(1)-a(2)*exp(-a(3)*x);主程序:主程序: clear allclcx=0.5 1 2 3 4 5 7 9;y=6.36 6.48 7.26 8.22 8.66 8.99 9.43 9.63;a0=1 1 1;a=lsqcurvefit(fun,a0,x,y);y1=a(1)-(a(1)-a(2)*exp(-a(3)*x);plot(x,y,r*,x,y1,b)V1=a(2)tei=1/a(3)Optimization terminated: relative function valuechanging by less than OPTIONS.TolFun.V1 =5.5921tei =3.6856 如图所示:012345678966.577.588.599.510总结:通过本次试验,学习到了曲线拟合的方法,知道了线性拟合的次数越多,则拟合函数就越接近原来的函数,但是你和次数也不能太高。因为这要有一定的需求,所以在进行拟合时。根据误差的需求来选择最佳的拟合次数,从而达到最佳效果。

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 中学教育 > 初中教育

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