Matlab直线拟合和平面拟合(共6页)_第1页
Matlab直线拟合和平面拟合(共6页)_第2页
Matlab直线拟合和平面拟合(共6页)_第3页
Matlab直线拟合和平面拟合(共6页)_第4页
Matlab直线拟合和平面拟合(共6页)_第5页
已阅读5页,还剩1页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、精选优质文档-倾情为你奉上利用Matlab实现直线和平面的拟合 2011-04-14 10:45:43| 分类: 算法思想 |举报|字号 订阅直线和平面拟合是很常用的两个算法,原理非常简单。但如果matlab不太熟的话,写起来也不是那么容易。搜了很久才找到这两个代码,保存之,免得日后麻烦。1、直线拟合的matlab代码% Fitting a best-fit line to data, both noisy and non-noisyx = rand(1,10);n = rand(size(x); % Noisey = 2*x + 3; % x and y satisfy y = 2*x +

2、3yn = y + n; % x and yn roughly satisfy yn = 2*x + 3 due to the noise% Determine coefficients for non-noisy line y=m1*x+b1Xcolv = x(:); % Make X a column vectorYcolv = y(:); % Make Y a column vectorConst = ones(size(Xcolv); % Vector of ones for constant termCoeffs = Xcolv ConstYcolv; % Find the coef

3、ficientsm1 = Coeffs(1);b1 = Coeffs(2);% To fit another function to this data, simply change the first % matrix on the line defining Coeffs% For example, this code would fit a quadratic % y = Coeffs(1)*x2+Coeffs(2)*x+Coeffs(3)% Coeffs = Xcolv.2 Xcolv ConstYcolv; % Note the . before the exponent of th

4、e first term% Plot the original points and the fitted curvefigureplot(x,y,'ro')hold onx2 = 0:0.01:1;y2 = m1*x2+b1; % Evaluate fitted curve at many pointsplot(x2, y2, 'g-')title(sprintf('Non-noisy data: y=%f*x+%f',m1,b1)% Determine coefficients for noisy line yn=m2*x+b2Xcolv =

5、 x(:); % Make X a column vectorYncolv = yn(:); % Make Yn a column vectorConst = ones(size(Xcolv); % Vector of ones for constant termNoisyCoeffs = Xcolv ConstYncolv; % Find the coefficientsm2 = NoisyCoeffs(1);b2 = NoisyCoeffs(2);% Plot the original points and the fitted curvefigureplot(x,yn,'ro&#

6、39;)hold onx2 = 0:0.01:1;yn2 = m2*x2+b2;plot(x2, yn2, 'g-')title(sprintf('Noisy data: y=%f*x+%f',m2,b2)2、平面拟合matlab代码x = rand(1,10);y = rand(1,10);z = (3-2*x-5*y)/4; % Equation of the plane containing % (x,y,z) points is 2*x+5*y+4*z=3Xcolv = x(:); % Make X a column vectorYcolv = y(:)

7、; % Make Y a column vectorZcolv = z(:); % Make Z a column vectorConst = ones(size(Xcolv); % Vector of ones for constant termCoefficients = Xcolv Ycolv ConstZcolv; % Find the coefficientsXCoeff = Coefficients(1); % X coefficientYCoeff = Coefficients(2); % X coefficientCCoeff = Coefficients(3); % cons

8、tant term% Using the above variables, z = XCoeff * x + YCoeff * y + CCoeffL=plot3(x,y,z,'ro'); % Plot the original data pointsset(L,'Markersize',2*get(L,'Markersize') % Making the circle markers largerset(L,'Markerfacecolor','r') % Filling in the markershold o

9、nxx, yy=meshgrid(0:0.1:1,0:0.1:1); % Generating a regular grid for plottingzz = XCoeff * xx + YCoeff * yy + CCoeff;surf(xx,yy,zz) % Plotting the surfacetitle(sprintf('Plotting plane z=(%f)*x+(%f)*y+(%f)',XCoeff, YCoeff, CCoeff)% By rotating the surface, you can see that the points lie on the

10、 plane% Also, if you multiply both sides of the equation in the title by 4,% you get the equation in the comment on the third line of this example如何用matlab最小二乘法进行平面拟合MATLAB软件提供了基本的曲线拟合函数的命令:多项式函数拟合: a = polyfit(xdata,ydata,n)其中n表示多项式的最高阶数,xdata,ydata 为要拟合的数据,它是用数组的方式输入。输出参数a为拟合多项式y = a1xn + + anx +

11、an+1的系数a = a1, , an, an+1。多项式在x处的值y可用下面程序计算。y = polyval (a, x)一般的曲线拟合: p = curvefit(Funp0,xdata,ydata)其中Fun表示函数Fun (p, xdata)的M-文件,p0表示函数的初值。curvefit命令的求解问题形式是:minp sum (Fun (p, xdata)-ydata).2若要求解点x处的函数值可用程序f = Fun(p, x) 计算。例如已知函数形式 y = ae - bx + ce dx ,并且已知数据点(xi, yi), i = 1,2, n,要确定四个未知参数a, b, c,

12、 d。使用curvefit命令,数据输入xdata = x1,x2, , xn; ydata = y1,y2, , yn;初值输入p0 = a0,b0,c0,d0; 并且建立函数y = ae - bx + ce dx的M-文件(Fun.m)。若定义p1 = a, p2 = b, p3 = c, p4 = d , 则输出p = p1, p2, p3, p4。引例求解: t=1:16; %数据输入 y=4 6.4 8 8.4 9.28 9.5 9.7 9.86 10 10.2 10.32 10.42 10.5 10.55 10.58 10.6; plot(t,y,'o') %画散点

13、图 p=polyfit(t,y,2) (二次多项式拟合)计算结果: p = -0.0445 1.0711 4.3252 %二次多项式的系数从而得到某化合物的浓度y与时间t的拟合函数:y = 4.3252+1.0711t 0.0445t2对函数的精度如何检测呢?仍然以图形来检测,将散点与拟合曲线画在一个画面上。 xi=linspace(0,16,160); yi=polyval(p,xi); plot(x,y,'o',xi,yi) 在MATLAB的NAG Foundation Toolbox中也有一些曲面拟合函数,如e02daf,e02cf,e02def可分别求出矩形网格点数据、

14、散点数据的最小平方误差双三次样条曲面拟合,e02def等可求出曲面拟合的函数值。用matlab的regress命令进行平面拟合(2011-08-16 22:00:38)标签: 分类: 以少量数据为例x = 1 5 6 3 7'y = 2 9 3 5 8'z = 4 3 5 11 6'scatter3(x,y,z,'filled')hold on即可将散点绘制出来我们继续 X = ones(5,1) x y; /5为size(x) b = regress(z,X) /拟合,其实是线性回归,但可以用来拟合平面。regress命令还有其它用

15、法,但一般这样就可以满足要求了。 于是显示出 b =    6.5642   -0.1269   -0.0381这就表示 z = 6.5643 - 0.1269 * x - 0.0381 * y 是拟合出来的平面的方程下面把它绘制出来xfit = min(x):0.1:max(x);  /注 0.1表示数据的间隔yfit = min(y):0.1:max(y);XFIT,YFIT= meshgrid (xfit,yfit); /制成网格数据ZFIT = b(1) + b(2) * XFIT + b(3) * YFIT;mesh (XFIT,YFIT,ZFIT)这样,图就出来啦%r p q就是你的x,y,zr = randi(10,20,1);p = randi(10,20,1);q = randi(10,20,1);b =

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论