# Fit values to a function and evaluate it

Hi,

I am totally new to math numerics and concepts of polynomials.

I would like to ask how can I fit an array of x and y values in order to get a function.

I attached the x and y datasets https://mega.nz/#!ppJW3axR!HJlOd0h0pEpVA1h-hRhO6vUjU6hK5tSoxntHby7-fbs
I tried this code:

``````double[] coef = MathNet.Numerics.Fit.Polynomial(x.ToArray(), y.ToArray(), 3);
System.Numerics.Complex n = MathNet.Numerics.Polynomial.Evaluate(2.2175597825, coef);
``````

First I passsed x and y arrays and then I would like to evaluate this function by passing variable 2.2175597825 I expect to get number 1, but the result is something very random.

Maybe conceptually I am doing something wrong, but essentially I would like to fit values to some function and after getting that function, pass the x values and get the result.

Could you help me with this?

Currently I can plot this list of values:

I think you need to use a different version of FIT. Probably you expected a regression, so you might try this:

#### PolynomialFit (Double[] x, Double[] y, int order, DirectRegressionMethod method)

BTW, the data sets did not load for me, and scales on the chart would be helpful.

Thank you for a reply @PVanderwaart .

How I can correctly write it since also I do not know which order I should put since I am not newbie:
Is it fine like this:

``````var p = MathNet.Numerics.Fit.Polynomial(x.ToArray(), y.ToArray(), 3, MathNet.Numerics.LinearRegression.DirectRegressionMethod.NormalEquations);
``````

I have uploaded the data set here since I was a new user, I could not do it.
In the graph the values of y goes from 0 to 90.

datasets.zip (919 Bytes)

After fitting the polynomial how can I evaluate the function?

For polynomial regression, the simplest way is actually to use the `Polynomial` type:

``````var polynomial = Polynomial.Fit(x, y, 3);
double r = polynomial.Evaluate(2.2175597825);
``````

However, your dataset does not look as if an order-3 polynomial would be a good fit:

Maybe x and y are reversed? Then it looks like this:

Thank you works perfectly. What does the number 3 means ? the large number I input the better results are.

That’s the order of the polynomial, see https://en.wikipedia.org/wiki/Polynomial for a few examples on the effect of the order.