0
$\begingroup$

My code is

data = {{0, 0.00355}, {6, 0.00343}, {9, 0.00331}, {48, 0.00319}, {173, 0.00308}, {200, 0.00308}};
fun= FindFit[data, a*Exp[b*(x)] + c, {a, b, c}, x]
p[x_] := a*Exp[b*x] /. fun

This gives me {a -> -3.2091*10^-91, b -> 1., c -> 0.003312}

whereas when I use desmos it gives me: desmos

where a=0.000437, b=-0.0598, and c=0.00310.

Desmos also gives the same answer as when I put it into wolfram alpha. I just want to know why my mathematica doesn't give me the same result and how I can make it give me the same as desmos. I have a lot of other data I would like to process in the same way and doing it all in desmos would be too painful.

$\endgroup$
2
  • $\begingroup$ Giving better starting values works: FindFit[data, a*Exp[b*(x)] + c, {a, {b, -1}, c}, x]. $\endgroup$
    – JimB
    Commented Aug 28, 2019 at 0:59
  • $\begingroup$ Try this NonlinearModelFit[data, a Exp[b x] + c, {a, b, c}, x, Method -> {NMinimize, Method -> "DifferentialEvolution"}, MaxIterations -> 1000] $\endgroup$ Commented Aug 28, 2019 at 2:35

1 Answer 1

3
$\begingroup$

It just failed to converge (or rather, it converged to a bad fixed point). Use Exp[-b x] instead of Exp[b x]; this time it works as expected:

NonlinearModelFit[data, a Exp[-b x] + c, {a, b, c}, x]
(* {a -> 0.000436835, b -> 0.0584236, c -> 0.0031018} *)

Also, use NonlinearModelFit instead of FindFit (see here for more details).

$\endgroup$
5
  • $\begingroup$ I pasted your code in and it gave me: General::munfl: -7.9988*10^-8/-6.277922286640201*10^7320 is too small to represent as a normalized machine number; precision may be lost. General::munfl: 3.9994*10^-8/3.138961143320101*10^7320 is too small to represent as a normalized machine number; precision may be lost. General::munfl: -6.88556*10^-8/-1.864483853397434*10^5969 is too small to represent as a normalized machine number; precision may be lost. General::stop: Further output of General::munfl will be suppressed during this calculation. $\endgroup$ Commented Aug 28, 2019 at 0:01
  • $\begingroup$ @Toadfuture huh, that's weird. Can you restart your kernel (run Quit[]) and try again? It works for me... $\endgroup$ Commented Aug 28, 2019 at 0:05
  • $\begingroup$ I quit the kernel and the errors still showed up but the fitted model output was what I wanted so I guess that's all I need. Thank you so much for your help! $\endgroup$ Commented Aug 28, 2019 at 0:08
  • $\begingroup$ @Toadfuture A few messages about underflow may not be a serious problem: it just indicates that machine numbers were flushed to zero during intermediate calculations, which can happen when you use exponents. If the returned fit is good you don't need to worry about it. $\endgroup$ Commented Aug 28, 2019 at 8:41
  • $\begingroup$ @AccidentalFourierTransform Method->"NMinize"although solves the first case "...Exp[+b x]": NonlinearModelFit[data, a Exp[ b x] + c, {a, b, c}, x, Method -> "NMinimize"] $\endgroup$ Commented Aug 28, 2019 at 12:56

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.