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?

 Akzeptierte Antwort

Walter Roberson
Walter Roberson am 21 Jun. 2021

0 Stimmen

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 Kommentar

涼吾 神田
涼吾 神田 am 21 Jun. 2021
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!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Produkte

Version

R2020b

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by