Download Iterative Methods for Linear Systems: Jacobi, Gauss-Seidel, and Over-Relaxation - Prof. As and more Assignments Computer Science in PDF only on Docsity!
Numerical Computing S’09 MATH/CSCI 4800- Homework-4 Solutions
- (Pencil-and-paper and computer) In class we considered three iterative methods for solving linear systems of type Ax = b; the Jacobi method, the GS (Gauss-Seidel) method and the SOR (successive over-relaxation) method. See sections 2.5.1 and 2.5.2 of the Text. In component form the Jacobi iteration is written as
x( ik +1)=
bi −
∑i− 1 j=1 aij^ x
(k) j −^
∑n j=i+1 aij^ x
(k) j aii
while the corresponding formula for the GS iteration is
x( ik +1)=
bi −
∑i− 1 j=1 aij^ x
(k+1) j −^
∑n j=i+1 aij^ x
(k) j aii
In many practical problems the matrix A is sparse, i.e., many of its entries are zeros. In such cases the iterative methods can be particularly attractive, from the points of view of both storage (zero entries need not be stored) and operation count. One frequently occurring example of a sparse matrix is the tridiagonal matrix, which has nonzero entries only along the main diagonal and the diagonals immediately above (superdiagonal) and below (subdiagonal) the main diagonal. For example, such a matrix of order 5 can be written as
A =
d 1 u 1 0 0 0 2 d 2 u 2 0 0 0 3 d 3 u 3 0 0 0 4 d 4 u 4 0 0 0 5 d 5
Only the entries di, `i and ui need be stored. The notation can be extended in an obvious way to a square matrix of arbitrary order n. Sparse matrices are discussed in section 2.5.4 of the Text but here we take a slightly different approach.
(a) Rewrite the formulas (1) and (2) for a tridiagonal matrix A of order n, using the notation introduced in (3). (You may need to treat x( 1 k +1) and x( nk +1) differently.) (b) Let A be a tridiagonal matrix with 4 as each entry on the main diagonal and -1 on the diagonals above and below the main. Let b be a vector of all ones. Write a well-commented Matlab script to solve Ax = b to a relative error of 10−^6 using the Jacobi iteration and the formulas derived in part (a). You may use the relative error to be
||x(k+^1 )^ − x(k)|| ||xk+^1 || + 10 −^16
(The term 10 −^16 is added to the denominator to guard against the case ||x(k+^1 )|| = 0. ) You may use the infinity norm, for which the Matlab notation is norm(x,inf). Print out the iteration number and the solution for each iteration. Comment on your results. (c) Repeat part (b) above using the GS method. Comment on your results and compare with those of part (a). (d) (Extra credit) Repeat part (b) above using the SOR method. Choose the relaxation parameter ω to lie in the interval (1, 1 .5). Experiment with several values of ω and comment on the results, comparing them with those of parts (b) and (c) above.
(a) The formulas (1) and (2) above reduce to the following.
x( 1 k +1) =
b 1 − u 1 x 2 d 1
x( nk +1) =
bn − `nxn− 1 dn
x( i k+1) =
bi − `ixi− 1 − uixi+ dI
, i = 2, 3 ,... n − 1.
(b) See attached Matlab Log. Only the first 5 and the last 5 iterations are presented. Twenty iterations are needed for convergence. (c) See attached Matlab Log. Only the first 5 and the last 5 iterations are presented. As expected, Gauss-Seidel converges faster, requiring 13 iterations to Jacobi’s 20. (d) See attached Matlab Log. Trial and error shows that ω = 1.08 results in the fastest convergence; 11 iterations. Results of the last 3 iterations are presented.
- (Pencil-and-paper and computer) Consider the three equations
x^21 + 2 sin x 2 + x 3 = 0 , cos x 2 − x 3 = 2 , x^21 + x^22 + x^23 = 2.
We seek the solution for x 1 , x 2 and x 3 using Newton’s method for nonlinear systems. As we discussed in class, (also see section 2.7.1 of the Text), Newton’s method applies to nonlinear systems of form fi(x 1 , x 2 , · · · xn) = 0, i = 1, 2 , · · · n. Given the n th iterate (^)
x( 1 n) x( 2 n) · · · x( nn)
the method computes the increment (^)
∆x( 1 n) ∆x( 2 n) · · · ∆x( nn)
by solving a related linear system J(n)∆x(n)^ = −f (n), where J is the Jacobian matrix. The n + 1 th iterate is then given by x(n+1)^ = x(n)^ + ∆x(n).
(a) Compute by hand the Jacobian matrix of the given 3 × 3 system. (b) Implement Newton’s method numerically to solve the given system. Your code must print at each iteration the following information: the iteration number, the solution at that iteration, and the norm of the increment to the solution at that iteration. Terminate the solution when the infinity norm of the increment to the solution is smaller than 10 −^6. Use the initial guess x 1 = 1. 1 , x 2 = 0. 1 , x 3 = − 0. 9. You may use as a guide the Matlab routines that are available on line under the title Newton2d scripts, or you may write your own script. If the online scripts are used, note that they are provided in a single file for ease of access. You must put each script in an m-file of its own. Further, the scripts must be modified to suit the problem and the display requirements stated above.
The interpolation requirement leads to the lower-triangular matrix problem
a 1 a 2 a 3 a 4
The solution, by forward substitution, is a 1 = 10, a 2 = − 14 , a 3 = 8, a 4 = − 73. The result, again, is the same as before, p(x) = − 4 −
x + 8x^2 −
x^3.
- (Pencil-and-paper) Text, page 159, section 3.2 Exercises, problem 2.
(a) Let the degree 2 interpolating polynomial through the points (1, 0), (2, ln 2), (4, ln 4) have the Newton form p(x) = a 1 + a 2 (x − 1) + a 3 (x − 1)(x − 2). Interpolating conditions yield the lower triangular matrix problem
a 1 a 2 a 3
ln 2 ln 4
The solution, by forward substitution, is a 1 = 0, a 2 = ln 2, a 3 = − 16 ln 2. Then the interpolant becomes p(x) = (x − 1) ln 2
x − 2 6
(b) p(3) = 53 ln 2 = 1. 552. (c) We note that the three data points lie on the graph of f (x) = ln x. Then we can use the error bound formula for interpolating f (x) over n data points:
f (x) − p(x) = f (n)(c) n!
W (x), c ∈ [x 1 , xn],
where W (x) = (x − x 1 )(x − x 2 ) · · · (x − xn). Here, n = 3 and f ′′′(x) = 2x−^3. Then the above formula becomes
f (x) − p(x) =
2 c−^3 3!
W (x), W (x) = (x − 1)(x − 2)(x − 4), and c ∈ [1, 4].
We need to compute the error bound at x = 3. Noting that |W (3)| = 2, and that | 2 c−^3 | takes its maximum value 2 at c = 1, the above formula leads to the error bound
|f (3) − p(3)| ≤
max | 2 c−^3 ||W (3)|
=
(d) The actual error turns out to be | ln 3 − p(3)| = | 1. 0986 − 1. 1552 | = 0. 0566 , indicating that the bound above is extremely conservative.
- (Pencil-and-paper) Text, page 169, section 3.3 Exercises, Problems 2(c), 4.
(a) Problem 2(c). Here we have n = 6 and the end points of the interval are a = 4, b = 12. The Chebyshev points are given by (see page 166 of Text)
xi = b + a 2
b − a 2
cos (2i − 1)π 2 n
= 8 + 4 cos (2i − 1)π 12
, i = 1, 2 ,... 6.
Also, |W (x)| = |(x − x 1 )(x − x 2 ) · · · (x − xn) ≤
b − a 2
)n 21 −n.
Here, the above reduces to
|(x − x 1 )(x − x 2 ) · · · (x − x 6 ) ≤ 46 × 2 −^5 = 128.
(b) Problem 4. Here we have f (x) = ex, n = 6 and the interval is a = 0. 6 , b = 1. The interpolation error in the interval is given by
ex^ − p(x) =
f (6)(c) 6! W (x), W (x) = (x − x 1 )(x − x 2 ) · · · (x − x 6 ), and c ∈ [0. 6 , 1].
We know that max |f (6)(c)| = max |ec| = e. Also, using the formula from part (a) above,
|W (x)| ≤ (0.2)^6 × 2 −^5.
The upshot is that |ex^ − p(x)| ≤ e(0.2)^6 × 2 −^5 = 7. 55 × 10 −^9.
Solution vector
% Define vector b
b=ones(n,1);
% Specify maximum number of iterations
M=25;
% Initialize solution vector
x=zeros(n,1);
y=zeros(n,1);
for k=1:M
y(1,1)=(b(1)-ell(2,1)*x(2,1))/d(1,1);
for i=2:n-
y(i,1)=(b(i,1)-u(i-1,1)y(i-1,1)-ell(i+1,1)x(i+1,1))/
d(i,1);
end
y(n,1)=(b(n,1)-u(n-1,1)*y(n-1,1))/d(n,1);
err=norm(y-x,inf)/(norm(y,inf)+eps); % error
x=y;
format short e
fprintf('Iteration number = %2d \n',k);
disp('Solution vector');
disp(x);
if (err<0.000001), return; end
end
Numerical Output for Gauss-Seidel
Only the first 5 and last 5 iterates are shown.
Iteration number = 1
Solution vector
2.500000000000000e-
3.125000000000000e-
3.281250000000000e-
3.320312500000000e-
3.330078125000000e-
3.332519531250000e-
3.333129882812500e-
3.333282470703125e-
3.333320617675781e-
3.333330154418945e-
Iteration number = 2
Solution vector
Solution vector
4.640977250500775e-
4.903675194134900e-
4.973728689337766e-
4.991242533583493e-
4.991242978124494e-
4.973730089887205e-
4.903677673604303e-
4.640980706184201e-
3.660245176546050e-
Iteration number = 13
Solution vector
3.660244312625194e-
4.640979876690023e-
4.903677141506947e-
4.973729918772610e-
4.991243224224277e-
4.991243328527870e-
4.973730250533043e-
4.903677739179311e-
4.640980728931340e-
3.660245182232835e-
1(d)
MATLAB LOG
Script file sor.m and numerical output are given below. Trial and error finds omega=1.
to be the optimal choice, as it leads to convergence in the least number of iterations
% Script File sor.m
% Define A
%clc
n=10;
omega=1.08; % SOR parameter
d=4*ones(n,1);
u=-ones(n-1,1);
ell=-ones(n,1);
b=ones(n,1);
x=zeros(n,1);
y=zeros(n,1);
for k=1:
y(1,1)=(b(1)-ell(2,1)*x(2,1))/d(1,1);
for i=2:n-
%y(i,1)=(b(i,1)-u(i-1,1)x(i-1,1)-ell(i+1,1)x(i+1,1))/
d(i,1);
y(i,1)=(b(i,1)-u(i-1,1)y(i-1,1)-ell(i+1,1)x(i+1,1))/
d(i,1);
end
%y(n,1)=(b(n,1)-u(n-1,1)*x(n-1,1))/d(n,1);
y(n,1)=(b(n,1)-u(n-1,1)*y(n-1,1))/d(n,1);
% Define GS increment
z=y-x;
% Define new SOR iterate
y = x+omega*z;
err=norm(y-x,inf)/(norm(y,inf)+eps);
x=y;
format long e
disp(k);
disp(x);
if (err<0.000001), return; end
end
Numerical results
(Last 3 iterations)
Iteration count = 9
Solution
3.660218768157313e-
4.640969128497018e-
4.903657954676057e-
4.973724814229266e-
4.991237843035582e-
4.991241486940494e-
4.973729228774881e-
4.903677361751863e-
4.640980594220886e-
3.660245148890766e-
Iteration count = 10
Solution
3.660244163241609e-
4.640975533656322e-
4.903676329451838e-
4.973728473937747e-
4.991242788800109e-
4.991243026874718e-
4.973730135828502e-
4.903677690032113e-
F=[F1;F2;F3];
% Define jacobian
J11=2x1; J12=2cos(x2); J13=1;
J21=0; J22=-sin(x2); J23=-1;
J31=2x1; J32=2x2; J33=2*x3;
JF=[J11,J12, J13;J21,J22,J23;J31,J32,J33];
function x = Newton3d(fun,x0,tol,m)
% Matlab function Newton3d.m
% Solves a nonlinear system f(x)=0 by Newton's method
% Inputs:
% x0 = initial guess, a column vector
% tol = tolerance for stopping
% m = maximum number of iterations
% Outputs:
% x = solution, a column vector
% Required m-files
% fun.m for the function and its jacobian matrix
format long e
x=x0; k = 0;
while k <= m
k=k+1;
fprintf('Iteration count = %i \n',k);
[f,Jf]=feval(fun,x);
dx = -Jf\f;
x=x+dx;
disp('Solution');
disp(x);
disp('Norm of increment');
disp(norm(dx));
if norm(dx)<tol, x; return;end
end
warning(sprintf('root not found within tolerance after %d
iterations\n',k));
Numerical output
Iteration count = 1
Solution
1.021674086149511e+
-2.292724149007840e-
-9.927235882050501e-
Norm of increment
1.727533481310678e-
Iteration count = 2
Solution
1.000739745550038e+
-6.410959513879488e-
-9.997519026114061e-
Norm of increment
3.137381237116450e-
Iteration count = 3
Solution
1.000000714115834e+
0 10 20 30 40 50 60 70
0
1
2
3
4
5
6
Iteration count
Norm of increment
Problem 2(c)
Graph of norm of increment against iteration count shows no evidence of convergence.
- 2.500000000000000e-
- 2.500000000000000e-
- 2.500000000000000e-
- 2.500000000000000e-
- 2.500000000000000e-
- 2.500000000000000e-
- 2.500000000000000e-
- 2.500000000000000e-
- Iteration number =
- 3.125000000000000e-
- 3.750000000000000e-
- 3.750000000000000e-
- 3.750000000000000e-
- 3.750000000000000e-
- 3.750000000000000e-
- 3.750000000000000e-
- 3.750000000000000e-
- 3.750000000000000e-
- 3.125000000000000e-
- Iteration number =
- 3.437500000000000e- Solution vector
- 4.218750000000000e-
- 4.375000000000000e-
- 4.375000000000000e-
- 4.375000000000000e-
- 4.375000000000000e-
- 4.375000000000000e-
- 4.375000000000000e-
- 4.218750000000000e-
- 3.437500000000000e-
- Iteration number =
- 3.554687500000000e- Solution vector
- 4.453125000000000e-
- 4.648437500000000e-
- 4.687500000000000e-
- 4.687500000000000e-
- 4.687500000000000e-
- 4.687500000000000e-
- 4.648437500000000e-
- 4.453125000000000e-
- 3.554687500000000e-
- Iteration number =
- 3.613281250000000e- Solution vector
- 4.550781250000000e-
- 4.785156250000000e-
- 4.833984375000000e-
- 4.843750000000000e-
- 4.843750000000000e-
- 4.833984375000000e-
- 4.785156250000000e-
- 4.550781250000000e-
- 3.613281250000000e-
- Iteration number =
- 3.660231635440141e- Solution vector
- 4.640954923816025e-
- 4.903641494456679e-
- 4.973686849698424e-
- 4.991196070332080e-
- 4.991196070332080e-
- 4.973686849698424e-
- 4.903641494456679e-
- 4.640954923816025e-
- 3.660231635440141e-
- Iteration number =
- 3.660238730954006e- Solution vector
- 4.640968282474205e-
- 4.903660443378612e-
- 4.973709391197190e-
- 4.991220730007626e-
- 4.991220730007626e-
- 4.973709391197190e-
- 4.903660443378612e-
- 4.640968282474205e-
- 3.660238730954006e-
- Iteration number =
- 3.660242070618551e- Solution vector
- 4.640974793583155e-
- 3.281250000000000e-
- 4.140625000000000e-
- 4.365234375000000e-
- 4.423828125000000e-
- 4.439086914062500e-
- 4.443054199218750e-
- 4.444084167480469e-
- 4.444351196289062e-
- 4.444420337677002e-
- 3.611105084419250e-
- Iteration number =
- 3.535156250000000e-
- 4.475097656250000e-
- 4.724731445312500e-
- 4.790954589843750e-
- 4.808502197265625e-
- 4.813146591186523e-
- 4.814374446868896e-
- 4.814698696136475e-
- 4.606450945138931e-
- 3.651612736284733e-
- Iteration number =
- 3.618774414062500e- Solution vector
- 4.585876464843750e-
- 4.844207763671875e-
- 4.913177490234375e-
- 4.931581020355225e-
- 4.936488866806030e-
- 4.937796890735626e-
- 4.886061958968639e-
- 4.634418673813343e-
- 3.658604668453336e-
- Iteration number =
- 3.646469116210938e- Solution vector
- 4.622669219970703e-
- 4.883961677551270e-
- 4.953885674476624e-
- 4.972593635320663e-
- 4.977597631514072e-
- 4.965914897620678e-
- 4.900083392858505e-
- 4.639672015327960e-
- 3.659918003831990e-
- Iteration number =
- 3.660084538860247e- Solution vector
- 4.640790217963513e-
- 4.903515001096821e-
- 4.973614903792622e-
- 4.991172680583986e-
- 4.991205247143853e-
- 4.973712067938862e-
- 4.903670140248799e-
- 4.640978065878221e-
- 3.660244516469555e-
- Iteration number =
- 3.660197554490878e- Solution vector
- 4.640928138896925e-
- 4.903635760672387e-
- 4.973702110314093e-
- 4.991226839364487e-
- 4.991234726825837e-
- 4.973726216768659e-
- 4.903676070661720e-
- 4.640980146782819e-
- 3.660245036695705e-
- Iteration number =
- 3.660232034724231e- Solution vector
- 4.640966948849155e-
- 4.903667264790812e-
- 4.973723526038825e-
- 4.991239563216165e-
- 4.991241444996206e-
- 4.973729378914482e-
- 4.903677381424325e-
- 4.640980604530007e-
- 3.660245151132502e-
- Iteration number =
- 3.660241737212289e- Solution vector
- -5.435214085515189e-
- -9.999997948464610e-
- Iteration count = - 1.000000000000572e+ Solution
- -3.904983200031964e-
- -9.999999999998522e-
- 9.205778722024930e- Norm of increment
- 2.268070022473634e+ convergence after 60 iterations.
- 1.476300214137365e+
- 6.547372818650538e-
- 3.048364168296681e-
- 7.260185027009951e-
- 3.890375147249630e-
- 2.627034359501224e-
- 1.360705323149030e+
- 6.932731133829346e-
- 3.742780684651118e-
- 2.615511129724186e-
- 5.478224452578883e+
- 2.742237647778653e+
- 1.377394699730319e+
- 7.014524430017555e-
- 3.779678877947888e-
- 2.617168174476218e-
- 3.054778234362746e+
- 1.533015385029267e+
- 7.779066115087337e-
- 4.129822059073763e-
- 2.667722139065973e-
- 6.727796735643141e-
- 3.650915089183535e-
- 2.615641156860901e-
- 5.055455119632074e+
- 2.531115359707647e+
- 1.272365167039057e+
- 6.500572746533630e-
- 3.550146192596594e-
- 2.624248169332177e-
- 1.544884684485611e+
- 7.837498037971786e-
- 4.156912010210271e-
- 2.673626559021245e-
- 6.406143303658820e-
- 3.508646661038769e-
- 2.630950594773638e-
- 1.187788393714701e+
- 6.088294605407025e-
- 3.370853442232386e-
- 2.670601732977131e-
- 6.564379537399471e-
- 3.578318008650072e-
- 2.620817403135898e-
- 1.929411876493384e+
- 9.736887811243203e-
- 5.054091259933440e-
- 2.952949134716104e-
- 3.177366599220379e-
- 2.796309481856957e-
- 3.944646505628043e-
- 2.634191686032065e-
- 1.086519560534466e+
- 5.597092592455358e-
- 3.165167118025697e-
- 2.808420633066052e-
- 3.848157617189251e-
- 2.622470846053057e-
- 1.711795853818733e+
- 8.660612565666633e-