Navigation : Top/MATLAB/カーブフィッテング

  • 追加された行はこの色です。
  • 削除された行はこの色です。
*カーブフィッテング

**多項式 

関数 polyfit は、最小二乗的にデータ群に適合する多項式の係数を出力します。 
 p = polyfit(x,y,n) 
x と y は、適合されるデータ群のxデータ、yデータで、n は、出力される多項式の次数です。 

細かい刻みで、polyfit 推定値を計算し、実データ値と比較するために重ねて、プロットします。 
 x2 = 1:.1:5; 
 y2 = polyval(p,x2); 
 plot(x,y,'o',x2,y2) 
 grid on 

----------------

**一般形 

If you know the form of the required function you can use fmins to find the best coefficients. Suppose that you think that some data should conform to a relation of the form a*x+b*sin(x)+c. You need to write a matlab function that for given values of a, b and c, calculates the square of the disparity. Its first argument is a vector of coefficients, its last argument is a vector of the given data values. The other arguments (in this case, just one) are vectors of free variables. You need to write the function this way so that fmins can use it. 

 function out=fun(coeff,X,Y) 
 % fun.m 
 a = coeff(1); 
 b = coeff(2); 
 c = coeff(3); 
 Y_fun = a .* X + b .* sin(X)+c; 
 DIFF = Y_fun - Y; 
 SQ_DIFF = DIFF.^2; 
 
 out = sum(SQ_DIFF); 

If you call this file fun.m, then 
 x=1:10; 
 y=sin(x); 
 bestcoeffs=fmins('fun',[1 1 1],[],[],x,y); 
 yfit=bestcoeffs(1)*x +bestcoeffs(2)*sin(x) + bestcoeffs(3); 
 %Now compare y with yfit 
 plot(x,y,x,yfit); 

will display the results. The [1 1 1] argument to fmins provides the starting values for a, b and c. 

fmins becomes fminsearch since R13. 

Referenece: http://www-h.eng.cam.ac.uk/help/tpl/programs/Matlab/curve_fitting.html