Indirect methods solutions#
Solutions to the exercises on indirect methods.
Solution to Exercise 7.1
The Jacobi method for this system is
Using starting values of \(\vec{x} = \vec{0}\). Calculating the first iteration
Calculate the residual
Since \(\max(| \vec{r}^{(1)} |) = 6.5 > 10^{-4}\) we continue iterating. Calculating the second iteration
Calculate the residual
Since \(\max(| \vec{r}^{(2)} |) = 3.4 > 10^{-4}\) we continue iterating.
Solution to Exercise 7.2
The Gauss-Seidel method for this system is
Using starting values of \(\vec{x} = \vec{0}\). Calculating the first iteration
Calculate the residual
Since \(\max(| \vec{r}^{(1)} |) = 5.24 > 10^{-4}\) we continue iterating. Calculating the second iteration
Calculate the residual
Since \(\max(| \vec{r}^{(2)} |) = 1.07893 > 10^{-4}\) we continue iterating.
Solution to Exercise 7.3
The iteration matrix for the Jacobi method for this system of linear equations is
and the eigenvalues are \(\lambda_1 = -0.5347\), \(\lambda_2 = -0.249523\), \(\lambda_3 = 0.561913\) and \(\lambda_4 = 0.222310\) so \(\rho(T_J) = 0.561913\). Using equation (7.11)
The SOR method for this system is
Using starting values of \(\vec{x} = \vec{0}\). Calculating the first iteration
Calculate the residual
Since \(\max(| \vec{r}^{(1)} |) = 5.24 > 10^{-4}\) we continue iterating. Calculating the second iteration
Calculate the residual
Since \(\max(| \vec{r}^{(2)} |) = 1.078933 > 10^{-4}\) we continue iterating.
Solution to Exercise 7.4
The iteration matrices for the Jacobi, Gauss-Seidel and SOR methods (with \(\omega = 1.094573\)) are
and the spectral radii for these matrices are
so we would expect the SOR method to convergence the fastest. This is supported by the solutions to Exercise 7.1, Exercise 7.2 and Exercise 7.3.
Solution to Exercise 7.5
import numpy as np
def jacobi(A, b, tol=1e-6):
n = len(b)
x = np.zeros(n)
maxiter = 100
for k in range(maxiter):
xo = np.copy(x)
for i in range(n):
s = b[i]
for j in range(n):
if i != j:
s -= A[i,j] * xo[j]
x[i] = s / A[i,i]
r = b - np.dot(A, x)
if max(abs(r)) < tol:
break
return x, k + 1
def gauss_seidel(A, b, tol=1e-6):
n = len(b)
x = np.zeros(n)
maxiter = 100
for k in range(maxiter):
for i in range(n):
s = b[i]
for j in range(n):
if i != j:
s -= A[i,j] * x[j]
x[i] = s / A[i,i]
r = b - np.dot(A, x)
if max(abs(r)) < tol:
break
return x, k + 1
def sor(A, b, omega, tol=1e-6):
n = len(b)
x = np.zeros(n)
maxiter = 100
for k in range(maxiter):
for i in range(n):
s = b[i]
for j in range(n):
if i != j:
s -= A[i,j] * x[j]
x[i] = (1 - omega) * x[i] + omega / A[i,i] * s
r = b - np.dot(A, x)
if max(abs(r)) < tol:
break
return x, k + 1
# Define linear system
A = np.array([[5, 1, -1, 1],
[1, 4, -1, -1],
[-1, -1, 5, 1],
[1, -1, 1, 3]])
b = np.array([14, 10, -15, 3])
# Solve linear system
x, iterations = jacobi(A, b)
print(f"Jacobi method: {iterations:3d} iterations")
x, iterations = gauss_seidel(A, b)
print(f"Gauss-Seidel method: {iterations:3d} iterations")
x, iterations = sor(A, b, 1.094573)
print(f"SOR method: {iterations:3d} iterations")
print("\nSolution:")
for i in range(len(x)):
print(f" x{i+1} = {x[i]:9.6f}")
% Define linear system
A = [5, 1, -1, 1 ;
1, 4, -1, -1, ;
-1, -1, 5, 1 ;
1, -1, 1 3 ];
b = [14, ; 10 ; -15 ; 3];
% Solve linear system
[x, iterations] = jacobi(A, b, 1e-6);
fprintf("Jacobi method: %3i iterations", iterations)
[x, iterations] = gauss_seidel(A, b, 1e-6);
fprintf("Gauss-Seidel method: %3i iterations", iterations)
[x, iterations] =sor(A, b, 1.094573, 1e-6);
fprintf("Gauss-Seidel method: %3i iterations", iterations)
for i = 1 : length(x)
fprintf("x%1i = %9.6f\n", i, x(i))
end
% --------------------------------------------------------------
function [x, k] = jacobi(A, b, tol)
n = length(b);
x = zeros(size(b));
maxiter = 100;
for k = 1 : maxiter
xo = x;
for i = 1 : n
s = b(i);
for j = 1 : n
if i ~= j
s = s - A(i,j) * xo(j);
end
end
x(i) = s / A(i,i);
end
r = b - A * x;
if max(abs(r)) < tol
break
end
end
end
% --------------------------------------------------------------
function [x, k] = gauss_seidel(A, b, tol)
n = length(b);
x = zeros(size(b));
maxiter = 100;
for k = 1 : maxiter
for i = 1 : n
s = b(i);
for j = 1 : n
if i ~= j
s = s - A(i,j) * x(j);
end
end
x(i) = s / A(i,i);
end
r = b - A * x;
if max(abs(r)) < tol
break
end
end
end
% --------------------------------------------------------------
function [x, k] = sor(A, b, omega, tol)
n = length(b);
x = zeros(n, 1);
for k = 1 : 100
for i = 1 : n
sum_ = 0;
for j = 1 : n
if j ~= i
sum_ = sum_ + A(i,j) * x(j);
end
end
x(i) = (1 - omega) * x(i) + omega * (b(i) - sum_) / A(i,i);
end
r = b - A * x;
if max(abs(r)) < tol
break
end
end
end
Output
Jacobi method: 27 iterations
Gauss-Seidel method: 14 iterations
SOR method: 10 iterations
Solution:
x1 = 1.438776
x2 = 1.979592
x3 = -2.734694
x4 = 2.091837
Solution to Exercise 7.6
The iteration matrix for the Jacobi method use to solve this system of linear equations is
Calculate the eigenvalues of \(T_J\)
For a method to converge \(\rho(T) < 1\) so \(\frac{1}{2} \sqrt{\alpha} < 1\) therefore \(\alpha < 4\).