computational physics approximation of a functionschloerb/ph281/... · computational physics...

Post on 09-Oct-2020

9 Views

Category:

Documents

1 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Approximation of a Function

Computational Physics

Approximation of a Function

Outline

Interpolation Problem

Interpolation Schemes

Nearest Neighbor

Linear

Quadratic

Spline

Spline function in Python

Calculations result in Tables

Index T Y

1 0 02 1 0.843 2 0.914 3 0.145 4 -0.766 5 -0.967 6 -0.288 7 0.669 8 0.9910 9 0.4111 10 -0.54

Interpolation used to find value between calculated points

Interpolation

Nearest Neighbor

Linear

Quadratic

Spline

t

y

Basis

Taylor Series Expansion of a function

We can expand a function, y(t), about a specific point, t0 according to:

The Taylor Series is used to approximate behavior of functions with a few terms.

Approximation gets better with fewer terms as (t-t0) becomes small.

Interpolation

Nearest Neighbor

Linear

Quadratic

Spline

Where yi is the value in the table corresponding to timeclosest to t.

i is index to array

Interpolation

Nearest Neighbor

Linear

Quadratic

Spline

t lies between tabularvalues: ti and ti+1

Interpolation

Nearest Neighbor

Linear

Quadratic

Spline

Interpolation

Nearest Neighbor

Linear

Quadratic

Spline

Cubic Function

Constraints to match first and second derivatives between segments

Constructing the Spline

. . .

t1 t2 t3 t4 tNtN-1tN-2

p1 p2

p3

pN-1

y1

yNN points: ti, yi N-1 cubic polynomials: pi

require 4(N-1)coefficients

for cubic polynomials 1 through N-2:pi(ti+1) = yi+1 function reproduces valuepi(ti+1) = pi+1(ti+1) continuity conditionp'i(ti+1) = p'i+1(ti+1) continuity of 1st derivativep''i(ti+1) = p''i+1(ti+1) continuity of 2nd derivative

pN-2

Constructing the Spline(continued)

Constraint equations give 4(N-2) equations todetermine 4(N-1) unknown coefficients

Need 4 more constraints. Two are obvious:

p1(t1) = y1

pN(tN) = yN

to these we add “Natural Spline” conditions of

p''1(t1) = 0p''N(tN) = 0

Now have enough constraints to determine allpolynomial segments pi

Summary Example

LEGEND

NEARESTNEIGHBOR

LINEAR

SPLINE

TRUE

LEGEND

NEARESTNEIGHBOR

LINEAR

SPLINE

TRUE

Using pythoninterpolation

import matplotlib.pyplot as pl import numpy as np from scipy.interpolate import interp1d

# make our tabular values x_table = np.arange(11) y_table = np.sin(x_table)

# linearly interpolate x = np.linspace(0.,10.,201)

# here we create linear interpolation function linear = interp1d(x_table,y_table,'linear')

# apply and create new array y_linear = linear(x)

# plot results to illustrate pl.ion() pl.plot(x_table,y_table,'bo',markersize=20) pl.plot(x,y_linear,'r') pl.plot(x,np.sin(x),'g') pl.legend(['Data','Linear','Exact'],loc='best') pl.xlabel('X') pl.ylabel('Y')

import matplotlib.pyplot as pl import numpy as np from scipy.interpolate import interp1d

# make our tabular values x_table = np.arange(11) y_table = np.sin(x_table)

# linearly interpolate x = np.linspace(0.,10.,201)

# here we create linear interpolation function linear = interp1d(x_table,y_table,'linear')

# apply and create new array y_linear = linear(x)

# plot results to illustrate pl.ion() pl.plot(x_table,y_table,'bo',markersize=20) pl.plot(x,y_linear,'r') pl.plot(x,np.sin(x),'g') pl.legend(['Data','Linear','Exact'],loc='best') pl.xlabel('X') pl.ylabel('Y')

Interpolation function is in theScipy package. Import it here.

Create table of x,y values.

New x values where we want y

Invoke the interpolationfunction interp1d

Compute new y

Plot results

top related