Chap9 CurveFitting Interpolation
Chap9 CurveFitting Interpolation
February 8, 2023
ℎ(𝑘𝑚) 𝜌
0.000 1.0000
1.525 0.8617
3.050 0.7385
4.575 0.6292
6.100 0.5328
7.625 0.4481
1
ℎ(𝑘𝑚) 𝜌
9.150 0.3741
h = array([0,1.525,3.050,4.575,6.10,7.625,9.150])
rho = array([1,0.8617,0.7385,0.6292,0.5328,0.4481,0.3741])
plot(h,rho)
show()
h = array([0,1.525,3.050,4.575,6.10,7.625,9.150])
rho = array([1,0.8617,0.7385,0.6292,0.5328,0.4481,0.3741])
def fquad(h,a,b,c):
return a*h**2+b*h+c
yquad,errq = curve_fit(fquad,h,rho)
a=yquad[0]
b=yquad[1]
2
c=yquad[2]
print('a = ',a)
print('b = ',b)
print('c = ',c)
#print('error = ',errq)
a = 0.0027632101009355467
b = -0.09344730684922747
c = 0.9988952381654874
rho at h=8.0 km is 0.4281622298315426
1 INTERPOLATION
The package scipy.interpolate contains a large variety of functions and classes for interpolation and
splines in one and more dimensions. Some of the more important are described in this section.
ℎ(𝑘𝑚) 𝜌
0.000 1.0000
1.525 0.8617
3.050 0.7385
4.575 0.6292
6.100 0.5328
7.625 0.4481
9.150 0.3741
h = array([0,1.525,3.050,4.575,6.10,7.625,9.150])
rho = array([1,0.8617,0.7385,0.6292,0.5328,0.4481,0.3741])
3
f_linear = interp1d(h, rho)
f_cubic = interp1d(h, rho, kind='cubic')
f_nearest = interp1d(h, rho, kind='nearest')
f_quadratic = interp1d(h, rho, kind='quadratic')
f_slinear = interp1d(h, rho,kind='slinear')
f_zero = interp1d(h, rho,kind='zero')
hh=8.0
print('linear = ',f_linear(hh))
print('cubic = ',f_cubic(hh))
print('nearest = ',f_nearest(hh))
print('slinear = ',f_slinear(hh))
print('quadratic = ',f_quadratic(hh))
print('zero = ',f_zero(hh))
linear = 0.42990327868852457
cubic = 0.4289469617028989
nearest = 0.4481
slinear = 0.42990327868852457
quadratic = 0.4289242135593473
zero = 0.4481
4
plt.plot(x2 , f_nearest (x2), label ='nearest')
plt.plot(x2 , f_linear (x2), label ='linear')
plt.plot(x2 , f_cubic (x2), label ='cubic')
plt. legend ()
plt.show ()
1.3 Example:
In the following example, we calculate the function
𝜋𝑥 𝑦/2
𝑧(𝑥, 𝑦) = sin ( )𝑒
2
on a grid of points (𝑥, 𝑦) which is not evenly spaced in the y-direction. We then use
scipy.interpolate.interp2d to interpolate these values onto a finer, evenly spaced (𝑥, 𝑦) grid
5
[10]: import numpy as np
from scipy.interpolate import interp2d
import matplotlib.pyplot as plt
x = np.linspace(0, 4, 13)
y = np.array([0, 2, 3, 3.5, 3.75 , 3.875 , 3.9375 , 4])
X, Y = np.meshgrid(x, y)
Z = np.sin(np.pi*X/2)*np.exp(Y/2)
x2 = np. linspace (0, 4, 65)
y2 = np. linspace (0, 4, 65)
f = interp2d(x, y, Z, kind='cubic')
Z2 = f(x2, y2)
fig, ax = plt.subplots(nrows =1, ncols =2)
ax[0].pcolormesh(X, Y, Z)
X2, Y2 = np.meshgrid (x2 , y2)
ax[1].pcolormesh(X2 , Y2 , Z2)
plt.show()
1.3.1 Minimization
SciPy’s optimization routines minimize a function of one or more variables, f (x 1 , x 2 , . . . , x n
). To find the maximum, one determines the minimum of − f (x 1 , x 2 , . . . , x n ).
1.3.2 Example
Consider the Himmelblau’s function
6
𝑓(𝑥, 𝑦) = (𝑥2 + 𝑦 − 11)2 + (𝑥 + 𝑦2 − 7)2 .
To find a minimum, call minimize with some initial guess, say (x, y) = (0, 0):
fun: 1.3782261326630835e-13
hess_inv: array([[ 0.01578229, -0.0094806 ],
[-0.0094806 , 0.03494937]])
jac: array([-3.95019832e-06, -1.19075540e-06])
message: 'Optimization terminated successfully.'
nfev: 64
nit: 10
njev: 16
status: 0
success: True
x: array([2.99999994, 1.99999999])
[6]: print(f((2.99999994,1.99999999)))
1.4689999624268296e-13