数学建模社区-数学中国

标题: 优秀例题——用最小二乘法画噪声数据的近似曲线 [打印本页]

作者: 森之张卫东    时间: 2015-9-8 22:35
标题: 优秀例题——用最小二乘法画噪声数据的近似曲线
  1. <p>% Script file: lsqfit.m
  2. % Purpose:
  3. % To perform a leastsquares fit of an input data set
  4. % to a straight line, and print out the resulting slope
  5. % and intercept values. The input data for this fit
  6. % comes from a userspecified input data file.
  7. %
  8. % Record of revisions:
  9. % Date Programmer Description of change
  10. % ==== ========== =====================
  11. % 01/03/99 S. J. Chapman Original code
  12. %
  13. % Define variables:
  14. % ii Loop index
  15. % n_points Number in input [x y] points
  16. % slope Slope of the line
  17. % sum_x Sum of all input x values
  18. % sum_x2 Sum of all input x values squared
  19. % sum_xy Sum of all input x*y yalues
  20. % sum_y Sum of all input y values
  21. % temp Variable to read user input
  22. % x Array of x values
  23. % x_bar Average x value
  24. % y Array of y values
  25. % y_bar Average y value
  26. % y_int yaxis intercept of the line
  27. disp('This program performs a leastsquares fit of an ');
  28. disp('input data set to a straight line.');
  29. n_points = input('Enter the number of input [x y] points: ');
  30. % Read the input data
  31. for ii = 1:n_points
  32. temp = input('Enter [x y] pair: ');
  33. x(ii) = temp(1);
  34. y(ii) = temp(2);
  35. end
  36. % Accumulate statistics
  37. sum_x = 0;
  38. sum_y = 0;
  39. sum_x2 = 0;
  40. sum_xy = 0;
  41. for ii = 1:n_points
  42.     sum_x = sum_x + x(ii);
  43.     sum_y = sum_y + y(ii);
  44.     sum_x2 = sum_x2 + x(ii)^2;
  45.     sum_xy = sum_xy + x(ii) * y(ii);
  46. end
  47. % Now calculate the slope and intercept.
  48. x_bar = sum_x / n_points;
  49. y_bar = sum_y / n_points;
  50. slope = (sum_xy - sum_x * y_bar) / ( sum_x2 - sum_x * x_bar);
  51. y_int = y_bar - slope * x_bar;
  52. % Tell user.
  53. disp('Regression coefficients for the leastsquares line:');
  54. fprintf(' Slope (m) = %8.3f\n', slope);
  55. fprintf(' Intercept (b) = %8.3f\n', y_int);
  56. fprintf(' No of points = %8d\n', n_points);
  57. % Plot the data points as blue circles with no
  58. % connecting lines.
  59. plot(x,y,'bo');
  60. hold on;
  61. % Create the fitted line
  62. xmin = min(x);
  63. xmax = max(x);
  64. ymin = slope * xmin + y_int;
  65. ymax = slope * xmax + y_int;
  66. % Plot a solid red line with no markers
  67. plot([xmin xmax],[ymin ymax],'r','LineWidth',2);
  68. hold off;
  69. % Add a title and legend
  70. title ('\bfLeastSquaresFit');
  71. xlabel('\bf\itx');
  72. ylabel('\bf\ity');
  73. legend('Input data','Fitted line');
  74. grid on
  75. </p>
复制代码

结果1.PNG (129.37 KB, 下载次数: 245)

结果1.PNG

结果2.PNG (129.54 KB, 下载次数: 226)

结果2.PNG

例题4-7.PNG (37.14 KB, 下载次数: 248)

例题4-7.PNG

例题.doc

139 KB, 下载次数: 5, 下载积分: 体力 -2 点

详细内容


作者: 森之张卫东    时间: 2015-9-8 22:36
不错的例题,共同学习!!!

作者: 瞥神    时间: 2015-9-9 21:10
非常好!!!!!!!!!!!!!!

作者: 614492427    时间: 2018-2-10 11:26
66666666666666666666





欢迎光临 数学建模社区-数学中国 (http://www.madio.net/) Powered by Discuz! X2.5