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]
  • 2
    "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 at 15:20
  • 1
    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 at 15:30
  • 1
    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 at 15:33
  • 1
    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 at 15:40
  • 2
    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 at 16:48

This is an extended comment that continues from the latest comment by @LukasLang: Is your formula correct?

Here's what it looks like without substituting numbers in for the constants and performing some simplifications:

$$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).

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.