Interpolation comparison

1061 days ago by ncr006

def interpolating_polynomial(pts): R.<x> = PolynomialRing(RR) return R.lagrange_polynomial(pts) 
       
def spline_interpolation(pts): from sage.gsl.all import spline return spline(pts) 
       
pts = [(0,8),(1,12),(3,2),(4,6),(8,0)] 
       
Q = spline_interpolation(pts) f = interpolating_polynomial(pts) 
       
P1 = list_plot([(x,Q(x)) for x in srange(0,8,0.1)],color='red') P2 = list_plot([(x,f(x)) for x in srange(0,8,.1)]) P3 = list_plot(pts,color='green',size=100) show(P1+P2+P3) 
       

                                
                            

                                
sin(2.5) 
       
0.598472144103957
0.598472144103957
 
       

File: /nfs/software/sage-6.3/src/sage/gsl/interpolation.pyx

Type: <type ‘type’>

Definition: spline(x)

Docstring:

Create a spline interpolation object.

Given a list v of pairs, s = spline(v) is an object s such that s(x) is the value of the spline interpolation through the points in v at the point x.

The values in v do not have to be sorted. Moreover, one can append values to v, delete values from v, or change values in v, and the spline is recomputed.

EXAMPLES:

sage: S = spline([(0, 1), (1, 2), (4, 5), (5, 3)]); S
[(0, 1), (1, 2), (4, 5), (5, 3)]
sage: S(1.5)
2.76136363636...

Changing the points of the spline causes the spline to be recomputed:

sage: S[0] = (0, 2); S
[(0, 2), (1, 2), (4, 5), (5, 3)]
sage: S(1.5)
2.507575757575...

We may delete interpolation points of the spline:

sage: del S[2]; S
[(0, 2), (1, 2), (5, 3)]
sage: S(1.5)
2.04296875

We may append to the list of interpolation points:

sage: S.append((4, 5)); S
[(0, 2), (1, 2), (5, 3), (4, 5)]
sage: S(1.5)
2.507575757575...

If we set the n-th interpolation point, where n is larger than len(S), then points (0, 0) will be inserted between the interpolation points and the point to be added:

sage: S[6] = (6, 3); S
[(0, 2), (1, 2), (5, 3), (4, 5), (0, 0), (0, 0), (6, 3)]

This example is in the GSL documentation:

sage: v = [(i + sin(i)/2, i+cos(i^2)) for i in range(10)]
sage: s = spline(v)
sage: show(point(v) + plot(s,0,9, hue=.8))

We compute the area underneath the spline:

sage: s.definite_integral(0, 9)
41.196516041067...

The definite integral is additive:

sage: s.definite_integral(0, 4) + s.definite_integral(4, 9)
41.196516041067...

Switching the order of the bounds changes the sign of the integral:

sage: s.definite_integral(9, 0)
-41.196516041067...

We compute the first and second-order derivatives at a few points:

sage: s.derivative(5)
-0.16230085261803...
sage: s.derivative(6)
0.20997986285714...
sage: s.derivative(5, order=2)
-3.08747074561380...
sage: s.derivative(6, order=2)
2.61876848274853...

Only the first two derivatives are supported:

sage: s.derivative(4, order=3)
Traceback (click to the left of this block for traceback)
...

                                
                            

File: /nfs/software/sage-6.3/src/sage/gsl/interpolation.pyx

Type: <type ‘type’>

Definition: spline(x)

Docstring:

Create a spline interpolation object.

Given a list v of pairs, s = spline(v) is an object s such that s(x) is the value of the spline interpolation through the points in v at the point x.

The values in v do not have to be sorted. Moreover, one can append values to v, delete values from v, or change values in v, and the spline is recomputed.

EXAMPLES:

sage: S = spline([(0, 1), (1, 2), (4, 5), (5, 3)]); S
[(0, 1), (1, 2), (4, 5), (5, 3)]
sage: S(1.5)
2.76136363636...

Changing the points of the spline causes the spline to be recomputed:

sage: S[0] = (0, 2); S
[(0, 2), (1, 2), (4, 5), (5, 3)]
sage: S(1.5)
2.507575757575...

We may delete interpolation points of the spline:

sage: del S[2]; S
[(0, 2), (1, 2), (5, 3)]
sage: S(1.5)
2.04296875

We may append to the list of interpolation points:

sage: S.append((4, 5)); S
[(0, 2), (1, 2), (5, 3), (4, 5)]
sage: S(1.5)
2.507575757575...

If we set the n-th interpolation point, where n is larger than len(S), then points (0, 0) will be inserted between the interpolation points and the point to be added:

sage: S[6] = (6, 3); S
[(0, 2), (1, 2), (5, 3), (4, 5), (0, 0), (0, 0), (6, 3)]

This example is in the GSL documentation:

sage: v = [(i + sin(i)/2, i+cos(i^2)) for i in range(10)]
sage: s = spline(v)
sage: show(point(v) + plot(s,0,9, hue=.8))

We compute the area underneath the spline:

sage: s.definite_integral(0, 9)
41.196516041067...

The definite integral is additive:

sage: s.definite_integral(0, 4) + s.definite_integral(4, 9)
41.196516041067...

Switching the order of the bounds changes the sign of the integral:

sage: s.definite_integral(9, 0)
-41.196516041067...

We compute the first and second-order derivatives at a few points:

sage: s.derivative(5)
-0.16230085261803...
sage: s.derivative(6)
0.20997986285714...
sage: s.derivative(5, order=2)
-3.08747074561380...
sage: s.derivative(6, order=2)
2.61876848274853...

Only the first two derivatives are supported:

sage: s.derivative(4, order=3)
Traceback (most recent call last):
...
ValueError: Order of derivative must be 1 or 2.