手机版
你好,游客 登录 注册
背景:
阅读新闻

Matlab 线性拟合 & 非线性拟合

[日期:2012-07-02] 来源:Linux社区  作者:abcjennifer [字体:大 中 小]

法1:

  1. x=[1;1.5;2;2.5;3];y=[0.9;1.7;2.2;2.6;3];  
  2. EXPR = {'x','sin(x)','1'};  
  3. p=fittype(EXPR)  
  4. f=fit(x,y,p)  
  5. plot(f,x,y);  

运行结果:

  1.  x=[1;1.5;2;2.5;3];y=[0.9;1.7;2.2;2.6;3];  
  2. EXPR = {'x','sin(x)','1'};  
  3. p=fittype(EXPR)  
  4. f=fit(x,y,p)  
  5. plot(f,x,y);  
  6.   
  7. p =   
  8.   
  9.      Linear model:  
  10.      p(a,b,c,x) = a*x + b*sin(x) + c  
  11.   
  12. f =   
  13.   
  14.      Linear model:  
  15.      f(x) = a*x + b*sin(x) + c  
  16.      Coefficients (with 95% confidence bounds):  
  17.        a =       1.249  (0.9856, 1.512)  
  18.        b =      0.6357  (0.03185, 1.24)  
  19.        c =     -0.8611  (-1.773, 0.05094)  

法2:
  1. x=[1;1.5;2;2.5;3];y=[0.9;1.7;2.2;2.6;3];  
  2.  p=fittype('a*x+b*sin(x)+c','independent','x')  
  3. f=fit(x,y,p)  
  4. plot(f,x,y);  

运行结果:

  1. x=[1;1.5;2;2.5;3];y=[0.9;1.7;2.2;2.6;3];  
  2.  p=fittype('a*x+b*sin(x)+c','independent','x')  
  3. f=fit(x,y,p)  
  4. plot(f,x,y);  
  5.   
  6. p =   
  7.   
  8.      General model:  
  9.      p(a,b,c,x) = a*x+b*sin(x)+c  
  10. Warning: Start point not provided, choosing random start  
  11. point.   
  12. > In fit>iCreateWarningFunction/nThrowWarning at 738  
  13.   In fit>iFit at 320  
  14.   In fit at 109   
  15.   
  16. f =   
  17.   
  18.      General model:  
  19.      f(x) = a*x+b*sin(x)+c  
  20.      Coefficients (with 95% confidence bounds):  
  21.        a =       1.249  (0.9856, 1.512)  
  22.        b =      0.6357  (0.03185, 1.24)  
  23.        c =     -0.8611  (-1.773, 0.05094)  


/***********************************非线性拟合***********************************/

Example:y=a*x^2+b*x+c

法1:

  1. x=[1;1.5;2;2.5;3];y=[0.9;1.7;2.2;2.6;3];  
  2.  p=fittype('a*x.^2+b*x+c','independent','x')  
  3. f=fit(x,y,p)  
  4. plot(f,x,y);  
运行结果:
  1. p =   
  2.   
  3.      General model:  
  4.      p(a,b,c,x) = a*x.^2+b*x+c  
  5. Warning: Start point not provided, choosing random start  
  6. point.   
  7. > In fit>iCreateWarningFunction/nThrowWarning at 738  
  8.   In fit>iFit at 320  
  9.   In fit at 109   
  10.   
  11. f =   
  12.   
  13.      General model:  
  14.      f(x) = a*x.^2+b*x+c  
  15.      Coefficients (with 95% confidence bounds):  
  16.        a =     -0.2571  (-0.5681, 0.05386)  
  17.        b =       2.049  (0.791, 3.306)  
  18.        c =       -0.86  (-2.016, 0.2964)  



法2:

  1. x=[1;1.5;2;2.5;3];y=[0.9;1.7;2.2;2.6;3];  
  2. %use c=0;  
  3. c=0;  
  4. p1=fittype(@(a,b,x) a*x.^2+b*x+c)  
  5. f1=fit(x,y,p1)  
  6. %use c=1;  
  7. c=1;  
  8. p2=fittype(@(a,b,x) a*x.^2+b*x+c)  
  9. f2=fit(x,y,p2)  
  10. %predict c  
  11. p3=fittype(@(a,b,c,x) a*x.^2+b*x+c)  
  12. f3=fit(x,y,p3)  
  13.   
  14. %show results  
  15. scatter(x,y);%scatter point  
  16. c1=plot(f1,'b:*');%blue  
  17. hold on  
  18. plot(f2,'g:+');%green  
  19. hold on  
  20. plot(f3,'m:*');%purple  
  21. hold off  

linux
相关资讯       Matlab  Matlab教程 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款