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. %
Stephen J.Chapman《MATLAB 编程(第二版) 》第一章_工学_高等教育_教育专区。Stephen J.Chapman《MATLAB 编程(第二版) 》郑碧波翻译 Matlab 中文论坛首发 http:...
Stephen J.Chapman《MATLAB 编程(第二版) 》第二章Stephen J.Chapman《MATLAB 编程(第二版) 》第二章隐藏>> 郑碧波翻译 Matlab 中文论坛首发 http://www.iLo...
Stephen J.Chapman《MATLAB 编程(第二版) 》第三章Stephen J.Chapman《MATLAB 编程(第二版) 》第三章隐藏>> 郑碧波翻译 Matlab 中文论坛首发 http://www.iLove...
Stephen J.Chapman《MATLAB 编程(第二版) 》第六章Stephen J.Chapman《MATLAB 编程(第二版) 》第六章隐藏>> 郑碧波翻译 Matlab 中文论坛首发 http://www.iLo...
Matlab编程 第四版(Stephen J.Chapman)Quiz部分答案_计算机软件及应用_IT/计算机_专业资料。Matlab编程 第四版 (Stephen J.ChapmanQuiz答案PD...
郑碧波翻译 Matlab 中文论坛首发 http://www.iLoveMatlab.cn 前言 本文为 Stephen J.Chapman《MATLAB 编程(第二版) 》英文影印版的中文译本,这本 书对初学者很...
Chapman 著 邢树军 郑碧波 译 前言郑碧波本文为 Stephen J.Chapman《MATLAB 编程(第二版) 》英文影印版的中文译本,这本 书对初学者很好的入门教材。之前从网...
matlab编程英文版(Stephen J. Chapman )课后答案1_工学_高等教育_教育专区。matlab编程第二版Stephen J. ChapmanMATLAB MATLAB Programming for Enginees Enginees 姓名...
S.J.Chapman《MATLAB编程》中文版修正版_部分7_信息与通信_工程科技_专业资料。...>> cellstring{1} = 'Stephen J. Chapman'; >> cellstring{2} = 'Male'...
S.J.Chapman《MATLAB编程》中文版修正版_部分3_信息与通信_工程科技_专业资料。...Stephen J.Chapman《MAT... 39页 免费 Stephen J.Chapman《MAT... 37页 免...
热门文档