3
$\begingroup$

I have some data:

data = {{0.3, 0}, {0.5, 0}, {0.84, 0}, {1, 0}, {1.16, 159.1940}, {1.3, 
218.835}, {1.5, 278.0620}, {1.8, 340.758}, {2.01, 374.9820}, {2.3, 
416.09}}

which looks like:

enter image description here

It seems that some function of the form: $$a (x - 1)^b$$ should fit the data.

However, if we use

FindFit[data, a (x - 1)^b, {a, b}, x]

Mathematica gives: "The Jacobian is not a matrix of real numbers at {a,b} = {1.,1.}".

How can I fix this?

$\endgroup$
  • $\begingroup$ Possible duplicate of How to fit the data? $\endgroup$ – rhermans Aug 21 at 22:54
  • 2
    $\begingroup$ While accepting is one of the things to do after your question is answered, we recommend that users should test answers before voting and wait 24 hours before accepting the best one. That allows people in all timezones to answer your question and an opportunity for other users to point alternatives, caveats or limitations of the available answers. $\endgroup$ – rhermans Aug 21 at 22:56
  • $\begingroup$ @rhermans Thanks for your comment. I'm new to this website. $\endgroup$ – user67082 Aug 21 at 22:57
2
$\begingroup$

You just edited away your original data, but I used the original before you did.

Try this

data = {{0.3, 0}, {0.5, 0}, {0.84, 0}, {1, 0}, {1.16, 159.1940}, {1.3, 218.835}, 
  {1.5, 278.0620}, {1.8, 340.758}, {2.01, 374.9820}, {2.3, 416.09}, {2.49, 439.6510},
  {2.8, 473.367}, {2.99, 492.4980}, {3.3, 521.149}, {3.5, 538.4470}, {3.8, 563.011},
  {4.01, 579.4590}, {4.3, 600.841}, {4.51, 615.5270}, {4.8, 635.659}, {4.98, 647.8910},
  {5.3,668.192}, {5.5, 680.684}, {5.8, 698.925}, {6, 710.746}, {6.3, 728.073},
  {6.5, 739.405}, {6.8, 755.988}, {7, 766.847}, {7.3, 782.793}, {7.5, 793.227},
  {7.8, 808.629}, {8, 818.706}, {8.3, 833.612}, {8.5, 843.363}, {8.8, 857.815}, 
  {9, 867.267}, {9.3, 881.302}, {9.5, 890.536}, {9.8, 904.183}, {10, 913.159}};
sumsquared=Total[Map[(#[[2]]-a*(#[[1]]-1)^b)^2&,Drop[data,4]]];
sol=NMinimize[sumsquared,{a,b}];
Show[ListPlot[data],Plot[a(x-1)^b/.sol[[2]],{x,1,10}]]

You might notice that I dropped your first four data points because the zero values for y were causing complaints about complex values. Then I took the rest and did a sum of squares and minimized that. The result looks pretty good, even though the residual isn't close to zero, probably because of the size of your y values.

See if that is good enough for your purposes.

$\endgroup$
  • $\begingroup$ Many thanks! How can I get the value of a and b? $\endgroup$ – user67082 Aug 21 at 22:25
  • 1
    $\begingroup$ Look at the value of sol and you should see {525.9802649238213, {a -> 368.267337527918, b -> 0.4111500193417123}} and there are your a and b values. $\endgroup$ – Bill Aug 21 at 22:27
  • $\begingroup$ Many thanks for your kind help! $\endgroup$ – user67082 Aug 21 at 22:28
  • 1
    $\begingroup$ You be careful with that. Don't trust it. Don't trust me. Check all your assumptions. Test it before you depend on it. Glad it seemed to work for you. $\endgroup$ – Bill Aug 21 at 22:33
3
$\begingroup$

You can try a Piecewise model:

ClearAll[a, b, c]
fit = FindFit[data, Piecewise[{{a (x - 1)^b, x > 1}}], {a, b}, x]

{a -> 373.218, b -> 0.446663}

Show[Plot[Piecewise[{{a (x - 1)^b, x > 1}}] /. fit, {x, 0, 3}], 
 ListPlot[data, PlotStyle -> Directive[Red, PointSize[Large]]]]

enter image description here

You can also try a three-parameter model:

ClearAll[a, b, c]
fit = FindFit[data, Piecewise[{{a (x - c)^b, x > c}}], {a, b, c}, x]

{a -> 381.099, b -> 0.397244, c -> 1.04951}

Show[Plot[Piecewise[{{a (x - c)^b, x > c}}] /. fit, {x, 0, 3}], 
 ListPlot[data, PlotStyle -> Directive[Red, PointSize[Large]]]]

enter image description here

$\endgroup$
  • $\begingroup$ Perfect! Thanks! $\endgroup$ – user67082 Aug 21 at 22:32
2
$\begingroup$

Similar Q&A here:

Need to fit curve to 5 parameters: what's a problem with NonlinearModelFit?

How to fit the data?


@kglr beated me to Piecewise, this one with NonlinearModelFit

data = {{0.3, 0}, {0.5, 0}, {0.84, 0}, {1, 0}, {1.16, 159.1940}, {1.3,
    218.835}, {1.5, 278.0620}, {1.8, 340.758}, {2.01, 374.9820}, {2.3,
    416.09}}

fit = NonlinearModelFit[
   data
   , Piecewise[{{a (x - 1)^b, x >= c}}, 0]
   , {a, b, {c, 1}}
   , x
   ];

Show[ 
 Plot[
  fit[x]
  , {x, 0, 3}
  , PlotStyle -> Red
  , PlotRange -> {-100, 700}
  , PlotTheme -> "Scientific"
  , Exclusions -> None
  ]
 , ListPlot[
  data
  , Joined -> False
  , PlotRange -> All
  , PlotStyle -> Black
  ]
 ]

enter image description here

fit["ParameterTable"]

enter image description here

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy