QQ登录

只需要一步,快速开始

 注册地址  找回密码
查看: 1754|回复: 3
打印 上一主题 下一主题

优秀例题——用最小二乘法画噪声数据的近似曲线

[复制链接]
字体大小: 正常 放大

413

主题

36

听众

1854

积分

升级  85.4%

  • TA的每日心情
    开心
    2019-9-18 21:55
  • 签到天数: 258 天

    [LV.8]以坛为家I

    社区QQ达人

    群组2015国赛冲刺

    群组2016美赛公益课程

    群组国赛讨论

    群组第三届数模基础实训

    群组Matlab讨论组

    跳转到指定楼层
    1#
    发表于 2015-9-8 22:35 |只看该作者 |倒序浏览
    |招呼Ta 关注Ta
    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, 下载次数: 204)

    结果1.PNG

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

    结果2.PNG

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

    例题4-7.PNG

    例题.doc

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

    详细内容

    zan
    已有 1 人评分体力 收起 理由
    jt202010 + 2

    总评分: 体力 + 2   查看全部评分

    转播转播0 分享淘帖0 分享分享0 收藏收藏0 支持支持0 反对反对0 微信微信
    数学中国版主团队!

    413

    主题

    36

    听众

    1854

    积分

    升级  85.4%

  • TA的每日心情
    开心
    2019-9-18 21:55
  • 签到天数: 258 天

    [LV.8]以坛为家I

    社区QQ达人

    群组2015国赛冲刺

    群组2016美赛公益课程

    群组国赛讨论

    群组第三届数模基础实训

    群组Matlab讨论组

    回复

    使用道具 举报

    瞥神        

    1

    主题

    10

    听众

    64

    积分

    升级  62.11%

  • TA的每日心情
    开心
    2015-9-11 07:28
  • 签到天数: 8 天

    [LV.3]偶尔看看II

    社区QQ达人

    群组数学建摸协会

    群组数学建模培训课堂1

    回复

    使用道具 举报

    614492427        

    2

    主题

    1

    听众

    70

    积分

    升级  68.42%

  • TA的每日心情
    开心
    2018-2-12 15:08
  • 签到天数: 12 天

    [LV.3]偶尔看看II

    自我介绍
    努力
    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 注册地址

    qq
    收缩
    • 电话咨询

    • 04714969085
    fastpost

    关于我们| 联系我们| 诚征英才| 对外合作| 产品服务| QQ

    手机版|Archiver| |繁體中文 手机客户端  

    蒙公网安备 15010502000194号

    Powered by Discuz! X2.5   © 2001-2013 数学建模网-数学中国 ( 蒙ICP备14002410号-3 蒙BBS备-0002号 )     论坛法律顾问:王兆丰

    GMT+8, 2024-6-17 22:43 , Processed in 2.767357 second(s), 72 queries .

    回顶部