How to perform 3D fitting using "nlinfit" 

I want to fit a cross-sectional image of a laser beam with a Gaussian function. And I'm thinking of using "nlinfit" to output the parameter covariance matrix. The image size is 1024 x 1344 pixels. The following process was performed, but fitting was not possible.
process
I=double(cdata)  % cdata is an array of image intensity distributions(1024×1344)
[h,w]=size(I);[X,Y]=meshgrid(1:w,1:h)
X=X(:);Y=Y(:);Z=I (:)
modelfun=@(b,X,Y)(b(1)+b(2)*exp((-1/2*(1-b(3)^2))*(((X-b(4))/b(5)).^2+((Y-b(6))/b(7)).^2-2*b(3)*(X-b(4)).*(Y-b(6))/(b(5)*b(7)))))
beta0=[15;150;0;600;90;570;85]
[beta,R,J,CovB,MSE,ErrorModelInfo] = nlinfit([X,Y],Z,modelfun,beta0)
When I performed the above process, I got an error if the number of arguments was incorrect.
Can you tell me the right way?

 採用された回答

Walter Roberson
Walter Roberson 2021 年 6 月 21 日

0 投票

Your modelfun expects 3 parameters, but nlinfit requires a function that expects two parameters
You should probably use
modelfun=@(b,XY)(b(1)+b(2)*exp((-1/2*(1-b(3)^2))*(((XY(:,1)-b(4))/b(5)).^2+((XY(:,2)-b(6))/b(7)).^2-2*b(3)*(XY(:,1)-b(4)).*(XY(:,2)-b(6))/(b(5)*b(7)))))

1 件のコメント

涼吾 神田
涼吾 神田 2021 年 6 月 21 日
Thanks for your answer.
I was able to fit when I typed "XY=[X Y]" and the code you written.
Thank you very much for your prompt response!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeInterpolation についてさらに検索

製品

リリース

R2020b

タグ

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by