Cubic spline interpolation: In the olden days of Engineering design, a spline wa
ID: 3804493 • Letter: C
Question
Cubic spline interpolation: In the olden days of Engineering design, a spline was a thin, springy strip of flexible rubber, wood, or metal which could be constrained to pass through specific points on a schematic drawing in order to trace out smooth curves. Analogously, we can mathematically define smooth curves which pass through (or interpolate) two or more points. The resulting mathematical objects are also called splines. Create a program in Python which takes as input a list of n +1 floating point numbers y(t) for integer values t = 0, 1, ... n, and outputs the tridiagonal matrix of coefficients A and right-hand side b for the linear system. Finally, solve the linear system by choosing two of the following three methods: figure out how to use scipy.linalg.solve_banded research and implement the Gauss-Seidel method research and implement the Jacobi Iteration methodExplanation / Answer
Please find the code below to generate Tridiagonal Matrix:
>>> import numpy as np
# Limiting the matrix to dimension 6x6
>>> N = 6
>>> diagonals = np.zeros((3, N)) # 3 diagonals
diagonals[0,:] = np.linspace(-1, -N, N)
diagonals[1,:] = -2
diagonals[2,:] = np.linspace(1, N, N)
>>> import scipy.sparse
>>> A = scipy.sparse.spdiags(diagonals, [-1,0,1], N, N, format=’csc’)
# We will get A here
>>> A.toarray() # look at corresponding dense matrix
[[-2. 2. 0. 0. 0. 0.]
[-1. -2. 3. 0. 0. 0.]
[ 0. -2. -2. 4. 0. 0.]
[ 0. 0. -3. -2. 5. 0.]
[ 0. 0. 0. -4. -2. 6.]
[ 0. 0. 0. 0. -5. -2.]]
>>> x = np.linspace(-1, 1, N) # choose solution
# We will get the matrix B here
>>> b = A.dot(x) # sparse matrix vector product
-----------------------------------------------------------
To solve the above using Gauss Seidel Method:
import numpy as np
import scipy.sparse
from scipy.linalg import solve
def gauss(A, b, x, n):
L = np.tril(A)
U = A - L
for i in range(n):
x = np.dot(np.linalg.inv(L), b - np.dot(U, x))
print str(i).zfill(3),
print(x)
return x
'''___MAIN___'''
N = 6
diagonals = np.zeros((3, N)) # 3 diagonals
diagonals[0,:] = np.linspace(-1, -N, N)
diagonals[1,:] = -2
diagonals[2,:] = np.linspace(1, N, N)
A = scipy.sparse.spdiags(diagonals, [-1,0,1], N, N, format=’csc’)
A = A.toarray()
x = np.linspace(-1, 1, N) # choose solution
b = A.dot(x)
#A = np.array([[4.0, -2.0, 1.0], [1.0, -3.0, 2.0], [-1.0, 2.0, 6.0]])
#b = [1.0, 2.0, 3.0]
#x = [1, 1, 1]
n = 20
print gauss(A, b, x, n)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.