0
$\begingroup$

I know for Fit one can use e.g.

data = Table[x^2, {x, 1, 10}]

(* ==> {1, 4, 9, 16, 25, 36, 49, 64, 81, 100} *)

f[x_] = Fit[data, {1, x}, x]

the result is a function. What is the corresponding code for FindFit? I get an error when trying a similar thing there.

Edit: actually I get the error

"Tag Plus in (18.4685 -63.5287\ x+49.1388\ \
x^2)\[InvisibleApplication](x_) is Protected."

EDIT2: Actually it works it's just that I had previous definition messing things up. So there is no problem here, I can close or delete this if you wish.

$\endgroup$
2
$\begingroup$

Well, this:

f[x_] := a x + b
g[x_] := g[x] = f[x] /. FindFit[data, f[x], {a, b}, x]
g[x]

-22. + 11. x

The memoization should be used in order not to fit repeatedly every time g[x] is called.

$\endgroup$
  • $\begingroup$ Every time you evaluate g[x] it will be fitting again and again? $\endgroup$ – BlacKow Sep 30 '16 at 22:30
  • $\begingroup$ Previous definitions messed things up, see edit. $\endgroup$ – Your Majesty Sep 30 '16 at 22:33
  • 1
    $\begingroup$ @BlacKow Good point, should be g[x_]:=g[x]=... I think. $\endgroup$ – corey979 Sep 30 '16 at 22:51
  • $\begingroup$ @corey979 you probably want to edit your answer to include memoization $\endgroup$ – BlacKow Sep 30 '16 at 22:53

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