• Keine Ergebnisse gefunden

Worksheet 5: Spline Interpolation Solutions

N/A
N/A
Protected

Academic year: 2021

Aktie "Worksheet 5: Spline Interpolation Solutions"

Copied!
5
0
0

Wird geladen.... (Jetzt Volltext ansehen)

Volltext

(1)

Physik auf dem Computer SS 2017

Worksheet 5: Spline Interpolation Solutions

June 13, 2017

Author: Johannes Zeman Task 5.1: Cubic and Quadratic Splines (6 points)

In this task, you will spline-interpolate the following functions on the specified domains:

Name Definition Domain

Sine Function f (x) = sin x [0, 2π]

Runge Function g(x) = 1+x 1

2

[−5, 5]

Lennard-Jones Function h(x) = x −12x −6 [1, 5]

The python script ws5.py provides a demo which uses the class scipy.interpolate.interp1d to com- pute interpolating splines.

5.1.1 (2 points) Write a class CubicSplineInterpolation which implements cubic spline interpo- lation. As in the previous worksheet, the class shall provide a constructor __init__(self, ...) to initialize the interpolation and a method __call__(self,x) to compute the value of the in- terpolating function at x . On the boundaries, set the second derivative of the spline to zero.

Use the class to create the same plots as the demo does. Note that the splines in this class will not be identical to the splines in the demo as SciPy uses different boundary conditions.

Hint The constructor has to compute the spline coefficients by first generating the defining linear equations (see lecture notes pp. 35-36, especially the equation after equation 2.8) and then solving them using scipy.linalg.solve .

5.1.2 (2 points) Derive the equations for the quadratic spline, where the spline polynomial is defined as

S j (x) = a j + b j (x − x j ) + c j (x − x j ) 2 . (1) The conditions for the quadratic spline are:

It matches the function values at the supporting points x j and x j+1 . The first derivative of the spline at x j+1 is the same for S j and S j+1 .

In your implementation, the first derivative should vanish at the lower boundary.

5.1.3 (2 points) Write a class QuadraticSplineInterpolation which implements the quadratic

spline interpolation. Redo the same plots as in task 5.1.1 with quadratic splines.

(2)

Solution of Task 5.1.1 (Cubic Spline): Derivation of Defining Linear Equations

A cubic spline interpolation connecting n points (x j |y j ), j ∈ [0, 1, ...n − 1] can be expressed as splines S j (x) of the general form

S j (x) = a j + b j (x − x j ) + c j (x − x j ) 2 + d j (x − x j ) 3 , j ∈ [0, 1, ..., n − 1] , (2) where each segment S j (x) is the interpolating third-order polynomial on the interval [x j , x j+1 ].

The corresponding first and second derivatives with respect to x are thus d

dx S j (x) = b j + 2c j (x − x j ) + 3d j (x − x j ) 2 (3) d 2

dx 2 S j (x) = 2c j + 6d j (x − x j ) . (4)

Each segment has to fulfill the following conditions:

i) S j (x j ) = y j , j ∈ [0, 1, ..., n − 1]

ii) S j+1 (x j+1 ) = S j (x j+1 ) , j ∈ [0, 1, ..., n − 2]

iii) dx d S j+1 (x j+1 ) = dx d S j (x j+1 ) , j ∈ [0, 1, ..., n − 2]

iv) dx d

22

S j+1 (x j+1 ) = dx d

22

S j (x j+1 ) , j ∈ [0, 1, ..., n − 2]

Additionally, it has to fulfill the boundary conditions v) dx d

22

S 0 (x 0 ) = A

vi) dx d

22

S n−2 (x n−1 ) = B .

Here, we impose the so-called natural boundary conditions by requiring A = B = 0.

Using these conditions, the defining linear equations for the coefficients a j , b j , c j , and d j are derived in the following.

For the coefficients a j , it follows from condition i) together with Eq. (2) that

a j = y j . (5)

Applying condition iv) to Eq. (4) yields

2c j+1 + 6d j+1 (x j+1x j+1 )

| {z }

=0

= 2c j + 6d j (x j+1x j )

d j = c j+1c j

3(x j+1x j ) . (6)

Furthermore, applying conditions i) and ii) to Eq. (2) and inserting Eq. (5) and (6) yields y j+1 = y j + b j (x j+1x j ) + c j (x j+1x j ) 2 + d j (x j+1x j ) 3

b j = y j+1y j x j+1x j − 1

3 (x j+1x j )(2c j + c j+1 ) . (7)

Analogously, by applying condition iii) on Eq. (3) and inserting Eq. (6), it follows that b j+1 = b j + (c j + c j+1 )(x j+1x j ) .

2

(3)

Plugging in Eq. (7) leads to y j+2y j+1

x j+2x j+1

= y j+1y j

x j+1x j

+ 1

3 (c j (x j+1x j ) + 2c j+1 (x j+2x j ) + c j+2 (x j+2x j+1 ))

Inserting Eq. (7), bringing all c-dependent terms to the left side, dividing by (x j+2x j ) and shifting the index j → (j − 1) yields the system of linear equations

α j c j−1 + 2c j + β j c j+1 = 3γ j (8) with

α j := x jx j−1

x j+1x j−1

, β j := x j+1x j x j+1x j−1

, and γ j :=

y

j+1

−y

j

x

j+1

−x

j

x y

j

−y

j−1

j

−x

j−1

x j+1x j−1

. In order to solve the equation system, we write Eq. (8) in matrix notation:

α 1 2 β 1 0

α 2 2 β 2 . .. . .. . ..

0 α n−2 2 β n−2

c 1 c 2 .. . c n−2

=

 3γ 12 .. . 3γ n−2

(9)

Yet, there are two equations missing in order to solve Eq. (9), which are the boundary conditions v) and vi). With natural boundary conditions A = B = 0, applying these conditions to Eq. (4) together with Eq. (6) yields

c 0 = c n−1 = 0 .

Adding these equations to Eq. (9) finally leads to the complete system of linear equations:

1 0 0

α 1 2 β 1 α 2 2 β 2

. .. . .. . ..

α n−2 2 β n−2

0 0 1

c 0 c 1 c 2

.. . c n−2

c n−1

=

 0 3γ 1 3γ 2

.. . 3γ n−2

0

(10)

Once the system of linear equations is solved, the coefficients b j and d j are obtained from Eq. (7) and (6), respectively.

For the implementation of the class CubicSplineInterpolation , see the Python script ws5_solution.py .

(4)

Solution of Task 5.1.2 (Quadratic Spline): Derivation of Defining Linear Equations A quadratic spline interpolation connecting n points (x j |y j ), j ∈ [0, 1, ...n − 1] can be expressed as splines S j (x) of the general form

S j (x) = a j + b j (x − x j ) + c j (x − x j ) 2 , j ∈ [0, 1, ..., n − 1] , (11) where each segment S j (x) is the interpolating second-order polynomial on the interval [x j , x j+1 ].

The corresponding first derivatives with respect to x are thus d

dx S j (x) = b j + 2c j (x − x j ) . (12)

Each segment has to fulfill the following conditions:

i) S j (x j ) = y j , j ∈ [0, 1, ..., n − 1]

ii) S j+1 (x j+1 ) = S j (x j+1 ) , j ∈ [0, 1, ..., n − 2]

iii) dx d S j+1 (x j+1 ) = dx d S j (x j+1 ) , j ∈ [0, 1, ..., n − 2]

Additionally, it has to fulfill the boundary condition iv) dx d S 0 (x 0 ) = A .

We impose natural boundary conditions by setting A = 0.

Using these conditions, the defining linear equations for the coefficients a j , b j , and c j are derived in the following.

For the coefficients a j , it follows from condition i) together with Eq. (11) that

a j = y j . (13)

Applying condition iii) to Eq. (12) yields

b j+1 + 2c j+1 (x j+1x j+1 )

| {z }

=0

= b j + 2c j (x j+1x j )

c j = b j+1b j

2(x j+1x j ) . (14)

Furthermore, applying conditions i) and ii) to Eq. (11) and inserting Eq. (13) and (14) yields y j+1 = y j + b j (x j+1x j ) + c j (x j+1x j ) 2

b j + b j+1 = 2 y j+1y j

x j+1x j . (15)

With an index shift j → (j − 1) we write this as

b j−1 + b j = 2γ j (16)

with

γ j := y jy j−1

x jx j−1

.

4

(5)

In order to solve the equation system, we write Eq. (16) in matrix notation:

1 1 0

1 1

. .. ...

0 1 1

b 1

b 2

.. . b n−1

=

 2γ 1

2γ 2

.. . 2γ n−1

(17)

Yet, there is one equation missing in order to solve Eq. (17), which is the boundary condition iv).

With the natural boundary condition A = 0, applying this condition to Eq. (12) yields b 0 = 0 .

Adding this equation to Eq. (17) finally leads to the complete system of linear equations:

1 0 0

1 1

1 1

. .. ...

0 1 1

b 0 b 1

b 2 .. . b n−1

=

 0 2γ 1

2 .. . 2γ n−1

(18)

Referenzen

ÄHNLICHE DOKUMENTE

a certain graph, is shown, and he wants to understand what it means — this corre- sponds to reception, though it involves the understanding of a non-linguistic sign;

Output For every case, the output should contain one integer number on a separate line – the maximal total energy yield of that case. The output contains one line for

A deoxyribodipyrimidine photolyase family protein 559 showed highest induction in both origins after the 2 °C UVR treatment, 560 X-ray repair cross-complementing protein 6

To match the market stochasticity we introduce the new market-based price probability measure entirely determined by probabilities of random market time-series of the

2 In particular we do not allow that all voters cast abstain/negative votes for all candidates. With this requirement we avoid stating that all candidates must be elected in case

Tarang, Stability of the spline collocation method for second order Volterra integro-differential equations, Mathematical Modelling and Analysis, 9, 1, 2004, 79-90....

This exercise sheet aims to assess your progress and to explicitly work out more details of some of the results proposed in the previous lectures. Then (A/I, q) is a Banach algebra

This exercise sheet aims to assess your progress and to explicitly work out more details of some of the results proposed in the previous lectures. Please, hand in your solutions