Matlab直线拟合和平面拟合

上传人:206****923 文档编号:37523302 上传时间:2018-04-17 格式:DOC 页数:6 大小:79KB
返回 下载 相关 举报
Matlab直线拟合和平面拟合_第1页
第1页 / 共6页
Matlab直线拟合和平面拟合_第2页
第2页 / 共6页
Matlab直线拟合和平面拟合_第3页
第3页 / 共6页
Matlab直线拟合和平面拟合_第4页
第4页 / 共6页
Matlab直线拟合和平面拟合_第5页
第5页 / 共6页
点击查看更多>>
资源描述

《Matlab直线拟合和平面拟合》由会员分享,可在线阅读,更多相关《Matlab直线拟合和平面拟合(6页珍藏版)》请在金锄头文库上搜索。

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

2、 y = 2*x + 3 yn = y + n; % x and yn roughly satisfy yn = 2*x + 3 due to the noise % Determine coefficients for non-noisy line y=m1*x+b1 Xcolv = x(:); % Make X a column vector Ycolv = y(:); % Make Y a column vector Const = ones(size(Xcolv); % Vector of ones for constant term Coeffs = Xcolv ConstYcolv

3、; % Find the coefficients m1 = 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 . bef

4、ore the exponent of the first term % Plot the original points and the fitted curve figure plot(x,y,ro) hold on x2 = 0:0.01:1; y2 = m1*x2+b1; % Evaluate fitted curve at many points plot(x2, y2, g-) title(sprintf(Non-noisy data: y=%f*x+%f,m1,b1) % Determine coefficients for noisy line yn=m2*x+b2 Xcolv

5、 = x(:); % Make X a column vector Yncolv = yn(:); % Make Yn a column vector Const = ones(size(Xcolv); % Vector of ones for constant term NoisyCoeffs = Xcolv ConstYncolv; % Find the coefficients m2 = NoisyCoeffs(1); b2 = NoisyCoeffs(2); % Plot the original points and the fitted curve figureplot(x,yn,

6、ro) hold on x2 = 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=3 Xcolv = x(:); % Make X a column vector Ycolv = y(:); % Make Y

7、 a column vector Zcolv = z(:); % Make Z a column vector Const = ones(size(Xcolv); % Vector of ones for constant term Coefficients = Xcolv Ycolv ConstZcolv; % Find the coefficients XCoeff = Coefficients(1); % X coefficient YCoeff = Coefficients(2); % X coefficient CCoeff = Coefficients(3); % constant

8、 term % Using the above variables, z = XCoeff * x + YCoeff * y + CCoeff L=plot3(x,y,z,ro); % Plot the original data points set(L,Markersize,2*get(L,Markersize) % Making the circle markers larger set(L,Markerfacecolor,r) % Filling in the markers hold on xx, yy=meshgrid(0:0.1:1,0:0.1:1); % Generating

9、a regular grid for plotting zz = XCoeff * xx + YCoeff * yy + CCoeff; surf(xx,yy,zz) % Plotting the surface title(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 plane % Also, if you multiply both sides of the equat

10、ion 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 + an+1的系数 a = a1, , an, an+1

11、。 多项式在 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, d

12、。 使用 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 = regress(r,p q); scatter3(r,p,q,filled); hold on rfit = min(r):1:max(r); pfit = min(p):1:max(p); RFIT PFIT = meshgrid(rfit,pfit); QFIT = b(1) * RFIT + b(2) * PFIT; mesh(RFIT,PFIT,QFIT); view(60,10);这个程序有问题,只能拟合得到 ZAX+BY,得不到 ZAX+BY+D 的形式

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

最新文档


当前位置:首页 > 行业资料 > 其它行业文档

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