24.03.2013 Views

Chapter 3 Solution of Linear Systems - Math/CS

Chapter 3 Solution of Linear Systems - Math/CS

Chapter 3 Solution of Linear Systems - Math/CS

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

72 CHAPTER 3. SOLUTION OF LINEAR SYSTEMS<br />

n = 200:200:1000;<br />

t = zeros(length(n), 3);<br />

for i = 1:length(n)<br />

d = rand(n(i),1);<br />

A = diag(d);<br />

x = ones(n(i),1);<br />

b = A*x;<br />

tic, x1 = DiagSolve1(A,b);, t(i,1) = toc;<br />

tic, x2 = DiagSolve2(A,b);, t(i,2) = toc;<br />

tic, x3 = DiagSolve3(d,b);, t(i,3) = toc;<br />

end<br />

disp(’---------------------------------------------’)<br />

disp(’ Timings for DiagSolve functions’)<br />

disp(’ n DiagSolve1 DiagSolve2 DiagSolve3’)<br />

disp(’---------------------------------------------’)<br />

for i = 1:length(n)<br />

disp(sprintf(’%4d %9.3e %9.3e %9.3e’, n(i), t(i,1), t(i,2), t(i,3)))<br />

end<br />

Use the Matlab help and/or doc commands to write a brief explanation <strong>of</strong> what happens when<br />

these commands are executed. Write a script M-file with these commands, run the script, and<br />

describe what you observe from the computed results.<br />

3.5.2 Triangular <strong>Linear</strong> <strong>Systems</strong><br />

In this subsection we describe Matlab implementations <strong>of</strong> the forward substitution algorithms for<br />

lower triangular linear systems. Implementations <strong>of</strong> backward substitution for upper triangular<br />

linear systems are left as exercises.<br />

Consider the lower triangular linear system<br />

a11x1<br />

a21x1 + a22x2<br />

.<br />

= b1<br />

= b2<br />

an1x1 + an2x2 + · · · + annxn = bn<br />

.<br />

⇔<br />

⎡<br />

⎢<br />

⎣<br />

a11 0 · · · 0<br />

a21<br />

.<br />

a22<br />

.<br />

· · ·<br />

. ..<br />

0<br />

.<br />

an1 an2 · · · ann<br />

⎤ ⎡<br />

⎥ ⎢<br />

⎥ ⎢<br />

⎥ ⎢<br />

⎦ ⎣<br />

x1<br />

x2<br />

.<br />

xn<br />

⎤<br />

⎡<br />

⎥<br />

⎦ =<br />

⎢<br />

⎣<br />

We first develop a Matlab implementation to solve this lower triangular system using the pseudocode<br />

for the row-oriented version <strong>of</strong> forward substitution given in Fig. 3.4. A direct translation <strong>of</strong><br />

the pseudocode into Matlab code results in the following implementation:<br />

function x = LowerSolve0(A, b)<br />

%<br />

% x = LowerSolve0(A, b);<br />

%<br />

% Solve Ax=b, where A is an n-by-n lower triangular matrix,<br />

% using row-oriented forward substitution.<br />

%<br />

n = length(b);<br />

x = zeros(n,1);<br />

for i = 1:n<br />

for j = 1:i-1<br />

b(i) = b(i) - A(i,j)*x(j);<br />

end<br />

x(i) = b(i) / A(i,i);<br />

end<br />

b1<br />

b2<br />

.<br />

bn<br />

⎤<br />

⎥<br />

⎦ .

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!