Exercise 3.6 Solutions

3.

A=(({{-1, 3}, {6, -2}}));B=(({{2}, {1}}));X=(({{x}, {y}}));

Jacobi[A, Flatten[B], {0, 0}, 0.001, 3]

GaussSeidel[A, Flatten[B], {0, 0}, 0.001, 3]

N[Solve[{-x + 3  y == 1, 6  x - 2  y == 2}, {x, 
          y}]]

8.

A=(({{4, 1, -1}, {1, -5, -1}, {2, -1, -6}}));B=(({{13}, {-8}, {-2}}));X=(({{x}, {y}, {z}}));

Jacobi[A, Flatten[B], {0, 0, 0}, 0.001, 3]

GaussSeidel[A, Flatten[B], {0, 0, 0}, 0.001, 3]

Solve[{4  x + y - z == 13, x - 5  y - z == -8, 
        2  x - y - 6  z == -2}, {x, y, z}]

9.

Algorithms and Programs 2.

A=(({{1, 0, 1}, {-1, 1, 0}, {1, 2, -3}}));B=(({{2}, {0}, {0}}));X=(({{x}, {y}, {z}}));

Solve[{x + z == 2, -x + y == 0, 
        x + 2  y - 3  z == 0}, {x, y, z}]

Jacobi[A, Flatten[B], {0, 1.2, 0}, 0.001, 3]

GaussSeidel[A, Flatten[B], {1, 0, 3}, 0.001, 3]

Functions Used in this notebook

$Post=If[MatrixQ[#],MatrixForm[#] ,#]&;

\(\(\(Jacobi[A0_, B0_, P0_, ϵ_, 
          max_] := 
Module[{A = N[A0], B = N[B0], e, i, j, 
            k = 0, n = Length[P0], P = P0, 
            Pold = P0}, \ 
Print["P"\_0, " = ", 
            P]; \ 
e = 1; \ 
While[\ 
            And[\ k < max, e > ϵ\ ], 
For[\ i = 1, 
              i ≤ 
                n, \(i++\), 
P\_\(\([[\)\(i\)\(]]\)\)\  = \ \(1\/A\_\(\([[\)\(i, i\)\(]]\)\)\) \((B\_\(\([[\)\(i\)\(]]\)\)\  + \ 
                      A\_\(\([[\)\(i, i\)\(]]\)\)\ Pold\_\(\([[\)\(i\)\(]]\)\)\  - \ ∑\+\(j = 1\)\%n A\_\(\([[\)\(i, j\)\(]]\)\)\ Pold\_\(\([[\)\(j\)\(]]\)\))\)\ ]; \ 
            Print[P]; 
e = \@\(\((P - Pold)\) . \((P - Pold)\)\); \ 
Print["P"\_\(k + 1\), " = ", 
              P]; \ 
Pold = P; \ 
k = 
              k + 1]; \ 
Return[
            P];\ ];\)\(\ \ \)\)\)

\(\(\(GaussSeidel[A0_, B0_, P0_, ϵ_, 
          max_] := 
Module[{A = N[A0], B = N[B0], e, i, j, 
            k = 0, n = Length[P0], P = P0, 
            Pold = P0}, \ 
Print["P"\_0, " = ", 
            P]; \ 
e = 1; \ 
While[\ 
            And[\ k < max, e > ϵ\ ], 
Pold = 
              P; \ 
For[\ i = 1, 
              i ≤ 
                n, \(i++\), 
P\_\(\([[\)\(i\)\(]]\)\)\  = \ \(1\/A\_\(\([[\)\(i, i\)\(]]\)\)\) \((B\_\(\([[\)\(i\)\(]]\)\)\  + \ 
                      A\_\(\([[\)\(i, i\)\(]]\)\)\ P\_\(\([[\)\(i\)\(]]\)\)\  - \ ∑\+\(j = 1\)\%n A\_\(\([[\)\(i, j\)\(]]\)\)\ P\_\(\([[\)\(j\)\(]]\)\))\)\ ]; \ 
e = \@\(\((P - Pold)\) . \((P - Pold)\)\); \ 
Print["P"\_\(k + 1\), " = ", 
              P]; \ 
k = 
              k + 1]; \ 
Return[
            P];\ ];\)\(\ \ \)\)\)