From: Tim Balmer on
I'm trying to fit a 2D gaussian to a surface using the Surface Fitting Tool. I choose the custom equation option and enter this formula: A*exp(-x^2/(2*sigmax^2)-y^2/(2*sigmay^2))

I think that either my equation is wrong or I am not using the Surface Fitting Tool properly because it generates a flat surface that doesn't fit at all and has a negative R-square value.

Any help would be greatly appreciated!

Thanks!
From: Tim Balmer on
Alright, I figured it out. Here is my solution. First, I changed the custom equation to this: A*exp(-(x-x0)^2/(2*sigmax^2)-(y-y0)^2/(2*sigmay^2)) It was also necessary to change the 'Fit Options'. I changed the StartPoint of all the Unknows to 10.

Now it works great. I hope this helps someone out there who is trying to fit 2D gaussians to their data using the Surface Fitting Tool.






"Tim Balmer" <tbalmer2(a)student.gsu.edu> wrote in message <hukkmu$pcd$1(a)fred.mathworks.com>...
> I'm trying to fit a 2D gaussian to a surface using the Surface Fitting Tool. I choose the custom equation option and enter this formula: A*exp(-x^2/(2*sigmax^2)-y^2/(2*sigmay^2))
>
> I think that either my equation is wrong or I am not using the Surface Fitting Tool properly because it generates a flat surface that doesn't fit at all and has a negative R-square value.
>
> Any help would be greatly appreciated!
>
> Thanks!
From: Shu-huai ZHANG on
Hi, Tim Balmer:

I am a newcomer about MATLAB. Now want to do the same thing like you.
Could you give me some help?

Thanks a lot.

"Tim Balmer" <tbalmer2(a)student.gsu.edu> wrote in message <hukkmu$pcd$1(a)fred.mathworks.com>...
> I'm trying to fit a 2D gaussian to a surface using the Surface Fitting Tool. I choose the custom equation option and enter this formula: A*exp(-x^2/(2*sigmax^2)-y^2/(2*sigmay^2))
>
> I think that either my equation is wrong or I am not using the Surface Fitting Tool properly because it generates a flat surface that doesn't fit at all and has a negative R-square value.
>
> Any help would be greatly appreciated!
>
> Thanks!
From: Tim Balmer on
Hi Shu-huai,
I'm pretty new to Matlab myslef, and I have never had any formal instruction, so I'll explain how I have fit 2D gaussians to my own data and hopefully it will help you figure out how to use it for your application.

-First you need to put your data into x, y and z variables. For instance, my z data is in a 13x19 matrix that looks like this:
z=[0 0 0 0 0 1 0 1 0 1 0 0 0
0 0 0 1 1 0 0 0 0 1 1 0 0
0 0 1 0 0 2 1 1 0 0 1 0 1
0 0 0 0 0 0 1 1 1 0 1 1 0
0 0 0 0 0 1 2 2 3 5 0 0 0
0 1 2 4 1 5 13 4 3 9 0 2 0
1 1 8 10 10 14 22 23 9 10 0 0 0
5 4 6 15 21 21 24 24 17 16 0 1 0
4 9 12 24 20 28 34 30 13 19 0 1 0
2 5 13 17 31 31 35 39 20 18 0 0 0
2 6 13 22 28 34 46 35 13 20 0 1 1
5 7 12 14 31 27 39 27 14 12 0 1 0
6 6 10 17 18 32 28 27 17 6 1 0 1
3 4 7 10 15 18 17 11 11 11 2 0 1
1 1 7 8 12 14 8 9 4 2 1 0 1
1 1 1 3 3 6 5 5 3 0 0 0 1
1 1 0 3 1 0 1 1 0 1 0 3 0
1 1 0 0 0 1 0 0 1 2 0 0 1
0 1 1 0 0 0 0 1 1 0 0 0 2]

-to make this 13x19 matrix into x, y, z variables that can be used with the surface fitting tool, do this:
[x,y]=meshgrid(1:13,1:19);
x=x(:);
y=y(:);
z=z(:);

-now call the surface fitting tool:
sftool

-choose x, y and z for the inputs

-choose custom equation from the drop down menu and paste in this equation:
a1*exp(-(x-x0)^2/(2*sigmax^2)-(y-y0)^2/(2*sigmay^2))

-click 'Fit'

-if the equation doesn't fit at all, change the fit options:
change all of the StartPoints to 10

--after doing this you should get these results:
General model:
f(x,y) = a1*exp(-(x-x0)^2/(2*sigmax^2)-(y-y0)^2/(2*sigmay^2))
Coefficients (with 95% confidence bounds):
a1 = 39.23 (37.56, 40.9)
sigmax = 2.332 (2.232, 2.431)
sigmay = 2.725 (2.61, 2.841)
x0 = 6.585 (6.486, 6.684)
y0 = 10.64 (10.52, 10.75)

Goodness of fit:
SSE: 1723
R-square: 0.924
Adjusted R-square: 0.9227
RMSE: 2.668


--To make this much faster you can choose File/Generate M-File and it will make a function called createSurfaceFit, that can be run with any data that is named x, y and z and is in the proper format. That way you won't have to enter the sftool each time.

Hope this helps!
Tim
From: Shu-huai ZHANG on
Hi,Tim

Thanks so much. I will try it.

shu-huai ZHANG

"Tim Balmer" <tbalmer2(a)student.gsu.edu> wrote in message <hv7tqg$hcm$1(a)fred.mathworks.com>...
> Hi Shu-huai,
> I'm pretty new to Matlab myslef, and I have never had any formal instruction, so I'll explain how I have fit 2D gaussians to my own data and hopefully it will help you figure out how to use it for your application.
>
> -First you need to put your data into x, y and z variables. For instance, my z data is in a 13x19 matrix that looks like this:
> z=[0 0 0 0 0 1 0 1 0 1 0 0 0
> 0 0 0 1 1 0 0 0 0 1 1 0 0
> 0 0 1 0 0 2 1 1 0 0 1 0 1
> 0 0 0 0 0 0 1 1 1 0 1 1 0
> 0 0 0 0 0 1 2 2 3 5 0 0 0
> 0 1 2 4 1 5 13 4 3 9 0 2 0
> 1 1 8 10 10 14 22 23 9 10 0 0 0
> 5 4 6 15 21 21 24 24 17 16 0 1 0
> 4 9 12 24 20 28 34 30 13 19 0 1 0
> 2 5 13 17 31 31 35 39 20 18 0 0 0
> 2 6 13 22 28 34 46 35 13 20 0 1 1
> 5 7 12 14 31 27 39 27 14 12 0 1 0
> 6 6 10 17 18 32 28 27 17 6 1 0 1
> 3 4 7 10 15 18 17 11 11 11 2 0 1
> 1 1 7 8 12 14 8 9 4 2 1 0 1
> 1 1 1 3 3 6 5 5 3 0 0 0 1
> 1 1 0 3 1 0 1 1 0 1 0 3 0
> 1 1 0 0 0 1 0 0 1 2 0 0 1
> 0 1 1 0 0 0 0 1 1 0 0 0 2]
>
> -to make this 13x19 matrix into x, y, z variables that can be used with the surface fitting tool, do this:
> [x,y]=meshgrid(1:13,1:19);
> x=x(:);
> y=y(:);
> z=z(:);
>
> -now call the surface fitting tool:
> sftool
>
> -choose x, y and z for the inputs
>
> -choose custom equation from the drop down menu and paste in this equation:
> a1*exp(-(x-x0)^2/(2*sigmax^2)-(y-y0)^2/(2*sigmay^2))
>
> -click 'Fit'
>
> -if the equation doesn't fit at all, change the fit options:
> change all of the StartPoints to 10
>
> --after doing this you should get these results:
> General model:
> f(x,y) = a1*exp(-(x-x0)^2/(2*sigmax^2)-(y-y0)^2/(2*sigmay^2))
> Coefficients (with 95% confidence bounds):
> a1 = 39.23 (37.56, 40.9)
> sigmax = 2.332 (2.232, 2.431)
> sigmay = 2.725 (2.61, 2.841)
> x0 = 6.585 (6.486, 6.684)
> y0 = 10.64 (10.52, 10.75)
>
> Goodness of fit:
> SSE: 1723
> R-square: 0.924
> Adjusted R-square: 0.9227
> RMSE: 2.668
>
>
> --To make this much faster you can choose File/Generate M-File and it will make a function called createSurfaceFit, that can be run with any data that is named x, y and z and is in the proper format. That way you won't have to enter the sftool each time.
>
> Hope this helps!
> Tim