# How to recover the original function from a fitting?

Suppose I have an original function as follows fun[x_] := 6*Exp[-4 x^2];

which has the following plot

Plot[fun[x], {x, 0, 4}, PlotRange -> All]


Then I produce a dataset from it in far x's (the reason for choosing the far x's is that I want to do a cross-check about asymptotic behavior of my original function in a real case)

vals = Table[{x, fun[x]}, {x, 2, 5, 5/10000}];


when I do the fitting

fit = NonlinearModelFit[vals, {a1*Exp[-b1*(x)^2]}, {a1, b1}, x,
Method -> NMinimize];
fit//Normal
(*0.54381860615 E^(-3.42096742465 x^2)*)


I get the above result where exponent is close to original one (namely 4) but the coefficient is so far with respect to original one (6). However the more shift I do, the better coefficient is obtained. According to Taylor theorem if we have enough points of a function and its derivatives we must recover the original function, so why I can't recover the original function by enough points? any idea?

Clear["Global*"]

fun[x_] := 6*Exp[-4 x^2]

vals = Table[{x, fun[x]}, {x, 2, 5, 1/20}];


With data that is expected to be exponential, work with the log of the data.

vals2 = {#[[1]], Log[#[[2]]]} & /@ vals;

f = Exp[a1 - b1*x^2] /.
NonlinearModelFit[vals2, {a1 - b1*x^2, b1 > 0}, {a1, b1}, x,
Method -> "NMinimize"]["BestFitParameters"] // Quiet

(* E^(1.79176 - 4. x^2) *)


The constant factor is

E^(Log[f /. x -> 0])

(* 5.99999 *)

• Thanks but why do you use Exp[a1 - b1*x^2] for fitting? finally you just obtained factor 6 and not the original function, 6*Exp[-4 x^2] ? Apr 12, 2022 at 15:48
• Since I took the Log of the data in order to get a better fit, the parameters returned are for Exp[a1-b1*x^2]. The result obtained is E^(1.79176 - 4.*x^2). This is equivalent to E^(1.79176) * E^(-4.*x^2). The initial factor E^(1.79176)  is equivalent to 5.99999 so the result is equivalent to 5.99999 E^(-4.0*x^2). The last part was just showing you that the constant factor was 5.99999 or about 6 Apr 12, 2022 at 17:35
• Many thanks for your detailed comment. Apr 12, 2022 at 18:05

I suggest you consult statistician or someone who knows numerical analysis if these analyses/concerns are for work rather than a hobby.

The "data" generated from the formula is without error (other than numerical precision) and doesn't even require a regression function or even more than 2 data points. Here's why I say that:

(* Define the function *)
fun[x_] := 6*Exp[-4 x^2]

(* Now generate values at 2 values of x *)
x1=2;
x2=5;

(* Now determine the coefficients in the function from the 2 points *)
Solve[{a Exp[-b x1^2] == fun[x1], a Exp[-b x2 ^2] == fun[x2]}, {a, b}, Reals]
(* {{a -> 6, b -> 4}} *)


This also works for larger values of $$x$$ (which I think you are interested in larger values as opposed to what "asymptotic" means) as long as you rationalize the values of $$x$$:

x1 = Rationalize[20.7, 0];
x2 = Rationalize[50.8, 0];
Solve[{a Exp[-b x1^2] == fun[x1], a Exp[-b x2 ^2] == fun[x2]}, {a, b}, Reals]
(* {{a -> 6, b -> 4}} *)


For this particular function you can avoid the need to rationalize if you take the log of the function:

x1 = 20.7;
x2 = 50.8;
Solve[{Log[a] - b x1^2 == Log[6] - 4 x1^2, Log[a] - b x2 ^2 == Log[6] - 4 x2^2}, {a, b}, Reals]
(* {{a -> 6., b -> 4.}} *)
`