5
$\begingroup$

Beginning with five ordered pairs I made an interpolation, obtaining an a polynomial expression.

I got several other points with equal spacing from the polynomial expression. Are there any commands more specific to doing this operation?

Here is my code.

data = {{0, 0}, {12, 25}, {27, 52}, {33, 45}, {42, 31}};
eq = Fit[data, {1, x, x^2, x^3, x^4}, x];
xPoint = Range[0, 50, 2];
yPoint = Table[eq, {x, xPoint}];
positions = Transpose[{xPoint, yPoint}];
ListPlot[positions]
$\endgroup$
1

3 Answers 3

2
$\begingroup$

You can start by simplifying the code:

data = {{0, 0}, {12, 25}, {27, 52}, {33, 45}, {42, 31}};

eq = Fit[data, {1, x, x^2, x^3, x^4}, x];

ListPlot[Transpose[{Range[0, 50, 2], 
   Table[eq, {x, Range[0, 50, 2]}]}]]
$\endgroup$
2
  • $\begingroup$ You haven't defined xPoint. $\endgroup$
    – C. E.
    Sep 24, 2016 at 21:17
  • $\begingroup$ Now yes is helpful. Unfortunately came too late. $\endgroup$
    – JPeter
    Sep 26, 2016 at 19:39
7
$\begingroup$

I am assuming you want the interpolated points for further processing, because there is little reason to make the interpolation otherwise. I would proceed as follows:

  1. I get the fitted polynomial as function f. I use Block to protect the variable x from any previous assignment.

    data = {{0, 0}, {12, 25}, {27, 52}, {33, 45}, {42, 31}};
    Block[{x}, f[x_] = Fit[data, {1, x, x^2, x^3, x^4}, x]];
    
  2. I compute the points directly in a table.

    pts = Table[{i, f[i]}, {i, 0, 50, 2}];
    
  3. I plot the interpolated points along with the data to check my work.

    ListPlot[pts, Prolog -> {Red, AbsolutePointSize[8], Point[data]}]
    

plot

The extrapolation of the data beyond 42 is suspect.

$\endgroup$
2
  • $\begingroup$ Extrapolation was on purpose $\endgroup$
    – JPeter
    Sep 24, 2016 at 22:01
  • $\begingroup$ @user43142. I don't doubt it, but the extrapolation is simply not trustworthy unless you have some reason beyond the five data points to expect the sharp upward turn beyond 42. $\endgroup$
    – m_goldberg
    Sep 24, 2016 at 22:07
5
$\begingroup$

Table is enough:

Table[{x, eq}, {x, 1, 50, 2}] // ListPlot

Mathematica graphics

If your intention is to plot the points then you can also use Mesh:

data = {{0, 0}, {12, 25}, {27, 52}, {33, 45}, {42, 31}};
eq = Fit[data, {1, x, x^2, x^3, x^4}, x];

pl = Plot[
  Evaluate[eq], {x, 0, 50},
  PlotStyle -> None,
  MeshStyle -> Directive[PointSize[Medium], ColorData[97][1]],
  Mesh -> 25
  ]

Mathematica graphics

You can also retrieve the actual points like this:

Cases[Normal[pl], Point[pts_] :> pts, Infinity]

although this is a roundabout way to get the points, not really simpler than what you used. However, as described here, this could be the way to go if you want to generate points that are equally spaced along the curve rather than equally spaced on the x axis.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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