0% found this document useful (0 votes)
9 views5 pages

Cubic Spline

The document presents a Python implementation of natural and clamped cubic spline interpolation for the function f(x) = cos(πx). It includes calculations of spline coefficients, derivatives, integrals, and visualizations comparing the two spline methods. The results include the coefficients for each interval, integral values, and derivative evaluations at x = 0.5, along with a plot of the spline interpolations against the true function.

Uploaded by

anisharma1729
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views5 pages

Cubic Spline

The document presents a Python implementation of natural and clamped cubic spline interpolation for the function f(x) = cos(πx). It includes calculations of spline coefficients, derivatives, integrals, and visualizations comparing the two spline methods. The results include the coefficients for each interval, integral values, and derivative evaluations at x = 0.5, along with a plot of the spline interpolations against the true function.

Uploaded by

anisharma1729
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Assignment 3

Name : Anish Raghuvir Sharma

Enrollment Number : 22123005

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)

x = np.array([0, 0.25, 0.5, 0.75, 1.0])


y = f(x)

n = len(x) - 1

h = np.diff(x)

def natural_cubic_spline(x, y, h):


A = np.zeros((n + 1, n + 1))
b = np.zeros(n + 1)

A[0, 0] = 1
A[-1, -1] = 1

for i in range(1, n):


A[i, i - 1] = h[i - 1]
A[i, i] = 2 * (h[i - 1] + h[i])
A[i, i + 1] = h[i]
b[i] = 3 * ((y[i + 1] - y[i]) / h[i] - (y[i] - y[i - 1]) / h[i
- 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

def clamped_cubic_spline(x, y, h, df_x0, df_xn):


A = np.zeros((n + 1, n + 1))
b = np.zeros(n + 1)

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])

for i in range(1, n):


A[i, i - 1] = h[i - 1]
A[i, i] = 2 * (h[i - 1] + h[i])
A[i, i + 1] = h[i]
b[i] = 3 * ((y[i + 1] - y[i]) / h[i] - (y[i] - y[i - 1]) / h[i
- 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

def s_spline(x_val, x, a, b, c, d):


for i in range(n):
if x[i] <= x_val <= x[i + 1]:
dx = x_val - x[i]
spline_val = a[i] + b[i] * dx + c[i] * dx**2 + d[i] *
dx**3
spline_prime = b[i] + 2 * c[i] * dx + 3 * d[i] * dx**2
spline_double_prime = 2 * c[i] + 6 * d[i] * dx
return spline_val, spline_prime, spline_double_prime
return None, None, None

def integrate_spline(a, b, c, d):


integral = 0
for i in range(n):
h_i = h[i]
integral += (a[i] * h_i + b[i] * h_i**2 / 2 + c[i] * h_i**3 /
3 + d[i] * h_i**4 / 4)
return integral

a_natural, b_natural, c_natural, d_natural = natural_cubic_spline(x,


y, h)
a_clamped, b_clamped, c_clamped, d_clamped = clamped_cubic_spline(x,
y, h, 0, 0)

integral_natural = integrate_spline(a_natural, b_natural, c_natural,


d_natural)
integral_clamped = integrate_spline(a_clamped, b_clamped, c_clamped,
d_clamped)

true_integral, _ = quad(f, 0, 1)

_, natural_prime, natural_double_prime = s_spline(0.5, x, a_natural,


b_natural, c_natural, d_natural)
_, clamped_prime, clamped_double_prime = s_spline(0.5, x, a_clamped,
b_clamped, c_clamped, d_clamped)

true_prime = df(0.5)
true_double_prime = ddf(0.5)

print("Natural Cubic Spline Coefficients:")


for i in range(n):
print(f"Interval [{x[i]}, {x[i+1]}]:")
print(f"f(x) = {a_natural[i]}+{b_natural[i]}*x+
{c_natural[i]}*x**2+{d_natural[i]}*x**3")
print()

print("Clamped Cubic Spline Coefficients:")


for i in range(n):
print(f"Interval [{x[i]}, {x[i+1]}]:")
print(f" f(x) = {a_clamped[i]}+({b_clamped[i]})*x +
({c_clamped[i]})*x**2 +({d_clamped[i]})*x**3")
print()

print("Natural Cubic Spline:")


print(f"Integral over [0, 1]: {integral_natural:.6f}, True Integral:
{true_integral:.6f}")
print(f"f'(0.5): {natural_prime:.6f}, True f'(0.5): {true_prime:.6f}")
print(f"f''(0.5): {natural_double_prime:.6f}, True f''(0.5):
{true_double_prime:.6f}\n")
print("Clamped Cubic Spline:")
print(f"Integral over [0, 1]: {integral_clamped:.6f}, True Integral:
{true_integral:.6f}")
print(f"f'(0.5): {clamped_prime:.6f}, True f'(0.5): {true_prime:.6f}")
print(f"f''(0.5): {clamped_double_prime:.6f}, True f''(0.5):
{true_double_prime:.6f}")

x_vals = np.linspace(x[0], x[-1], 100)


y_natural = [s_spline(xi, x, a_natural, b_natural, c_natural,
d_natural)[0] for xi in x_vals]
y_clamped = [s_spline(xi, x, a_clamped, b_clamped, c_clamped,
d_clamped)[0] for xi in x_vals]
y_true = f(x_vals)

plt.plot(x_vals, y_natural, label='Natural Cubic Spline')


plt.plot(x_vals, y_clamped, label='Clamped Cubic Spline')
plt.plot(x_vals, y_true, label='f(x)')
plt.scatter(x, y, label='Data points')
plt.legend()
plt.title('Natural vs Clamped Cubic Spline Interpolation')
plt.xlabel('x')
plt.ylabel('y')
plt.show()

Natural Cubic Spline Coefficients:


Interval [0.0, 0.25]:
f(x) = 1.0+-0.7573593128807146*x+0.0*x**2+-6.627416997969521*x**3

Interval [0.25, 0.5]:


f(x) = 0.7071067811865476+-1.9999999999999996*x+-
4.970562748477141*x**2+6.627416997969519*x**3

Interval [0.5, 0.75]:


f(x) = 6.123233995736766e-17+-3.242640687119285*x+-
1.4210854715202005e-15*x**2+6.627416997969523*x**3

Interval [0.75, 1.0]:


f(x) = -0.7071067811865475+-
2.0000000000000004*x+4.970562748477141*x**2+-6.627416997969521*x**3

Clamped Cubic Spline Coefficients:


Interval [0.0, 0.25]:
f(x) = 1.0+(0.0)*x + (-5.193321002610615)*x**2
+(2.028118006381504)*x**3

Interval [0.25, 0.5]:


f(x) = 0.7071067811865476+(-2.2163883751087754)*x + (-
3.672232497824487)*x**2 +(4.896309997099315)*x**3
Interval [0.5, 0.75]:
f(x) = 6.123233995736766e-17+(-3.1344464995648975)*x + (-
1.4347497549002023e-15)*x**2 +(4.896309997099318)*x**3

Interval [0.75, 1.0]:


f(x) = -0.7071067811865475+(-2.2163883751087763)*x +
(3.672232497824487)*x**2 +(2.028118006381506)*x**3

Natural Cubic Spline:


Integral over [0, 1]: 0.000000, True Integral: 0.000000
f'(0.5): -3.242641, True f'(0.5): -3.141593
f''(0.5): -0.000000, True f''(0.5): -0.000000

Clamped Cubic Spline:


Integral over [0, 1]: 0.000000, True Integral: 0.000000
f'(0.5): -3.134446, True f'(0.5): -3.141593
f''(0.5): -0.000000, True f''(0.5): -0.000000

You might also like