簡體   English   中英

在Matlab中擬合2D數據

[英]Fitting 2D data in Matlab

你如何將ld(x ^ 2)+ 3y等2d曲線擬合到mxn數組?

更新

我的意思是我有mxn數組,並希望它適合2D曲線。 抱歉混淆。

您可以從使用meshgrid開始生成兩個與mxn數組的索引相對應的數組(為簡單起見,我們將其稱為z)。 例如:

[x,y] = meshgrid(1:size(z,2),1:size(z,1));

在命令窗口中跟蹤x和y以查看它們的結構,這是有意義的。

然后,您可以使用lsqnonlin(非線性最小二乘)等函數將2d曲線擬合到矩陣z。 因此,如果您想要擬合的曲線是“ log(x ^ 2)+ b y”,其中a和b是自由參數,那么您可以使用以下內容:

%Define a function which returns the residual between your matrix and your fitted curve
myfun = @(params) params(1)*log(x(:).^2) + params(2)*y(:) - z(:)
%Define initial guesses for parameters a, b
params0 = [1,3];
%Add lots of debugging info
opts = optimset('Display','Iter');
%Fit
fitparams = lsqnonlin(myfun,params0,[],[],opts);

我建議運行cftool 它實際上非常適合向導型小工具。

這是一個程序化的擬合示例 (我喜歡MATLAB文檔),以及一個可能相關的摘錄:

s = fitoptions('Method','NonlinearLeastSquares',...
               'Lower',[0,0],...
               'Upper',[Inf,max(cdate)],...
               'Startpoint',[1 1]);
f = fittype('a*(x-b)^n','problem','n','options',s);

使用擬合選項和n = 2的值擬合數據:

[c2,gof2] = fit(cdate,pop,f,'problem',2)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM