http://phylab.fudan.edu.cn/doku.php?id=howtos:matlab:mt1-5
一、 單一變量的曲線逼近
Matlab有一個功能強大的曲線擬合工具箱 cftool ,使用方便,能實現多種類型的線性、非線
性曲線擬合。下面結合我使用的 Matlab R2007b 來簡單介紹如何使用這個工具箱。
假設我們要擬合的函數形式是 y=A*x*x + B*x, 且A>0,B>0 。
1、在命令行輸入數據:
》x=[110.3323 148.7328 178.064 202.8258033 224.7105 244.5711 262.908 280.0447
296.204 311.5475]
》y=[5 10 15 20 25 30 35 40 45 50]
2、啟動曲線擬合工具箱
》cftool
3、進入曲線擬合工具箱界面“Curve Fitting tool”
(1)點擊“Data”按鈕,彈出“Data”窗口;
(2)利用X data和Y data的下拉菜單讀入數據x,y,可修改數據集名“Data set name”,然
后點擊“Create data set”按鈕,退出“Data”窗口,返回工具箱界面,這時會自動畫出數
據集的曲線圖;
(3)點擊“Fitting”按鈕,彈出“Fitting”窗口;
(4)點擊“New fit”按鈕,可修改擬合項目名稱“Fit name”,通過“Data set”下拉菜單
選擇數據集,然后通過下拉菜單“Type of fit”選擇擬合曲線的類型,工具箱提供的擬合類
型有:
Custom Equations:用戶自定義的函數類型
Exponential:指數逼近,有2種類型, a*exp(b*x) 、 a*exp(b*x) + c*exp(d*x)
Fourier:傅立葉逼近,有7種類型,基礎型是 a0 + a1*cos(x*w) + b1*sin(x*w)
Gaussian:高斯逼近,有8種類型,基礎型是 a1*exp(-((x-b1)/c1)^2)
Interpolant:插值逼近,有4種類型,linear、nearest neighbor、cubic spline、shape-
preserving
Polynomial:多形式逼近,有9種類型,linear ~、quadratic ~、cubic ~、4-9th degree ~
Power:冪逼近,有2種類型,a*x^b 、a*x^b + c
Rational:有理數逼近,分子、分母共有的類型是linear ~、quadratic ~、cubic ~、4-5th
degree ~;此外,分子還包括constant型
Smoothing Spline:平滑逼近(翻譯的不大恰當,不好意思)
Sum of Sin Functions:正弦曲線逼近,有8種類型,基礎型是 a1*sin(b1*x + c1)
Weibull:只有一種,a*b*x^(b-1)*exp(-a*x^b)
選擇好所需的擬合曲線類型及其子類型,並進行相關設置:
——如果是非自定義的類型,根據實際需要點擊“Fit options”按鈕,設置擬合算法、修改
待估計參數的上下限等參數;
——如果選Custom Equations,點擊“New”按鈕,彈出自定義函數等式窗口,有“Linear
Equations線性等式”和“General Equations構造等式”兩種標簽。
在本例中選Custom Equations,點擊“New”按鈕,選擇“General Equations”標簽,輸入函
數類型y=a*x*x + b*x,設置參數a、b的上下限,然后點擊OK。
(5)類型設置完成后,點擊“Apply”按鈕,就可以在Results框中得到擬合結果,如下例:
general model:
f(x) = a*x*x+b*x
Coefficients (with 95% confidence bounds):
a = 0.009194 (0.009019, 0.00937)
b = 1.78e-011 (fixed at bound)
Goodness of fit:
SSE: 6.146
R-square: 0.997
Adjusted R-square: 0.997
RMSE: 0.8263
同時,也會在工具箱窗口中顯示擬合曲線。
這樣,就完成一次曲線擬合啦,十分方便快捷。當然,如果你覺得擬合效果不好,還可以在“
Fitting”窗口點擊“New fit”按鈕,按照步驟(4)~(5)進行一次新的擬合。
不過,需要注意的是,cftool 工具箱只能進行單個變量的曲線擬合,即待擬合的公式中,變
量只能有一個。對於混合型的曲線,例如 y = a*x + b/x ,工具箱的擬合效果並不好。下一
篇文章我介紹幫同學做的一個非線性函數的曲線擬合。
以上內容出自:
http://hi.baidu.com/snowlxm/blog/item/15a714b122ae95500823025f.html
上邊對cftool工具箱做了很詳盡的說明,但並沒有對各種曲線擬合的性能做點評,在單變量曲線擬合中,如何選取一種最優化的擬合方式是非常重要的,我們在采用CFTOOL擬合后,會有一些性能說明,如:
Goodness of fit:
SSE: 6.146
R-square: 0.997
Adjusted R-square: 0.997
RMSE: 0.8263
官方的解釋:
Results -- Displays detailed results for the current fit including the fit type (model, spline, or interpolant), the fitted coefficients and 95% confidence bounds for parametric fits, and these goodness of fit statistics:
SSE -- The sum of squares due to error. This statistic measures the deviation of the responses from the fitted values of the responses. A value closer to 0 indicates a better fit.
R-square -- The coefficient of multiple determination. This statistic measures how successful the fit is in explaining the variation of the data. A value closer to 1 indicates a better fit.
Adjusted R-square -- The degree of freedom adjusted R-square. A value closer to 1 indicates a better fit. It is generally the best indicator of the fit quality when you add additional coefficients to your model.
RMSE -- The root mean squared error. A value closer to 0 indicates a better fit.