Message Boards Message Boards

GROUPS:

Non-linear data fitting: Overflow occurred in computation?

Posted 6 months ago
1637 Views
|
5 Replies
|
3 Total Likes
|

I have an imported data set

data1 = {{1.`*^-16, 0.`}, {7.746`, 0.1129`}, {13.4164`, 
    0.1554`}, {17.3205`, 0.1891`}, {20.4939`, 0.2139`}, {24.4949`, 
    0.2587`}, {30.`, 0.3351`}, {34.641`, 0.4076`}, {38.7298`, 
    0.4437`}, {42.4264`, 0.4992`}, {51.9615`, 0.646`}, {60.`, 
    0.7401`}, {67.082`, 0.8343`}, {73.4847`, 0.9055`}, {79.3725`, 
    0.9594`}, {84.8528`, 0.9615`}, {90.`, 0.9541`}, {94.8683`, 
    0.9723`}, {99.4987`, 0.9951`}, {103.923`, 0.9901`}, {108.1665`, 
    0.989`}, {112.2497`, 1.`}, {116.1895`, 0.9898`}, {120.`, 
    0.9876`}};

Which I'd like to fit to an equation. (in the screenshot)enter image description here

It seems that I've done something wrong along the way, but I can't seem to find it. The fitting parameter that Mathematica has given me is negative, which is obviously ridiculous.

Attachments:
POSTED BY: Jung Hwan Kim
5 Replies
Posted 6 months ago

Your first number is {1.*^-16, 0.} which is essentially two zeros. Maybe try to change it {0, 0}?

POSTED BY: Ehud Behar

Well, I've tried that, but I now have a new set of errors. enter image description here

POSTED BY: Jung Hwan Kim

Your fitting function is a horrible fit to your data. You must have something wrong. To visualize this do the following:

Manipulate[
 Show[ListLinePlot[data1, PlotStyle -> Orange], 
  Plot[2*Sqrt[diffusion]*x*(1/L)*(Pi^(-0.5) + 2*(\!\(
\*UnderoverscriptBox[\(\[Sum]\), \(n = 
           1\), \(100\)]\(\((\((\(-1\))\)^n)\)*
          Erfc[\((n*L)\)/\((Sqrt[diffusion]*x)\)]\)\))), {x, 0, 120}],
   PlotRange -> All], {diffusion, 0.0001, 1}]

You can sweep through values of diffusion and see it is a horrible fit. To get a "Best Fit" (which is equally horrible!) you need to constrain the value of diffusion to positive values to avoid imaginary numbers:

In[4]:= dataFitting = 
 FindFit[data1, {2*Sqrt[diffusion]*x*(1/L)*(Pi^(-0.5) + 2*(\!\(
\*UnderoverscriptBox[\(\[Sum]\), \(n = 
           1\), \(100\)]\(\((\((\(-1\))\)^n)\)*
          Erfc[\((n*L)\)/\((Sqrt[diffusion]*x)\)]\)\))), 
   diffusion > 0.0}, {diffusion}, x]

Out[4]= {diffusion -> 0.00911926}

Which gives you the "Best Fit" of:

function = 2*Sqrt[diffusion]*x*(1/L)*(Pi^(-0.5) + 2*(\!\(
\*UnderoverscriptBox[\(\[Sum]\), \(n = 
          1\), \(100\)]\(\((\((\(-1\))\)^n)\)*
         Erfc[\((n*L)\)/\((Sqrt[diffusion]*x)\)]\)\))) /. 
  dataFitting; 
Show[ListLinePlot[data1, PlotStyle -> Orange], Plot[function, {x, 0, 120}], 
     PlotRange -> All]

enter image description here

Which is not very good but "As good as you can get" with that function as your fitting function.

Regards,

Neil

POSTED BY: Neil Singer

Thank you for this enlightenment. I'll try to work things out with my function. Although I haven't been able to solve this, I've got some valuable tips from your coding.

Thank you again, and have a lovely day!

POSTED BY: Jung Hwan Kim
Posted 6 months ago

Neil Singer gave you good advice in checking out the fit. It just turned out that an even smaller value of diffusion is needed.

nMax = 5;
L = 0.05484; 
nlm = NonlinearModelFit[data1, 2 Sqrt[diffusion]*x*(1/L)*(Pi^(-0.5) +
     2*Sum[((-1)^n)*Erfc[(n*L)/(Sqrt[diffusion]*x)], {n, 1, nMax}]),
  {{diffusion, 0.0000002}}, x];
nlm["BestFitParameters"]
(* {diffusion -> 1.72409*10^-7} *)

Show[ListPlot[data1], Plot[nlm[x], {x, Min[data1[[All, 1]]], Max[data1[[All, 1]]]}]]

Data and fit

Not a great fit. (And using nMax = 5 or nMax = 100 changes almost nothing.) But I wonder if there's simply a missing multiplier. Here's a fit adding in a multiplicative parameter:

nMax = 5;
L = 0.05484; 

nlm = NonlinearModelFit[data1, 2 a Sqrt[diffusion]*x*(1/L)*(Pi^(-0.5) +
     2*Sum[((-1)^n)*Erfc[(n*L)/(Sqrt[diffusion]*x)], {n, 1, nMax}]),
  {{diffusion, 0.0000002}, {a, 1}}, x];
nlm["BestFitParameters"]
(* {diffusion -> 1.51303*10^-7, a -> 1.51} *)

Show[ListPlot[data1], Plot[nlm[x], {x, Min[data1[[All, 1]]], Max[data1[[All, 1]]]}]] 

Data and better fit

POSTED BY: Jim Baldwin
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract