文档下载 文档下载导航
设为首页 | 加入收藏
搜索 请输入内容:  
 导航当前位置: 文档下载 > 所有分类 > 高等教育 > 工学 > Stephen J.Chapman《MATLAB 编程(第二版) 》第四章
侵权投诉

Stephen J.Chapman《MATLAB 编程(第二版) 》第四章

Stephen J.Chapman《MATLAB 编程(第二版) 》第四章

已知有一系列含有噪声的数据(x,y)用最小二乘法计算m和b。数据要求从键盘输入,画出每一个数据点还有画出最适合的直线。

2. 定义输入输出值

这个程序所需的输入值为点的个数,以及点的坐标。输出是用最小二乘法得到的斜率以及y上的截距。

3. 设计算法

这个问题被分解为6个大步骤: Get the number of input data points Read the input data values Calculate the required statistics Calculate the slop and intercept Write out the slop and intercept

Plot the input points and the fitted line

第一大步是读取输入量的个数,所以我们要用到input函数。下一步我们要在for循环中使用input函数读取输入量(x,y)。每一个输入值将会产生一个数组([x,y]),然后这个函数将会返回这个数组到调用程序。注意在这里应用for循环是合适的,因为我们事先知道循环要执行多少次。

上述步骤的伪代码如下:

Print message describing purpose of the program n_points ← input('Enter number of [x y] pairs:'); for ii = 1:n_points

temp ← ('Enter [x y] pairs:'); x(ii) ← temp(1); y(ii) ← temp(2); end

下一步,我们必须计算出相关的统计量。这些统计量是∑x,∑y,∑xy,∑x。伪代码如下

Clear the vairables sum_x, sum_y, sum_x2, and sum_xy for ii = 1:n_points

sum_x ← sum_x + x(ii) sum_y ← sum_y + y(ii) sum_x2 ← sum_x2 + x(ii)^2 sum_xy ← sum_xy + x(ii) * y(ii) end

下一步我们必须计算出斜率m和y轴上的载距b。这一步的伪代码就是公式(4.4)和(4.5)。

x_bar ← sum_x / n_points y_bar ← sum_y / n_points

slope ← (sum_xy-sum_x * y_bar)/( sum_x2 –sum_x * x_bar) y_int ← y_bar – slope * x_bar

最后,我们必须写出和画出相应的结果。输入的坐标点要用圆点画出,不用连接线而用最小二乘法得到解析式对应的直线用2pixel的实直线画出。在此之前我们要用到hold on命令。画完直线之后,调用hold off命令。我们在图象中将会添加相应的标题,以及相应的图例。

4. 转化为matlab语句 % Script file: lsqfit.m % Purpose:

% To perform a leastsquares fit of an input data set % to a straight line, and print out the resulting slope % and intercept values. The input data for this fit % comes from a userspecified input data file. %

上一页第20页

你可能喜欢

热门文档

相关文档

站点地图 | 文档上传 | 侵权投诉 | 手机版
本站所有资源均来自互联网,本站只负责收集和整理,均不承担任何法律责任,如有侵权等其它行为请联系我们.
文档下载 Copyright 2013 doc.guandang.net All Rights Reserved.  -  email