# Quartic function from data [closed]

I am studying something about interpolation.

I have a function that is a power x function (f[x_]:=17000 x^-1.85).

From this function I got a list with coordinates for X and Y (data2)

With these data I tried to get a quartic function, but this failed, showing an error.

I donÂ´t know if the order as I created the code generates this. Maybe it is that…

The data presented in data1 were created just to an application test. One can see that I made an approximation manually. With these data I got what I wanted.

The phrase error is this: “General::ivar: 20 is not a valid variable. >>”

Manual test:

data1={{20,66.6106},{30,31.4612},{40,18.4773},{50,12.228},{60,8.72708}}
eq1=Fit[data1,{1,x,x^2,x^3,x^4},x]
Plot[eq1,{x,20,60}]

Real test:

f[x_]:=17000x^-1.85
x={20,30,40,50,60}
y=f[#]&/@x
data2={x[[#]],y[[#]]}&/@Range[5]
eq2=Fit[data2,{1,x,x^2,x^3,x^4},x]

=================

=================

2

=================

Your problem stems from the fact that you are using x in two different roles, i.e. as a container for a list of values first, and then again as the independent variable in your Fit expression, where it should NOT have a value associated with it.

You could Clear[x] before the Fit to fix that issue, but more generally your code can be simplified considerably. For instance:

Clear[f, x, y, data2]
f[x_] := 17000 x^-1.85
data2 = Table[{x, f[x]}, {x, 20, 60, 10}];
eq2 = Fit[data2, {1, x, x^2, x^3, x^4}, x]

(* Out: 322.355 – 23.0869 x + 0.68087 x^2 – 0.00924801 x^3 + 0.0000476869 x^4 *)

You can compare the fit to the data:

Plot[eq2, {x, 20, 60}, Epilog -> {PointSize[0.02], Point[data2]}]

You can avoid the use of global variables x and y altogether.

data2 = {#, f[#]} & /@ Range[20, 60, 10];
Fit[data2, {1, x, x^2, x^3, x^4}, x]

322.355 – 23.0869 x + 0.68087 x^2 – 0.00924801 x^3 + 0.0000476869 x^4

Is there any function that specifically do this? Something of Manipulation Lists, as (Join, Union,…)
– LMC
Jun 30 at 21:03

1

@LeandroMacieldeCarvalho I’m afraid that I don’t understand what you refer to with “this”. The creation of a table of values? Then you should look into Table. Note that the example you provided is not really a fit, but rather an interpolation, since you have exactly as many points as you have free parameters; so perhaps you might want to look at InterpolatingPolynomial as well.
– MarcoB
Jun 30 at 21:41

I commented on data2
– LMC
Jun 30 at 22:03