Java学习者论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

手机号码,快捷登录

恭喜Java学习者论坛(https://www.javaxxz.com)已经为数万Java学习者服务超过8年了!积累会员资料超过10000G+
成为本站VIP会员,下载本站10000G+会员资源,购买链接:点击进入购买VIP会员
JAVA高级面试进阶视频教程Java架构师系统进阶VIP课程

分布式高可用全栈开发微服务教程

Go语言视频零基础入门到精通

Java架构师3期(课件+源码)

Java开发全终端实战租房项目视频教程

SpringBoot2.X入门到高级使用教程

大数据培训第六期全套视频教程

深度学习(CNN RNN GAN)算法原理

Java亿级流量电商系统视频教程

互联网架构师视频教程

年薪50万Spark2.0从入门到精通

年薪50万!人工智能学习路线教程

年薪50万!大数据从入门到精通学习路线年薪50万!机器学习入门到精通视频教程
仿小米商城类app和小程序视频教程深度学习数据分析基础到实战最新黑马javaEE2.1就业课程从 0到JVM实战高手教程 MySQL入门到精通教程
查看: 2166|回复: 0

[默认分类] Matlab 线性拟合 & 非线性拟合

[复制链接]
  • TA的每日心情
    开心
    2021-12-13 21:45
  • 签到天数: 15 天

    [LV.4]偶尔看看III

    发表于 2018-7-7 11:52:59 | 显示全部楼层 |阅读模式

    使用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,...)
      
      
    /***********************************线性拟合***********************************/
    线性拟合公式:
      
    1. 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. p =
    6.      Linear model Poly1:
    7.      p(p1,p2,x) = p1*x + p2
    8. f =
    9.      Linear model Poly1:
    10.      f(x) = p1*x + p2
    11.      Coefficients (with 95% confidence bounds):
    12.        p1 =        1.02  (0.7192, 1.321)
    13.        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. p =
    7.      Linear model:
    8.      p(a,b,c,x) = a*x + b*sin(x) + c
    9. f =
    10.      Linear model:
    11.      f(x) = a*x + b*sin(x) + c
    12.      Coefficients (with 95% confidence bounds):
    13.        a =       1.249  (0.9856, 1.512)
    14.        b =      0.6357  (0.03185, 1.24)
    15.        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. p =
    6.      General model:
    7.      p(a,b,c,x) = a*x+b*sin(x)+c
    8. Warning: Start point not provided, choosing random start
    9. point.
    10. > In fit>iCreateWarningFunction/nThrowWarning at 738
    11.   In fit>iFit at 320
    12.   In fit at 109
    13. f =
    14.      General 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)
    复制代码
      

      
      
      


    /***********************************非线性拟合***********************************/
    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.      General model:
    3.      p(a,b,c,x) = a*x.^2+b*x+c
    4. Warning: Start point not provided, choosing random start
    5. point.
    6. > In fit>iCreateWarningFunction/nThrowWarning at 738
    7.   In fit>iFit at 320
    8.   In fit at 109
    9. f =
    10.      General model:
    11.      f(x) = a*x.^2+b*x+c
    12.      Coefficients (with 95% confidence bounds):
    13.        a =     -0.2571  (-0.5681, 0.05386)
    14.        b =       2.049  (0.791, 3.306)
    15.        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. %show results
    14. scatter(x,y);%scatter point
    15. c1=plot(f1,"b:*");%blue
    16. hold on
    17. plot(f2,"g:+");%green
    18. hold on
    19. plot(f3,"m:*");%purple
    20. hold off
    复制代码

      



    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    QQ|手机版|Java学习者论坛 ( 声明:本站资料整理自互联网,用于Java学习者交流学习使用,对资料版权不负任何法律责任,若有侵权请及时联系客服屏蔽删除 )

    GMT+8, 2024-4-24 18:53 , Processed in 0.376860 second(s), 37 queries .

    Powered by Discuz! X3.4

    © 2001-2017 Comsenz Inc.

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