6

I would like to fit a function of the form: $a \log(b x)$ to a set of data:

data = {{10, 10/153}, {100, 100/1833}, {200, 200/3814}, {300, 300/5847}, {500, 500/10006},
        {625, 625/12649}}

The input string to WolframAlpha: "log fit {{10, N[10/153,16]},{100,N[100/1833,16]},{200,N[200/3814,16]},{300,N[300/5847,16]},{500,N[500/10006,16]},{625,N[625/12649,16]}}" produces a nice logarithmic fit of this data. However, using FindFit with my copy of Mathematica 7.0 produces nonsensical values for $(a,b)$ and/or complex functions. How did WolframAlpha produce this fit, and how might I find this fit using my copy of Mathematica?

J. M.'s missing motivation
  • 124,525
  • 11
  • 401
  • 574
Ski
  • 63
  • 1
  • 3

2 Answers2

9

I'm using 8.0.4 and I get reasonable results (notice the constraint c>0) :

nlm = NonlinearModelFit[data, {a + b Log[c x], c > 0}, {a, b, c}, x] ;

nlm // Normal
(* 0.0740508 - 0.00391526 Log[1.0714 x] *)

FindFit[data, {a + b Log[c x], c > 0}, {a, b, c}, x]
(* {a -> 0.0740508, b -> -0.00391526, c -> 1.0714} *)

Show[Plot[nlm[x], {x, 10, 625}, PlotRange -> All], 
ListPlot[data, PlotStyle -> Directive[Red, PointSize[0.02]]]]

plot

The fit without an additive constant looks much worse :

nlm2 = NonlinearModelFit[data, {b Log[c x], c > 0}, {b, c}, x] ;

nlm2 // Normal
(* 0.0094653 Log[1.07146 x] *)

FindFit[data, {b Log[c x], c > 0}, {b, c}, x]
(* {b -> 0.0094653, c -> 1.07146} *)

Show[Plot[nlm2[x], {x, 10, 625}, PlotRange -> All], 
  ListPlot[data, PlotStyle -> Directive[Red, PointSize[0.02]]]]

plot2

b.gates.you.know.what
  • 20,103
  • 2
  • 43
  • 84
  • Right, I didn't properly constraint c>0. I suppose WolframAlpha is better at curve fitting somehow. Odd given that it should use the same functions as Mathematica... – Ski Nov 07 '12 at 09:59
  • fyi, simplifying the expression to just aLog[bx] produces basically the same residuals. – Ski Nov 07 '12 at 10:02
  • Identical results in 7.0.1, FWIW. @Ski: see my related answer here--you do not need any constraints using this approach. I've been working on a package to do this transformation automatically but have not had time to finish it so far. – Oleksandr R. Nov 07 '12 at 10:06
2

It's worth understanding that WolframAlpha is not a direct interface to Mathematica. Even if you type an exact Mathematica command, WolframAlpha will parse it and interpret it - perhaps in a different way. For example, the following two WolframAlpha inputs yield the exact same results:

enter image description here

Furthermore, none of those results matches the output of Solve[x^5-x-1==0,x] - so it's simply not the case that the Solve command is being called inside of WolframAlpha as you might expect.

Now, your input doesn't even correspond directly to a Mathematica command, so I'm really not sure what you're expecting. If you have basic enough input (which I think we do in this case) and you want to see what commands WolframAlpha sent to Mathemtatica to generate various outputs, you might try the following in Version 8:

WolframAlpha[
  "log fit {{10, N[10/153,16]},{100,N[100/1833,16]},{200,N[200/3814,16]},{300,N[300/5847,16]},{500,N[500/10006,16]},{625,N[625/12649,16]}}",
  "MathematicaForms"]

In the sea of output you'll notice the NonlinearModelFit command.

Mark McClure
  • 32,469
  • 3
  • 103
  • 161