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.

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

Given this factorization, and a vector b, we could solve the linear system Ax = b using the<br />

following Matlab statements:<br />

d = P*b;<br />

y = L \ d;<br />

x = U \ y;<br />

These statements could be nested into one line <strong>of</strong> code as:<br />

x = U \ ( L \ (P*b) );<br />

Thus, given a matrix A and vector b we could solve the linear system Ax = b as follows:<br />

[L, U, P] = lu(A);<br />

x = U \ ( L \ (P*b) );<br />

The cost <strong>of</strong> doing this is essentially the same as using the backslash operator. So when would we<br />

prefer to explicitly compute the P A = LU factorization? One situation is when we need to solve<br />

several linear systems with the same coefficient matrix, but many different right hand side vectors.<br />

It is more expensive to compute the P A = LU factorization than it is to use forward and backward<br />

substitution to solve lower and upper triangular systems. Thus if we can compute the factorization<br />

only once, and use it for the various different right hand side vectors, a substantial savings can be<br />

attained. This is illustrated in the following problem, which involves a relatively small linear system.<br />

Problem 3.5.18. Create a script m-file containing the following Matlab statements:<br />

n = 50;<br />

A = rand(n);<br />

tic<br />

for k = 1:n<br />

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

x = A\b;<br />

end<br />

toc<br />

tic<br />

[L, U, P] = lu(A);<br />

for k = 1:n<br />

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

x = U \ ( L \ (P*b) );<br />

end<br />

toc<br />

Currently the dimension <strong>of</strong> the problem is set to n = 50. Experiment with other values <strong>of</strong> n, such as<br />

n = 100, 150, 200. Comment on your observations.

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

Saved successfully!

Ooh no, something went wrong!