1
$\begingroup$

See this code, what mistake I did? (I got no plot)

data = {{595070, 340}, {595070, 340}, {595098, 339}, {595158, 
    340}, {595218, 340}, {595338, 348}, {595458, 351}, {595638, 
    355}, {595818, 356}, {596082, 360}, {596322, 361}, {596922, 
    366}, {597521, 367}, {598481, 367}, {599322, 375}, {600523, 
    380}, {601723, 382}, {603523, 384}, {605323, 388}, {608924, 
    394}, {612523, 396}, {619724, 403}, {626926, 408}, {648527, 
    417}, {670129, 419}, {691731, 423}, {712906, 428}, {734504, 
    429}, {756104, 428}, {776690, 430}, {798291, 430}, {819890, 
    433}, {841490, 435}, {863090, 436}, {884692, 437}, {906290, 
    438}, {927892, 441}, {949492, 442}, {971090, 441}, {992691, 
    441}, {1014291, 442}, {1035891, 446}, {1039491, 445}};
model = a + b*Exp[c*(e*x^f + g)^1.5];
fit = FindFit[data, model, {a, b, c, d, e, f, g}, x]
Show[Plot[Evaluate[model /. fit], {x, 595070, 1039491}], 
 ListPlot[data, PlotStyle -> Red]]

I got:

                           457884587
{a -> -2.186827693987254*10         , b -> 1., c -> 1., d -> 1., e -> 0.999993, f -> 1., 
 
  g -> 0.810866}

But I got no plot:

enter image description here


Similarly with

data = {{595070, 340}, {595070, 340}, {595098, 339}, {595158, 
    340}, {595218, 340}, {595338, 348}, {595458, 351}, {595638, 
    355}, {595818, 356}, {596082, 360}, {596322, 361}, {596922, 
    366}, {597521, 367}, {598481, 367}, {599322, 375}, {600523, 
    380}, {601723, 382}, {603523, 384}, {605323, 388}, {608924, 
    394}, {612523, 396}, {619724, 403}, {626926, 408}, {648527, 
    417}, {670129, 419}, {691731, 423}, {712906, 428}, {734504, 
    429}, {756104, 428}, {776690, 430}, {798291, 430}, {819890, 
    433}, {841490, 435}, {863090, 436}, {884692, 437}, {906290, 
    438}, {927892, 441}, {949492, 442}, {971090, 441}, {992691, 
    441}, {1014291, 442}, {1035891, 446}, {1039491, 445}};
model = a + b*Exp[c*(e*x^f + g)^1.5];
nlm = NonlinearModelFit[data, model, {a, b, c, d, e, f, g}, x]
Show[Plot[Evaluate[model /. fit], {x, 595070, 1039491}], 
 ListPlot[data, PlotStyle -> Red]]

I read in https://mathematica.stackexchange.com/ many similar and related questions, but could not understand. (I am a new user of mathematica, currently using V12.1).

And still confused with FindFit, FindFormula, Fit, NonlinearModelFit differences.

Somehow I got able to put the starting values, but still not working.

I should get something similar to this (the red curve was drawn (by me) for illustration):

enter image description here


Please tell me what is wrong.

Any help would be really appreciated. Thanks.

$\endgroup$
4
  • 2
    $\begingroup$ Where did you get that model? What would be reasonable values for the parameters? It seems very unlikely that you will ever get a reasonable fit without setting some initial values. $\endgroup$
    – MarcoB
    Jun 30, 2020 at 19:44
  • $\begingroup$ @MarcoB Sorry, I am sure that my model is fine to represent these data points. When I re-scaled the data (dividing x by 1000000 , and dividing y by 100), DESMOS found the parameters, this means my model is representative for my original data! i.imgur.com/klJatY4.png $\endgroup$ Jul 1, 2020 at 13:04
  • 1
    $\begingroup$ Hussain, the fit you show is not very good at all. In particular it seems to completely miss the increasing trend towards the end. If this was my data, I would actually take the fit you showed as proof that your model does NOT actually fit the data. If you want further help with the model you have, please describe where it came from physically. $\endgroup$
    – MarcoB
    Jul 1, 2020 at 14:44
  • $\begingroup$ @MarcoB , dear, whether it is good or not good, the fitted curve should be as this i.imgur.com/klJatY4.png. If you have some data points, you should not change the model as per your convenient/happiness. Hope you understand my point. Thanks for your comments. $\endgroup$ Jul 1, 2020 at 16:13

3 Answers 3

5
$\begingroup$

I don't see how your model could possibly fit the shape of the data. I have to believe that you're being imaginative rather than having a model based on some theory.

The following simpler model can provide a reasonable description of your data:

data = {{595070, 340}, {595070, 340}, {595098, 339}, {595158, 
    340}, {595218, 340}, {595338, 348}, {595458, 351}, {595638, 
    355}, {595818, 356}, {596082, 360}, {596322, 361}, {596922, 
    366}, {597521, 367}, {598481, 367}, {599322, 375}, {600523, 
    380}, {601723, 382}, {603523, 384}, {605323, 388}, {608924, 
    394}, {612523, 396}, {619724, 403}, {626926, 408}, {648527, 
    417}, {670129, 419}, {691731, 423}, {712906, 428}, {734504, 
    429}, {756104, 428}, {776690, 430}, {798291, 430}, {819890, 
    433}, {841490, 435}, {863090, 436}, {884692, 437}, {906290, 
    438}, {927892, 441}, {949492, 442}, {971090, 441}, {992691, 
    441}, {1014291, 442}, {1035891, 446}, {1039491, 445}};

model = a + b Log[(x - c)/100000];
fit = NonlinearModelFit[data, {model, c < Min[data[[All, 1]]]}, {{a, 450}, b, {c, 595000}}, x];
fit["BestFitParameters"]
(* {a -> 422.118, b -> 14.9335, c -> 594693.}
Show[ListPlot[data, PlotStyle -> Red], 
 Plot[fit[x], {x, 594700, 1039491}, PlotRange -> All]]

Data and fit

But this suggests your issue isn't a Mathematica issue.

Using NonlinearModelFit gets one information to check on model fit, whereas FindFit does not.

fit["ParameterTable"]

Parameter table

$\endgroup$
7
  • $\begingroup$ Sorry, I am sure that my model is fine to represent these data points. When I re-scaled the data (dividing x by 1000000 , and dividing y by 100), DESMOS found the parameters, this means my model is representative for my original data! i.imgur.com/klJatY4.png $\endgroup$ Jul 1, 2020 at 12:52
  • $\begingroup$ Yes, I was wrong that your equation couldn't be bent into a shape similar to your data. I'll update my answer later today. But note that your equation has redundant parameters. An equivalent model with one less parameter is a1 + a2 Exp[a3 (x^a5 + a6)^1.5]. That suggests that while predictions from your model can approximate the data, interpretation of the parameters should not occur. $\endgroup$
    – JimB
    Jul 1, 2020 at 16:01
  • $\begingroup$ Yes please sir, your help would be really appreciated. I am a new user of mathematica V12.1 as I mentioned earlier. I have one idea, I am not sure if it will help us to solve my problem. The idea is: since my x values are large, then we may do as follows: data={{x1,y1},{x2,y2},...}, then divide x values by 1000000 and divide y values by 100, so: data2={{x1/1000000,y1/100},{x2/1000000,y2/100},...}, then do FindFit for data2, once we get the model with the parameters, we can replace y by y/100 and replace x by x/1000000. Hope my idea is fine, and hope you understand my point. Thanks in advance. $\endgroup$ Jul 1, 2020 at 16:24
  • $\begingroup$ Scaling the data is a good thing and I should have followed that advice I've given here many times. But also please heed @MarcoB 's advice: either something is wrong with the data or something is wrong with the theoretical model (or even both). $\endgroup$
    – JimB
    Jul 1, 2020 at 16:28
  • $\begingroup$ I understand what @MarcoB means. But I say, if the model is wrong OR the data is wrong, OR both are wrong, then why Desmos could calculate the parameters for that model but mathematica did not. You understand my point sir? $\endgroup$ Jul 1, 2020 at 16:41
3
$\begingroup$

You may use NonlinearModelFit.

First observe from the shape of data (as defined in OP) that it follows some form of Log function.

ListPlot[data, PlotStyle -> Red]

Mathematica graphics

Then start with a basic log function. However, since the x values are large we can scale them down by the smallest value to assist the fit.

model = c Log[a x/Min[data[[All, 1]]] + b];
nlm = NonlinearModelFit[data, model, {a, b, c}, x]
nlm["AdjustedRSquared"]
FittedModel[15.032 Log[-9.23003*10^12+1.55212*10^7 x]]
0.999975

This gives a very good fit to the data.

Next plot data first in Show as the option settings of the first plot are used by default and we want to see the all of the data points. Also with NonlinearModelFit you should use the "Function" property for plotting.

Show[
 ListPlot[data, PlotStyle -> Red],
 Plot[Evaluate[nlm["Function"][x]], {x, 595070, 1039491}, PlotRange -> Full]
 ]

Mathematica graphics

The fit looks as good as "AdjustedRSquared" implies.

Hope this helps.

$\endgroup$
3
  • 1
    $\begingroup$ Note that using this parameterization of the log model results in an estimate of a perfect correlation between a and b: nlm["CorrelationMatrix"]//MatrixForm. Using either model = a + b Log[(x - c)/100000] or model = a1 + a2 Log[x + a3] results in estimators that are much less correlated (and more numerically stable). $\endgroup$
    – JimB
    Jul 1, 2020 at 5:23
  • $\begingroup$ Sorry, I am sure that my model is fine to represent these data points. When I re-scaled the data (dividing x by 1000000 , and dividing y by 100), DESMOS found the parameters, this means my model is representative for my original data! i.imgur.com/klJatY4.png. $\endgroup$ Jul 1, 2020 at 12:56
  • $\begingroup$ I should have added in my comment that it's not a critique of the kind of model fit. The log model as you proposed first clearly fits the data better than the model proposed by the OP and at minimum suggests that either the data or the theoretical model or both have problems. $\endgroup$
    – JimB
    Jul 1, 2020 at 16:25
2
$\begingroup$

Try starting simpler and work your way up. The "a" value you get above is very large. Your data is roughly constant. So start from there. Then add another term.

model = c;  

fit = FindFit[data, model, {c}, x;

    {c -> 398.163}

Show[Plot[Evaluate[model /. fit], {x, 595070, 1039491}], 
             ListPlot[data, PlotStyle -> Red]]

enter image description here

Or

FindFormula[data];

Will give a piecewise answer with the Log function (I've suppressed output, but don't in your notebook).

Show[Plot[g[x], {x, 595070, 1039491}, PlotRange -> {0, 1000}], 
    ListPlot[data, PlotStyle -> Red]]

enter image description here

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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