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?

 Respuesta aceptada

Walter Roberson
Walter Roberson el 21 de Jun. de 2021
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 comentario

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

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Productos

Versión

R2020b

Etiquetas

Preguntada:

el 21 de Jun. de 2021

Comentada:

el 21 de Jun. de 2021

Community Treasure Hunt

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

Start Hunting!

Translated by