Fitting an Equation to an Image

I'm very new to data analysis and I'm trying to figure out how I can fit an equation to an image I generated from a large set of data I collected. I'd then want to overlap this plot to the image to check how good of a job the fit did.

Equation[x_,y_,x0_,y0_,w_]=(Exp[-((x - x0)^2 + (y - y0)^2)/w^2] Abs[Exp[I ArcTan[(y - y0), (x - x0)]] Sqrt[(x - x0)^2 + (y - y0)^2]])^2;


When I attempt to fit this equation to the collected data set

DonutFit=FindFit{data,Equation[x,y,x0,y0,w],{{x0,0},{y0,0},{w,0}},{x,y}};
ImageCompose[donut,DonutFit]


I get the error

I'm sure my approach to this was too simple and that is why I'm getting this error. How can I correct this and generate the fit from the image? My Collected Dataset

This is not an answer but it might give you some hints. I am not sure if the function that you want to fit to your data is representative of the image.

Let's import the data and get rid of the first 3 rows which consist of zeros

ds = Drop[Import["fit.csv"], 3];

MatrixPlot[ds]


Now you have a matrix with dimensions 1024x1024

dim=Dimensions[ds]


{1024, 1024}

In order to fit your data you need to have them in a form $$(x,y,f(x,y))$$. Therefore we create the values for $$x$$ and $$y$$ as the image coordinates (or matrix position)

    ds3Ddata =Flatten[Table[{i, j, ds[[i, j]]}, {i, 1, dim[[1]]}, {j, 1,dim[[2]]}], 1];


Now, if you want we can plot everything but this is going to take a long time. Thus, I select only the area around the blob and I will be working only with it. If you want (for the analysis below) you can still use the entire data set (ds3Ddata).

subSet = Select[ds3Ddata, 100 < #[[1]] < 550 && 350 < #[[2]] < 800 &];
subSetPlot = ListPointPlot3D[subSet, PlotRange -> All]


I am now taking your equation

equation[x_, y_, x0_, y0_,
w_] = (Exp[-((x - x0)^2 + (y - y0)^2)/w^2] Abs[
Exp[I ArcTan[(y - y0), (x - x0)]] Sqrt[(x - x0)^2 + (y -
y0)^2]])^2;


and I try I check its form/behaviour. The quantities x0 and y0 correspond to the centre of the deep and the w to the extension of the surface around the deep.

Manipulate[
Plot3D[ equation[x, y, x0, y0, w], {x, 100, 550}, {y, 350, 800},
PlotRange -> All], {{x0, 350}, 100, 1000}, {{y0, 570}, 200,
1000}, {{w, 100}, 10, 1000, 1}]


You are probably expecting something like this from the fit (I have multiply the function by 60 in order to bring it roughly to the same scale with the data).

Show[{subSetPlot,
Plot3D[ 60 equation[x, y, 346, 580, 76], {x, 100, 550}, {y, 350,
800}, PlotRange -> All]}]


But as you see the function is not really representative of the actual shape formed by your data. It does have a deep but your points form a steep plateau (disk) on the top followed by a sudden drop of the values around the disc.

Note also, there is a white stripe for x=x0 and y<y0 that I do not know where does it come from!The only "problematic point" should occur for x=x0 and y=y0

equation[x0, y0, x0, y0, w]


Let's leave this point open for a separate discussion!

We can try to minimize the total chi-squared differences between your model and your data totChiSquare =

totChiSquare =Total[Table[(subSet[[i, 3]] - equation[subSet[[i, 1]], subSet[[i,2]],x0, y0, w])^2, {i, 1, Length[subSet]}]];

totChiSquareMin = NMinimize[totChiSquare, {x0, y0, w}]


{9.01926*10^13, {x0 -> 562.432, y0 -> 356.102, w -> 414.644}}

Show[{subSetPlot,
Plot3D[equation[x, y, x0, y0, w] /. totChiSquareMin[[2]], {x, 100,
550}, {y, 350, 800}, PlotRange -> All]}]


which is of course not representative of your data. If you try to add all the points then you are going simply to get a surface closer to the base of your surface.

You can get the same results with NonlinearModelFit by giving it some guidance around the x0 and y0 points, which is at the end overlooked from the optimisation algorithm

nlmf = NonlinearModelFit[subSet,
equation[x, y, x0, y0, w], {{x0, 346}, {y0, 550}, {w, 70}}, {x, y},
Method -> "NMinimize"];
nlmf["BestFitParameters"]


{x0 -> 562.432, y0 -> 356.101, w -> 414.644}

You can try and play a bit with the parameters and maybe you have more luck. Also, I strongly recommend to use a different function.

You do not describe the nature of these points i.e. what they represent. If the fitted function comes from theory and these are the results of an experiment then definitely the flat plateau, forming the disc, represents a type of saturation, therefore you need to increase, if possible, the threshold of your experiment/device.