Cubic Spline
Cubic Spline
Batch : EP2
import numpy as np
import matplotlib.pyplot as plt
from scipy.integrate import quad
def f(x):
return np.cos(np.pi * x)
def df(x):
return -np.pi * np.sin(np.pi * x)
def ddf(x):
return -np.pi**2 * np.cos(np.pi * x)
n = len(x) - 1
h = np.diff(x)
A[0, 0] = 1
A[-1, -1] = 1
a = y[:-1]
b = (y[1:] - y[:-1]) / h - h * (2 * c[:-1] + c[1:]) / 3
d = (c[1:] - c[:-1]) / (3 * h)
return a, b, c[:-1], d
A[0, 0] = 2 * h[0]
A[0, 1] = h[0]
A[-1, -2] = h[-1]
A[-1, -1] = 2 * h[-1]
b[0] = 3 * ((y[1] - y[0]) / h[0] - df_x0)
b[-1] = 3 * (df_xn - (y[-1] - y[-2]) / h[-1])
c = np.linalg.solve(A, b)
a = y[:-1]
b = (y[1:] - y[:-1]) / h - h * (2 * c[:-1] + c[1:]) / 3
d = (c[1:] - c[:-1]) / (3 * h)
return a, b, c[:-1], d
true_integral, _ = quad(f, 0, 1)
true_prime = df(0.5)
true_double_prime = ddf(0.5)