How obtain a (accurate) function from this graph with these points?

245 Views Asked by At

I need obtain the function from 0 to 20 from this graph:

enter image description here

I have the even numbers in the {x, f(x)} format: {0, 0}, {2, 1.8}, {4, 2}, {6, 4}, {8,4}, {10,6}, {12,4}, {14,3.6},{16,3.4}, {18,2.8}, {20,0}

I tried using interpolation from Wolfram Alpha but It got the Runge's phenomenon as you can see in this link http://www.wolframalpha.com/input/?i=interpolating+polynomial+{0%2C+0}%2C+{2%2C+1.8}%2C+{4%2C+2}%2C+{6%2C+4}%2C+{8%2C4}%2C+{10%2C6}%2C+{12%2C4}%2C+{14%2C3.6}%2C{16%2C3.4}%2C+{18%2C2.8}%2C+{20%2C0}

So, if anyone could give me a relatively accurate approximation I would be very grateful.

Thank you for reading this!

1

There are 1 best solutions below

3
On BEST ANSWER

A cubic spline does the job:

spline

There are infinitely many programs for spline interpolation. I rolled my own, however (in Scilab). It computes cubic spline by considering it as a "correction" to piecewise linear interpolation.

a = 0
b = 20
y = [0 1.8 2 4 4 6 4 3.6 3.4 2.8 0]
n = length(y)-1
h = (b-a)/n
jumps = diff(y,2)/h
A = (h/6)*(diag(4*ones(1,n-1))+diag(ones(1,n-2),1)+diag(ones(1,n-2),-1))
z = [0,-jumps/A,0] 
allx = []
spl = []
for i=1:n  
   xL = a+h*(i-1)
   xR = a+h*i
   x = linspace(xL,xR,100)
   linear = y(i)*(xR-x)/h + y(i+1)*(x-xL)/h
   correction = ((z(i+1)+2*z(i))*(xR-x)+(2*z(i+1)+z(i))*(x-xL)).*(xR-x).*(x-xL)/(6*h)
   allx = [allx, x]   
   spl = [spl, linear+correction] 
end
plot(allx, spl) 
plot(a:h:b, y, 'r*')

A spline is a piecewise defined function; instead of a single complicated formula you have a bunch of simple formulas. Let $(x_1,y_1),\dots, (x_{11},y_{11})$ be the points in your post. The code calculates numbers $z_1,\dots,z_{11}$ which you can get by simply typing z after running the program. The formula for the spline is as follows: when $x_i\le x\le x_{i+1}$, $$\begin{split} y &= y_i\frac{x_{i+1} -x}{h} + y_{i+1}\frac{x-x_i}{h} \\ &+ \frac{(x-x_i)(x_{i+1}-x)}{6h}((z_{i+1}+2 z_i) (x_{i+1} -x)+ (2 z_{i+1}+z_i) (x-x_i)) \end{split}$$ where $h=2$ (space between $x$-coordinates of the points). The first line is the piecewise linear interpolant. The second line is the cubic correction term. It must vanish at the nodes of interpolation, hence the factor $(x-x_i)(x_{i+1}-x)$. This leaves only a linear factor of correction term to figure out, which is done by using the condition that the first derivative of the correction term must jump at the interpolation node to negate the jump of the slope of the piecewise linear interpolant.

The numbers $z_i$ are actually $y''(x_i)$, as you can check by taking the derivative of $y$ above. This is a natural cubic spline, for which $y''$ vanishes at the endpoints.