-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcubspline.py
More file actions
58 lines (45 loc) · 1.22 KB
/
cubspline.py
File metadata and controls
58 lines (45 loc) · 1.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
def spline(xl,yl,yp1=1.e30,ypn=1.e30):
n = len(xl)
yp1 = (yl[1]-yl[0])/(xl[1]-xl[0])
ypn = (yl[n-2]-yl[n-1])/(xl[n-2]-xl[n-1])
u = []
y2 = []
for i in range(0,n):
u.append(0)
y2.append(0)
if yp1 < .99e30:
y2[0] = -.5
u[0] = (3./(xl[1]-xl[0]))*((yl[1]-yl[0])/(xl[1]-xl[0])-yp1)
for i in range(0,n-1):
sig=(xl[i]-xl[i-1])/(xl[i+1]-xl[i-1])
p = sig*y2[i-1]+2.
y2[i] = (sig-1.)/p
u[i] = (yl[i+1]-yl[i])/(xl[i+1]-xl[i]) - (yl[i]-yl[i-1])/(xl[i]-xl[i-1])
u[i] = (6.*u[i]/(xl[i+1]-xl[i-1])-sig*u[i-1])/p
if ypn > .99e30:
qn=un=0
else:
qn = 0.5
un = (3./(xl[n-1]-xl[n-2]))*(ypn-(yl[n-1]-yl[n-2])/(xl[n-1]-xl[n-2]))
y2[n-1] = (un-qn*u[n-2])/(qn*y2[n-1]+1.)
rng = range(n-1)
rng.reverse
for k in rng:
y2[k]=y2[k]*y2[k+1]+u[k]
return y2,xl,yl
def splint(xl,yl,y2l,x):
#Note, this requires xlist is ordered with sequentially increasing x values
k = 0
while xl[k] < x:
k += 1
klo = k - 1
khi = k
if xl[khi] < x or xl[klo] > x:
print xl[khi],xl[klo],x
return 'your khi/klo do not bracket x'
h = xl[khi]-xl[klo]
if h == 0.0:
return 'you used the same x value twice, dummy'
a = (xl[khi]-x)/h
b = (x-xl[klo])/h
return a*yl[klo]+b*yl[khi]+((a*a*a-a)*y2l[klo]+(b*b*b-b)*y2l[khi])*(h*h)/6.