Using Google Collab, build and solve question your own matrix to solve certain matrices
Budget: $10 – $30 USD
Tasks
(a) Implement a tridiagonal solver in Python. The function to implement must be called tri_solver,
with input/output as described in the template.
A tri-diagonal solver is a simplified Gaussian elemination solver that makes use of the banded
nature of the matrix to reduce the amount of storage and computation required. We give you
the following pseudo-code to get started:
% Inpu t :
% l ( 0 :N−1) −− a r ra y o f s i z e N c o n t ai ni n g low e r di a g o n al
% u ( 0 :N−1) −− a r ra y o f s i z e N c o n t ai ni n g upper di a g o n al
% d ( 0 :N) −− a r ra y o f s i z e N+1 c o n t ai ni n g main di a g o n al
% b ( 0 :N) −− a r ra y o f s i z e N+1 c o n t ai ni n g the r i g h t hand s i d e .
3
% Output : a r ra y b i s o v e r w ri t t e n with s o l u t i o n .
% Forward e l i m i n a t i o n s
f o r i from 0 to N−1 do
% Compute m u l t i p l i e r
mult = l ( i )/ d ( i )
% Per form row r e d u c ti o n
d ( i +1) = d ( i +1) −mult∗u ( i )
% Reduce r i g h t hand s i d e
b ( i +1) = b ( i +1) − mult∗b ( i )
end f o r
% Back s u b s t i t u t i o n s
b (N) = b (N)/ d (N)
f o r i from N−1 to 0 do
b ( i ) = ( b ( i ) − u ( i )∗b ( i +1))/d ( i )
end f o r
(b) Implement a function called compute_derivative that computes the derivative using 4th-order
compact finite-differences. Refer to the template for details on input/output of this function.
This function will use tri_solver coded in part (a) to solve the system (⋆).
(c) Consider the test function
f(x) = sin(5x) for 0 ≤ x ≤ 3.
Compute the derivatives of f using your function from 4b) on a grid with N = 16 segments
(x0 = 0, x1 = 3/16, x2 = 2 × 3/16, ..., x16 = 3). On the same figure, plot your numerically
obtained derivative alongside the exact derivative of f evaluated at the nodes x0,..., x16.
(d) Plot the numerical solution with N = 32, 64, and 128. How does the agreement with the
analytical solution change?
(e) Conduct a performance study of your function compute_derivative by timing the code execution with N = 10, 100, 1000, 5000, 10000. Plot mesh size versus time. Briefly discuss your
results.
(a) Implement a tridiagonal solver in Python. The function to implement must be called tri_solver,
with input/output as described in the template.
A tri-diagonal solver is a simplified Gaussian elemination solver that makes use of the banded
nature of the matrix to reduce the amount of storage and computation required. We give you
the following pseudo-code to get started:
% Inpu t :
% l ( 0 :N−1) −− a r ra y o f s i z e N c o n t ai ni n g low e r di a g o n al
% u ( 0 :N−1) −− a r ra y o f s i z e N c o n t ai ni n g upper di a g o n al
% d ( 0 :N) −− a r ra y o f s i z e N+1 c o n t ai ni n g main di a g o n al
% b ( 0 :N) −− a r ra y o f s i z e N+1 c o n t ai ni n g the r i g h t hand s i d e .
3
% Output : a r ra y b i s o v e r w ri t t e n with s o l u t i o n .
% Forward e l i m i n a t i o n s
f o r i from 0 to N−1 do
% Compute m u l t i p l i e r
mult = l ( i )/ d ( i )
% Per form row r e d u c ti o n
d ( i +1) = d ( i +1) −mult∗u ( i )
% Reduce r i g h t hand s i d e
b ( i +1) = b ( i +1) − mult∗b ( i )
end f o r
% Back s u b s t i t u t i o n s
b (N) = b (N)/ d (N)
f o r i from N−1 to 0 do
b ( i ) = ( b ( i ) − u ( i )∗b ( i +1))/d ( i )
end f o r
(b) Implement a function called compute_derivative that computes the derivative using 4th-order
compact finite-differences. Refer to the template for details on input/output of this function.
This function will use tri_solver coded in part (a) to solve the system (⋆).
(c) Consider the test function
f(x) = sin(5x) for 0 ≤ x ≤ 3.
Compute the derivatives of f using your function from 4b) on a grid with N = 16 segments
(x0 = 0, x1 = 3/16, x2 = 2 × 3/16, ..., x16 = 3). On the same figure, plot your numerically
obtained derivative alongside the exact derivative of f evaluated at the nodes x0,..., x16.
(d) Plot the numerical solution with N = 32, 64, and 128. How does the agreement with the
analytical solution change?
(e) Conduct a performance study of your function compute_derivative by timing the code execution with N = 10, 100, 1000, 5000, 10000. Plot mesh size versus time. Briefly discuss your
results.