※ 引述《metaldr (metaldr)》之銘言:
: 各位大大好 小弟初學matlab
: 但很多東西還搞不清楚 老師的作業也做不出來
: 一直有錯誤 所以想請教一下
: 請問最小平方法的計算 如下面這種問題該怎麼解?? 謝謝
: ------------------------------------------------------------------
: 2.
: 1 資料表如下:用最小平方法求出nonlinear model y=ax^b的方程式
: 2 write M-file with MATLAB to solve a , b and plot the transformed
: and power equation curves and data.
: i 1 2 3 4 5 6
: Xi 1 2 3 3.2 3.5 3.8
: yi 1.2 5.2 13 15.1 19 23
首先你要寫一個副程式來計算數列與模型的差平方和
function f = lsqm(a,x,y)
model_y = a(1)*x.^a(2) %定義模型方程式(注意x為向量,需.^)
f = sum(y-model_y).^2 %計算差平方和
然後要寫一個主程式來找最小平方和值,找最小值的方法
有很多種,可以用matlab內建的fminserch或fmincon,詳細用
法請help,這邊以fminserch為例
x = [1,2,3,3.2,3.5,3.8] %input x
y = [1.2,5.2,13,15.1,19,23] %input y
ini_p = [0.01, 1] %input initial value
a = fminserch('lsqm',ini_p,[],x,y) %finding optimal modeling constant
這個是自己寫程式來處理,其實matlab裡面有內建最小平方
法的曲線擬合函數,如果有興趣可以help lsqcurvefit。
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 220.136.52.51
討論串 (同標題文章)
完整討論串 (本文為第 2 之 2 篇):