Aufgabenstellung: Testen ob eine Funktion eine kubische Spline-Interpolierende ist.
In [11]:
var('x')
# input: funktionen der einzelnens splines
S = [
x/5 + 3/5 * x^3,
14/5 - 41/5 * x + 42/5 * x^2 - 2*x^3,
-122/5 + 151/5*x - 54/5* x^2 + 7/5*x^3
]
# input: definitionsbereich der spline funktionen
r = [
(0, 1),
(1, 2),
(2, 3)
]
for i, s in enumerate(S):
print "spline", i
show(s)
show(s.derivative(x))
show(s.derivative(x, 2))
print ""
In [12]:
eqns = []
for i in range(len(S) - 1):
eqns.extend([
(S[i] == S[i + 1]).subs(x=r[i][1]),
(S[i].derivative(x) == S[i + 1].derivative(x)).subs(x=r[i][1]),
(S[i].derivative(x, 2) == S[i + 1].derivative(x, 2)).subs(x=r[i][1])
])
eqns.extend([
(S[0].derivative(x, 2) == 0).subs(x=r[0][0]),
(S[-1].derivative(x, 2) == 0).subs(x=r[-1][1]),
])
for eqn in eqns:
show(eqn)
print "holds? ", eqn == True
print ""
Aufgabenstellung:
In [6]:
var('x')
# input:
constants = var('a,b,c,d,e')
S = [
a + b*(x - 1) + c*(x - 1)^2 + d * (x - 1)^3,
(x - 1)^3 + e*x^2 - 1
]
r = [
(0, 1),
(1, 2)
]
for i, s in enumerate(S):
print "S[{}], first, second derivative:".format(i)
for j in range(3):
show(s.derivative(x, j))
print ""
In [7]:
eqns = []
for i in range(len(S) - 1):
eqns.extend([
(S[i] == S[i + 1]).subs(x=r[i][1]),
(S[i].derivative(x) == S[i + 1].derivative(x)).subs(x=r[i][1]),
(S[i].derivative(x, 2) == S[i + 1].derivative(x, 2)).subs(x=r[i][1])
])
eqns.extend([
(S[0].derivative(x, 2) == 0).subs(x=r[0][0]),
(S[-1].derivative(x, 2) == 0).subs(x=r[-1][1]),
])
for eqn in eqns:
show(eqn.simplify())
In [8]:
for i in solve(eqns, *constants):
show(i)
In [ ]: