Matlab 线性拟合 & 非线性拟合


使用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. p =   
  7.   
  8.      Linear model Poly1:  
  9.      p(p1,p2,x) = p1*x + p2  
  10.   
  11. f =   
  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
  • 2
  • 下一页

相关内容