Computational Physics Approximation of a Functionschloerb/ph281/... · Computational Physics...
Transcript of Computational Physics Approximation of a Functionschloerb/ph281/... · Computational Physics...
![Page 1: Computational Physics Approximation of a Functionschloerb/ph281/... · Computational Physics Approximation of a Function. Outline Interpolation Problem Interpolation Schemes Nearest](https://reader034.fdocuments.in/reader034/viewer/2022051815/603dc2e0754e83289e101569/html5/thumbnails/1.jpg)
Approximation of a Function
Computational Physics
Approximation of a Function
![Page 2: Computational Physics Approximation of a Functionschloerb/ph281/... · Computational Physics Approximation of a Function. Outline Interpolation Problem Interpolation Schemes Nearest](https://reader034.fdocuments.in/reader034/viewer/2022051815/603dc2e0754e83289e101569/html5/thumbnails/2.jpg)
Outline
Interpolation Problem
Interpolation Schemes
Nearest Neighbor
Linear
Quadratic
Spline
Spline function in Python
![Page 3: Computational Physics Approximation of a Functionschloerb/ph281/... · Computational Physics Approximation of a Function. Outline Interpolation Problem Interpolation Schemes Nearest](https://reader034.fdocuments.in/reader034/viewer/2022051815/603dc2e0754e83289e101569/html5/thumbnails/3.jpg)
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
![Page 4: Computational Physics Approximation of a Functionschloerb/ph281/... · Computational Physics Approximation of a Function. Outline Interpolation Problem Interpolation Schemes Nearest](https://reader034.fdocuments.in/reader034/viewer/2022051815/603dc2e0754e83289e101569/html5/thumbnails/4.jpg)
Interpolation
Nearest Neighbor
Linear
Quadratic
Spline
t
y
![Page 5: Computational Physics Approximation of a Functionschloerb/ph281/... · Computational Physics Approximation of a Function. Outline Interpolation Problem Interpolation Schemes Nearest](https://reader034.fdocuments.in/reader034/viewer/2022051815/603dc2e0754e83289e101569/html5/thumbnails/5.jpg)
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.
![Page 6: Computational Physics Approximation of a Functionschloerb/ph281/... · Computational Physics Approximation of a Function. Outline Interpolation Problem Interpolation Schemes Nearest](https://reader034.fdocuments.in/reader034/viewer/2022051815/603dc2e0754e83289e101569/html5/thumbnails/6.jpg)
Interpolation
Nearest Neighbor
Linear
Quadratic
Spline
Where yi is the value in the table corresponding to timeclosest to t.
i is index to array
![Page 7: Computational Physics Approximation of a Functionschloerb/ph281/... · Computational Physics Approximation of a Function. Outline Interpolation Problem Interpolation Schemes Nearest](https://reader034.fdocuments.in/reader034/viewer/2022051815/603dc2e0754e83289e101569/html5/thumbnails/7.jpg)
Interpolation
Nearest Neighbor
Linear
Quadratic
Spline
t lies between tabularvalues: ti and ti+1
![Page 8: Computational Physics Approximation of a Functionschloerb/ph281/... · Computational Physics Approximation of a Function. Outline Interpolation Problem Interpolation Schemes Nearest](https://reader034.fdocuments.in/reader034/viewer/2022051815/603dc2e0754e83289e101569/html5/thumbnails/8.jpg)
Interpolation
Nearest Neighbor
Linear
Quadratic
Spline
![Page 9: Computational Physics Approximation of a Functionschloerb/ph281/... · Computational Physics Approximation of a Function. Outline Interpolation Problem Interpolation Schemes Nearest](https://reader034.fdocuments.in/reader034/viewer/2022051815/603dc2e0754e83289e101569/html5/thumbnails/9.jpg)
Interpolation
Nearest Neighbor
Linear
Quadratic
Spline
Cubic Function
Constraints to match first and second derivatives between segments
![Page 10: Computational Physics Approximation of a Functionschloerb/ph281/... · Computational Physics Approximation of a Function. Outline Interpolation Problem Interpolation Schemes Nearest](https://reader034.fdocuments.in/reader034/viewer/2022051815/603dc2e0754e83289e101569/html5/thumbnails/10.jpg)
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
![Page 11: Computational Physics Approximation of a Functionschloerb/ph281/... · Computational Physics Approximation of a Function. Outline Interpolation Problem Interpolation Schemes Nearest](https://reader034.fdocuments.in/reader034/viewer/2022051815/603dc2e0754e83289e101569/html5/thumbnails/11.jpg)
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
![Page 12: Computational Physics Approximation of a Functionschloerb/ph281/... · Computational Physics Approximation of a Function. Outline Interpolation Problem Interpolation Schemes Nearest](https://reader034.fdocuments.in/reader034/viewer/2022051815/603dc2e0754e83289e101569/html5/thumbnails/12.jpg)
Summary Example
LEGEND
NEARESTNEIGHBOR
LINEAR
SPLINE
TRUE
LEGEND
NEARESTNEIGHBOR
LINEAR
SPLINE
TRUE
![Page 13: Computational Physics Approximation of a Functionschloerb/ph281/... · Computational Physics Approximation of a Function. Outline Interpolation Problem Interpolation Schemes Nearest](https://reader034.fdocuments.in/reader034/viewer/2022051815/603dc2e0754e83289e101569/html5/thumbnails/13.jpg)
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