# Fitting different data sets with different models and 2 free variables using FindFit

I have a T1 equation from which I got two noisy datasets, one for y=0 (datax) and the other for x=0 (datay) (t is always equal to 1):

Dif = 0.00000013; K0 = 0.5; z = 0; linf\[Alpha] = 0.0;

T1[x_, y_, t_, b_, l_, d_, m_, Intens_] /;
NumberQ[x] && NumberQ[y] && NumberQ[b] && NumberQ[l] && NumberQ[d] &&
NumberQ[m] && NumberQ[Intens] :=
Intens/(2*\[Pi]*K0)*
NIntegrate[
Sqrt[1 + m^2]*
Erfc[Sqrt[(x - \[Alpha])^2 + (y - \[Beta])^2 + (z - (m*\[Alpha] -
d))^2]/Sqrt[
Dif*4*t]]/(Sqrt[(x - \[Alpha])^2 + (y - \[Beta])^2 + (z - (m*\
\[Alpha] - d))^2]), {\[Beta], -b/2, b/2}, {\[Alpha], linf\[Alpha],
l*(1/(1 + m^2))}]

Block[{y = 0, t = 1, b = 0.001, l = 0.001, d = 0.0001,
m = -Tan[45*Pi/180], Intens = 25000},
datax = Table[{x,
T1[x, y, t, b, l, d, m, Intens] +
Random[NormalDistribution[0, 0.1]]}, {x, -0.002, 0.002,
0.000015}]];

Block[{x = 0, t = 1, b = 0.001, l = 0.001, d = 0.0001,
m = -Tan[45*Pi/180], Intens = 25000},
datay = Table[{y,
T1[x, y, t, b, l, d, m, Intens] +
Random[NormalDistribution[0, 0.1]]}, {y, -0.002, 0.002,
0.000015}]];


My goal is to find the best fit parameters for both datasets. The parameters are {b,l,d,m,Intens}, and must be the same for the fit of datax and datay. The free variables instead are {x,y}. (t=1)

The main problem is that FindFit has to look for the same optimal values of the parameters {b,l,d,m,Intens} passing from T1 with y=0 for datax, to T1 with x=0 for datay. It would also be useful not to impose initial search values for the parameters, as I should not know them. Always if this is possible

The fit, for example, only for datax is of this type and it works:

fitx = FindFit[datax,
T1[x, 0, 1, b, l, d, m,
Intens], {{b, 0.0015}, {l, 0.0015}, {d, 0.00015}, {m, -1}, {Intens,
20000}}, x, Method -> "LevenbergMarquardt"]
{b -> 0.00120948, l -> 0.000999963, d -> 0.000084307, m -> -1.00055,
Intens -> 22038.8}


Can anyone suggest me a way to solve my problem?

• Take a look at this repository function. I think it may help you with what you need: resources.wolframcloud.com/FunctionRepository/resources/… – Sjoerd Smit Jan 29 '20 at 11:19
• Thank you a lot. I'm taking a look but I understand that for 2 free variables the system does not work. Or maybe I didn't understand something – Lorenzo F Jan 29 '20 at 11:46
• I think it should work. You should format your datasets as matrices {{x, y, T1}..} (where either the x or y column is 0) and then specify the fit function as T1[x, y, 1, b, l, d, m, Intens]. Then you can specify that x and y are independent variables and the rest as fit parameters. – Sjoerd Smit Jan 29 '20 at 12:23
• You have to excuse me but I'm a newbie. I just can't use this repository function. How do you proceed to use it? – Lorenzo F Jan 29 '20 at 15:11

May be you can learn from that example. (I just wanted to avoid waiting for the long caclulations in your post.)

model = a Exp[-b ((x - x0)^2 + (y - y0)^2)];

pl = Plot3D[1.2 Exp[-34 ((x - .02)^2 + (y - .07)^2)],
{x, 0, 1}, {y, 0, 1},
PlotRange -> All, PlotStyle -> Opacity[.5]];

data1 = Table[{x, 0,
1.2 Exp[-34 ((x - .02)^2 + (0 - .07)^2)] +
RandomReal[{-.1, .1}]}, {x, 0, 1, .01}];

data2 = Table[{0, y,
1.2 Exp[-34 ((0 - .02)^2 + (y - .07)^2)] +
RandomReal[{-.1, .1}]}, {y, 0, 1, .01}];

data12 = Join[data1, data2];

lp = ListPointPlot3D[data12, PlotStyle -> Red];

Show[pl, lp]


fit = FindFit[data12,
model, {{a, 1}, {b, 30}, {x0, .1}, {y0, .1}}, {x, y}]

(*   {a -> 1.2083, b -> 35.4499,
x0 -> 0.0248425, y0 -> 0.0709918}   *)

• It solved my problem! Mistake in data creation. Thanks a lot – Lorenzo F Jan 30 '20 at 11:23