最近要解決用2維矩陣擬合2維函式的問題,貌似在中文**中沒有找到方法,最後在stack overflow才問到了答案,解答如下。
假設有5*5陣列:
d= [0.0177104427823448,0.00246661459209512,0.0399831543374395,0.0615494164555707,0.0476204124707652;0.0275276152854314,0.0219153841813084,0.0581144391404502,0.144890028400954,0.157839631316098;0.0622883972729130,0.0716157303159909,0.245482781674067,0.123999612575059,0.177495187746408;0.0200735764542146,0.0573087934038160,0.0636451189717613,0.0160810084568415,0.0484992279558924;0.0185180386159227,0.00841167700273800,0.0372017422726281,0.0173721095082637,0.0459520362441099]
想用最小均方法擬合2維函式:
r = alfa*sin(pi*(n1+delta1))*sin(pi*(n2+delta2)) / (25*sin(pi/5*(n1+delta1))*sin(pi/5*(n2+delta2)))
可以在matlab中這樣實現:
[n,m]=size(d);%assumes that d is a n x m matrix
[x,y]=meshgrid(1:n,1:m);%your x-y coordinates
x(:,1)=x(:); % x= first column
x(:,2)=y(:); % y= second column
f=d(:); % your data f(x,y) (in column vector)
%--- now define the function in terms of x
%--- where you use x(:,1)=x and x(:,2)=y
fun = @(c,x) c(1)*sin(pi*(x(:,1)+c(2))).*sin(pi*(x(:,2)+c(3))) ./ (25*sin(pi/5*(x(:,1)+c(2))).*sin(pi/5*(x(:,2)+c(3))));
%--- now solve with lsqcurvefit
options=optimset('tolx',1e-6);
c0=[1 0 0];%start-guess here
cc=lsqcurvefit(fun,c0,x,f,,,options);
ifit=fun(cc,x);
ifit=reshape(ifit,[n m]);%fitting data reshaped as matrix
surf(x,y,ifit);
hold on;
plot3(x, y, dataarray);
在Matlab中使用Levmar
本文介紹如何在windows平台下,在matlab中適用levmar。首先,網上教程很多,有的需要適用cmake,那樣略顯麻煩。levmar本身提供了對visual studio的支援,並且大部分在windows的開發者都是適用visual studio的,因此本文不使用cmake,而直接利用vis...
在matlab中使用libsvm
上周五開始就嘗試使用libsvm,因為matlab彈出的各種編譯器問題解除安裝2014a,裝上2017b,憂傷地發現還是失敗 目前最新為libsvm 3.22 關於新增路徑 方法一 主頁 設定路徑 選擇windows資料夾即可 方法二 將libsvm 3.22所在資料夾設定為當前路徑 選擇windo...
2 如何使用matlab擬合曲線
輸入資料 做資料曲線擬合,當然該有資料,本經驗從以如下資料作為案例。新增資料到curve fitting程式 這一步就是將你要擬合的資料新增到curve fitting程式中,同時給你擬合的曲線命名。選擇曲線擬合的方法型別 常見的擬合曲線有多項式的 指數的 對數的等等。curve fitting程式...