# FindFit as a function [closed]

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.

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.

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