声振论坛

 找回密码
 我要加入

QQ登录

只需一步,快速开始

查看: 1805|回复: 0

[编程技巧] Matlab 线性拟合 & 非线性拟合

[复制链接]
发表于 2016-5-4 10:13 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?我要加入

x
fittype
Fit type for curve and surface fitting
Syntax
  1. <font color="#000000">ffun = fittype(libname)
  2. ffun = fittype(expr)
  3. ffun = fittype({expr1,...,exprn})
  4. ffun = fittype(expr, Name, Value,...)
  5. ffun= fittype({expr1,...,exprn}, Name, Value,...)</font>
复制代码
线性拟合公式:
coeff1 * term1 + coeff2 * term2 + coeff3 * term3 + ...其中,coefficient是系数,term都是x的一次项。
线性拟合Example:
Example1: y=kx+b;
法1:
  1. <font color="#000000">[csharp] view plaincopyprint?
  2. x=[1,1.5,2,2.5,3];y=[0.9,1.7,2.2,2.6,3];  
  3. p=polyfit(x,y,1);  
  4. x1=linspace(min(x),max(x));  
  5. y1=polyval(p,x1);  
  6. plot(x,y,'*',x1,y1); </font>
复制代码

结果:p =    1.0200    0.0400
即y=1.0200 *x+ 0.0400
6.jpg
  1. <font color="#000000">法2:
  2. [csharp] view plaincopyprint?
  3. x=[1;1.5;2;2.5;3];y=[0.9;1.7;2.2;2.6;3];  
  4. p=fittype('poly1')  
  5. f=fit(x,y,p)  
  6. plot(f,x,y);  
  7. 运行结果:
  8. [csharp] view plaincopyprint?
  9. x=[1;1.5;2;2.5;3];y=[0.9;1.7;2.2;2.6;3];  
  10. p=fittype('poly1')  
  11. f=fit(x,y,p)  
  12. plot(f,x,y);  
  13.   
  14. p =   
  15.   
  16.      Linear model Poly1:  
  17.      p(p1,p2,x) = p1*x + p2  
  18.   
  19. f =   
  20.   
  21.      Linear model Poly1:  
  22.      f(x) = p1*x + p2  
  23.      Coefficients (with 95% confidence bounds):  
  24.        p1 =        1.02  (0.7192, 1.321)  
  25.        p2 =        0.04  (-0.5981, 0.6781)  </font>
复制代码
7.jpg
  1. <font color="#000000">Example2:y=a*x + b*sin(x) + c
  2. 法1:
  3. [csharp] view plaincopyprint?
  4. x=[1;1.5;2;2.5;3];y=[0.9;1.7;2.2;2.6;3];  
  5. EXPR = {'x','sin(x)','1'};  
  6. p=fittype(EXPR)  
  7. f=fit(x,y,p)  
  8. plot(f,x,y);  
  9. 运行结果:
  10. [csharp] view plaincopyprint?
  11. x=[1;1.5;2;2.5;3];y=[0.9;1.7;2.2;2.6;3];  
  12. EXPR = {'x','sin(x)','1'};  
  13. p=fittype(EXPR)  
  14. f=fit(x,y,p)  
  15. plot(f,x,y);  
  16.   
  17. p =   
  18.   
  19.      Linear model:  
  20.      p(a,b,c,x) = a*x + b*sin(x) + c  
  21.   
  22. f =   
  23.   
  24.      Linear model:  
  25.      f(x) = a*x + b*sin(x) + c  
  26.      Coefficients (with 95% confidence bounds):  
  27.        a =       1.249  (0.9856, 1.512)  
  28.        b =      0.6357  (0.03185, 1.24)  
  29.        c =     -0.8611  (-1.773, 0.05094)  
  30. 法2:
  31. [csharp] view plaincopyprint?
  32. x=[1;1.5;2;2.5;3];y=[0.9;1.7;2.2;2.6;3];  
  33. p=fittype('a*x+b*sin(x)+c','independent','x')  
  34. f=fit(x,y,p)  
  35. plot(f,x,y);  
  36. 运行结果:
  37. [csharp] view plaincopyprint?
  38. x=[1;1.5;2;2.5;3];y=[0.9;1.7;2.2;2.6;3];  
  39. p=fittype('a*x+b*sin(x)+c','independent','x')  
  40. f=fit(x,y,p)  
  41. plot(f,x,y);  
  42.   
  43. p =   
  44.   
  45.      General model:  
  46.      p(a,b,c,x) = a*x+b*sin(x)+c  
  47. Warning: Start point not provided, choosing random start  
  48. point.   
  49. > In fit>iCreateWarningFunction/nThrowWarning at 738  
  50.   In fit>iFit at 320  
  51.   In fit at 109   
  52.   
  53. f =   
  54.   
  55.      General model:  
  56.      f(x) = a*x+b*sin(x)+c  
  57.      Coefficients (with 95% confidence bounds):  
  58.        a =       1.249  (0.9856, 1.512)  
  59.        b =      0.6357  (0.03185, 1.24)  
  60.        c =     -0.8611  (-1.773, 0.05094)  </font>
复制代码
8.jpg
  1. <font color="#000000">Example:y=a*x^2+b*x+c
  2. 法1:
  3. [cpp] view plaincopyprint?
  4. x=[1;1.5;2;2.5;3];y=[0.9;1.7;2.2;2.6;3];  
  5. p=fittype('a*x.^2+b*x+c','independent','x')  
  6. f=fit(x,y,p)  
  7. plot(f,x,y);  

  8. 运行结果:
  9. [csharp] view plaincopyprint?
  10. p =   
  11.   
  12.      General model:  
  13.      p(a,b,c,x) = a*x.^2+b*x+c  
  14. Warning: Start point not provided, choosing random start  
  15. point.   
  16. > In fit>iCreateWarningFunction/nThrowWarning at 738  
  17.   In fit>iFit at 320  
  18.   In fit at 109   
  19.   
  20. f =   
  21.   
  22.      General model:  
  23.      f(x) = a*x.^2+b*x+c  
  24.      Coefficients (with 95% confidence bounds):  
  25.        a =     -0.2571  (-0.5681, 0.05386)  
  26.        b =       2.049  (0.791, 3.306)  
  27.        c =       -0.86  (-2.016, 0.2964)  </font>
复制代码
9.jpg
  1. <font color="#000000">法2:
  2. [csharp] view plaincopyprint?
  3. x=[1;1.5;2;2.5;3];y=[0.9;1.7;2.2;2.6;3];  
  4. %use c=0;  
  5. c=0;  
  6. p1=fittype(@(a,b,x) a*x.^2+b*x+c)  
  7. f1=fit(x,y,p1)  
  8. %use c=1;  
  9. c=1;  
  10. p2=fittype(@(a,b,x) a*x.^2+b*x+c)  
  11. f2=fit(x,y,p2)  
  12. %predict c  
  13. p3=fittype(@(a,b,c,x) a*x.^2+b*x+c)  
  14. f3=fit(x,y,p3)  
  15.   
  16. %show results  
  17. scatter(x,y);%scatter point  
  18. c1=plot(f1,'b:*');%blue  
  19. hold on  
  20. plot(f2,'g:+');%green  
  21. hold on  
  22. plot(f3,'m:*');%purple  
  23. hold off  </font>
复制代码
10.jpg


回复
分享到:

使用道具 举报

您需要登录后才可以回帖 登录 | 我要加入

本版积分规则

QQ|小黑屋|Archiver|手机版|联系我们|声振论坛

GMT+8, 2024-5-4 13:01 , Processed in 0.291514 second(s), 21 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表