COMPUTER PROBLEMS 2.5
1 The Python program jacobi.py can be used to solve
the system after defining \(A\) and \(b\) with an altered version of
sparsesetup.py. The initial vector is set to \([0,\ldots,0]\).
By checking the infinity norm error of the solution \(x\), say by
the command norm(x-1,inf), the Jacobi method can be iterated
until the error is less than \(0.5\times 10^{-6}\). For \(n=100\), 36
Jacobi steps are required. For \(n=100000\), 36 steps are required.
In both cases, the backward error is approximately \(4.6\times
10^{-7}\).
3 The Gauss-Seidel method can be coded in Python as
follows:
def gauss_seidel(a,b,k):
n = b.shape[0]
d = np.diag(np.diag(a))
u = np.triu(a,1)
l = np.tril(a,-1)
x = np.zeros((n))
for j in range(k):
b1 = b - u@x
for i in range(n):
x[i] = (b1[i]-l[i,:]@x)/d[i,i]
return x
5 Using the code from Computer Problem 3, 21 steps of
Gauss-Seidel iteration, starting from \(x=[0,\ldots,0]\), are
needed to converge to the correct solution within 6 decimal
places. Using the code from Computer Problem 4, 16 steps of SOR
with \(\omega=1.2\), starting from \(x=[0,\ldots,0]\), are needed to
converge to the correct solution within 6 decimal places.
7 The results will depend on the computer. Using the sparse
matrix capability of Matlab and the Gauss-Seidel code from
Computer Problem 3, typical results for one second of computation
are given in the table.
| \(n\) |
steps |
forward error |
| \(400\) | \(50\) | \(1.1\times 10^{-8}\) |
| \(800\) | \(15\) | \(1.7\times 10^{-3}\) |
| \(1200\) | \(7\) | \(2.5\times 10^{-2}\) |