• Matlab 线性拟合 & 非线性拟合


    zz http://blog.csdn.net/abcjennifer/article/details/7684836#1536434-tsina-1-64403-66a1f5d8f89e9ad52626f6f40fdeadaa


    使用Matlab进行拟合是图像处理中线条变换的一个重点内容,本文将详解Matlab中的直线拟合和曲线拟合用法。

    关键函数:

     

    fittype

    Fit type for curve and surface fitting

    Syntax

    ffun = fittype(libname)
    ffun = fittype(expr)
    ffun = fittype({expr1,...,exprn})
    ffun = fittype(expr, Name, Value,...)
    ffun= fittype({expr1,...,exprn}, Name, Value,...)

     

    线性拟合公式:

    coeff1 * term1 + coeff2 * term2 + coeff3 * term3 + ...
    
    其中,coefficient是系数,term都是x的一次项。

    线性拟合Example:

    Example1: y=kx+b;

    法1:

    1. x=[1,1.5,2,2.5,3];y=[0.9,1.7,2.2,2.6,3];  
    2. p=polyfit(x,y,1);  
    3. x1=linspace(min(x),max(x));  
    4. y1=polyval(p,x1);  
    5. plot(x,y,'*',x1,y1);  
    结果:p =    1.0200    0.0400

    即y=1.0200 *x+ 0.0400

    法2:

    1. x=[1;1.5;2;2.5;3];y=[0.9;1.7;2.2;2.6;3];  
    2. p=fittype('poly1' 
    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('poly1' 
    3. f=fit(x,y,p)  
    4. plot(f,x,y);  
    5.   
    6.   
    7.   
    8.      Linear model Poly1:  
    9.      p(p1,p2,x) p1*x p2  
    10.   
    11.   
    12.   
    13.      Linear model Poly1:  
    14.      f(x) p1*x p2  
    15.      Coefficients (with 95% confidence bounds):  
    16.        p1        1.02  (0.7192, 1.321)  
    17.        p2        0.04  (-0.5981, 0.6781)  
    Example2:y=a*x + b*sin(x) + c

    法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.   
    8.   
    9.      Linear model:  
    10.      p(a,b,c,x) a*x b*sin(x)  
    11.   
    12.   
    13.   
    14.      Linear model:  
    15.      f(x) a*x b*sin(x)  
    16.      Coefficients (with 95% confidence bounds):  
    17.              1.249  (0.9856, 1.512)  
    18.             0.6357  (0.03185, 1.24)  
    19.            -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.   
    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.   
    17.   
    18.      General model:  
    19.      f(x) a*x+b*sin(x)+c  
    20.      Coefficients (with 95% confidence bounds):  
    21.              1.249  (0.9856, 1.512)  
    22.             0.6357  (0.03185, 1.24)  
    23.            -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.   
    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.   
    12.   
    13.      General model:  
    14.      f(x) a*x.^2+b*x+c  
    15.      Coefficients (with 95% confidence bounds):  
    16.            -0.2571  (-0.5681, 0.05386)  
    17.              2.049  (0.791, 3.306)  
    18.              -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  
    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  

  • 相关阅读:
    delphiXE7关于android 检测屏幕是否处于关闭状态
    delphiXE7关于android API的使用和检测WIFI状态的问题
    关于Android下Delphi XE7获取通讯录的问题
    多线程操里操作webbrowser的 Frames
    关于游戏引擎
    今天博客开通了
    集合类型-集合
    编程语言
    live Python4笔记
    live Python3笔记
  • 原文地址:https://www.cnblogs.com/gisalameda/p/12840628.html
Copyright © 2020-2023  润新知