# Performing a nonlinear fit from data imported from a text file

I have a text file data that I imported and I want to fit it with a given equation. I tried to fit but it did not work and gives

Here is my code:

data = Import["https://pastebin.com/raw/ZJDbhdgB", "Table"];

p = 3.5346*10^21;
k = 8.6173*10^-5;
g = 0.6361;
i = 0.03;
b = 0.2;
e = 1.6*10^-19;
d = 0.001;

curve =
NonlinearModelFit[
data,
p - 0.5*p*Sqrt[0.5*p^2+n^2*(Exp[-(g/k*x)])]*(1-m^2)/(p-0.5*p*Sqrt[0.5*p^2 + n^2*(E^-(g/k*x))]*(1 + m))^2,
{n,m},x]

• "but it did not work" What exactly didn't work? Did you get an error message? It makes it more difficult to help if you don't supply the data or the error/warning messages. – JimB Aug 14 '18 at 15:20
• Did you try to restart the kernel? Also, what does Dimension[data] return? And can you provide some sample data/your actual data? – Lukas Lang Aug 14 '18 at 15:30
• My guess is that you need to scale your data as the starting values are 1 for both m and n and likely those starting values are nowhere near the maximum likelihood estimates. Alternatively, choosing better starting values might help. Sharing some of your data (as @LukasLang) suggests would be very helpful. – JimB Aug 14 '18 at 15:33
• Try evaluating your function with the starting values p-0.5*p*Sqrt[0.5*p^2+n^2*(Exp[-(g/k*x)])]*(1-m^2)/(p-0.5*p*Sqrt[0.5*p^2+n^2*(E^-(g/k*x))]*(1+m^2))^2/.{n->1,m->1} and you'll just get 3.5346*10^21. No x in there so there's no place to go for trying subsequent values of n and m. You'll need either better starting values and/or get your equation to produce non-constant values. – JimB Aug 14 '18 at 15:40
• Thanks for the data (I've put the data on pastebin and edited your question to load the data from there). Are you sure the units between the measured data and your parameters match? As @JimB already said, the scales of the numbers are very different and very extreme, resulting in terms like $e^{-10^6}$, which feels very wrong – Lukas Lang Aug 14 '18 at 16:48

$$p-\frac{\left(1-m^2\right) \sqrt{n^2 e^{-\frac{g x}{k}}+\frac{p^2}{2}}}{2 \left(1-\frac{1}{2} (m+1) \sqrt{n^2 e^{-\frac{g x}{k}}+\frac{p^2}{2}}\right)^2}$$
Given that $p$ is so large, it certainly seems possible that the resulting equation is simply equal to $p$ for all practical purposes (which is what NonlinearModelFit is telling you).