# How can I find a function that fits some points? [closed]

I have some points and want to find a function from them. For example, $(-2,4),(0,0),(1,1),(5,25)$. I don't have a function that generates the points.

-

## closed as too localized by Ajasja, Oleksandr R., Artes, Yves Klett, m_goldbergApr 26 '13 at 20:30

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.If this question can be reworded to fit the rules in the help center, please edit the question.

Look up Interpolation[] or FindSequenceFunction[]. –  Ｊ. Ｍ. Apr 26 '13 at 16:03
Or, if you have some idea of the form of the function, look at FindFit, LinearModelFit, and NonlinearModelFit. –  rcollyer Apr 26 '13 at 16:06
Do you just want to fit those points or do you want a function that is good for interpolaton in between? The appropriate fit can be quite different. –  george2079 Apr 26 '13 at 18:32

Try Interpolation; the second form should do exactly what you want. This will return an InterpolatingFunction object, which you can apply to numerical arguments just like any other function.

if = Interpolation[{{-2, 4}, {0, 0}, {1, 1}, {5, 25}}]


I have if[3.2] returning 10.24.

-
sorry ,i don't understand.if[3.2] , 10.24 what are them mean? –  amir Apr 26 '13 at 18:54
evaluating the interpolating function if at 3.2 returns 10.24. –  s0rce Apr 26 '13 at 19:09
how can i obtain this evaluating? sorry because i know a little programming –  amir Apr 26 '13 at 19:23
@amir, just type in if[3.2] and hit Shift + Enter. –  Pillsy Apr 26 '13 at 19:35
Look up Plot in the documentation. –  s0rce Apr 26 '13 at 20:29

If you can hazard a guess as to the form of the function, then you can fit almost anything. For example, here is your data:

points = {{-2, 4}, {0, 0}, {1, 1}, {5, 25}}


Let's see how well a second order polynomial fits:

poly = NonlinearModelFit[points, a x^2 + b x + c, {a, b, c}, x]


The answer is: it fits really well! You can also pick other functional forms, to see how well they fit. You can read about this kind of function-finding in the help file for NonlinearModelFit section of the help.

Here you can see how well the function fits the points by plotting:

Show[ListPlot[points], Plot[poly[x], {x, -2, 5}]]


-
Since this is a linear model, one can even use LinearModelFit... –  Oleksandr R. Apr 26 '13 at 16:39
my point is {0.652121,0.00259997},{0.653734,0.0398411},{0.655347,0.0770823},{0.665245,0.1161‌​71},{0.672903,0.1683},{0.680445,0.229739},{0.686049,0.280009},{0.695993,0.315373}‌​,{0.713827,0.384244},{0.727623,0.443811},{0.747697,0.499642},{0.76994,0.548021},{‌​0.792253,0.590813},{0.822595,0.655937},{0.846962,0.700588},{0.89182,0.76755},{0.9‌​40832,0.834504},{0.994044,0.897727},{1.03286,0.949802},{1.07592,0.99442} and i guess poly = NonlinearModelFit[points, a x^5 + b x^4 + c x^3+d x^2+ex+f, {a, b, c,d,e,f}, x] but i had this error: Syntax::sntxf: "points = " cannot be followed by .what is my fualt? –  amir Apr 26 '13 at 18:51
You are missing a curly parenthesis - look carefully at the way I defined points -- it is a list of pairs. –  bill s Apr 26 '13 at 19:16
@amir points = {{0.652121,0.00259997},{0.653734,0.0398411},{0.655347,0.0770823},{0.665245,0.116‌​171},{0.672903,0.1683},{0.680445,0.229739},{0.686049,0.280009},{0.695993,0.315373‌​},{0.713827,0.384244}, {0.727623, 0.443811},{0.747697,0.499642},{0.76994,0.548021}, {0.792253,0.590813},{0.822595,0.655937},{0.846962,0.700588},{0.89182,0.76755},{0‌​.940832, 0.834504},{0.994044,0.897727}, {1.03286, 0.949802},{1.07592,0.99442}}; together with nlm = NonlinearModelFit[points,a+b x+c x^2+d x^3+e x^4+f x^5,{a,b,c,d,e,f},x] and Show[ListPlot@points,Plot[nlm[x], {x,0.6,1.1}]] should do the job. –  chromate Apr 26 '13 at 19:28
You need to look more carefully. There are many different kinds of parenthesis and brackets and they all mean different things. You have [{... it should be {{... –  bill s Apr 26 '13 at 20:26