We initialize the data we want to interpolate:

```
x = 0:0.5:10;
y = sin(x/2);
```

This means the underlying function for the data in the interval [0,10] is sinusoidal. Now the coefficients of the approximating polynómials are being calculated:

```
p1 = polyfit(x,y,1);
p2 = polyfit(x,y,2);
p3 = polyfit(x,y,3);
p5 = polyfit(x,y,5);
p10 = polyfit(x,y,10);
```

Hereby is `x`

the x-value and `y`

the y-value of our data points and the third number is the order/degree of the polynomial. We now set the grid we want to compute our interpolating function on:

```
zx = 0:0.1:10;
```

and calculate the y-values:

```
zy1 = polyval(p1,zx);
zy2 = polyval(p2,zx);
zy3 = polyval(p3,zx);
zy5 = polyval(p5,zx);
zy10 = polyval(p10,zx);
```

One can see that the approximation error for the sample gets smaller when the degree of the polynomial increases.

While the approximation of the straight line in this example has larger errors the order 3 polynomial approximates the sinus function in this intervall relatively good.

The interpolation with order 5 and order 10 polynomials has almost no apprroximation error.

However if we consider the out of sample performance one sees that too high orders tend to overfit and therefore perform badly out of sample. We set

```
zx = -10:0.1:40;
p10 = polyfit(X,Y,10);
p20 = polyfit(X,Y,20);
```

and

```
zy10 = polyval(p10,zx);
zy20 = polyval(p20,zx);
```

If we take a look at the plot we see that the out of sample performance is best for the order 1

and keeps getting worse with increasing degree.