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.

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

• Using array operations, the backward substitution step can be implemented as:<br />

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

x(i) = (b(i) - A(i,i+1:n)*x(i+1:n)) / A(i,i);<br />

end<br />

Note that here the statement<br />

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

indicates that the loop runs over values i = n, n − 1, . . . , 1.<br />

• The next question we must address is how to implement a check for singularity. Due to round<strong>of</strong>f<br />

errors, it is unlikely that any akk will be exactly zero, and so the statement<br />

if A(k,k) == 0<br />

is unlikely to detect singularity. An alternative approach is to see if akk is small compared to,<br />

say, the largest entry in the matrix A. Such a test could be implemented as follows:<br />

if abs(A(k,k)) < tol<br />

where tol is computed in an initialization step, such as:<br />

tol = eps * max(abs(A(:)));<br />

Here, the command A(:) reshapes the matrix A into one long vector, and max(abs(A(:)))<br />

finds the largest entry. See help max to learn why we did not simply use max(abs(A)).<br />

• Finally, we note that, in addition to tol, certain initialization steps are needed. For example,<br />

we need to know the dimension, n, and we should allocate space for the solution vector and<br />

for the multipliers. We can do these things using the length, zeros, and eye functions:<br />

n = length(b);<br />

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

m = eye(n);<br />

Putting these steps together, we obtain the following function that uses Gaussian elimination with<br />

partial pivoting to solve Ax = b.

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

Saved successfully!

Ooh no, something went wrong!