Vous êtes sur la page 1sur 2

MATLAB Worksheet for Solving Linear Systems by Gaussian Elimination

The following examples are taken from from the book Scientic Computing with MATLAB (2003) by Quarteroni and Saleri.

Computer Problem: Write a program to nd the LU decomposition of a matrix without pivoting. Test your program by solving various linear systems. Method of Solution: First, create a MATLAB function called lu gauss.m. function A = lu_gauss(A) [n,m] = size(A); if n ~= m; error(A is not a square matrix); else for k = 1:n-1 for i = k+1:n A(i,k) = A(i,k)/A(k,k); if A(k,k) == 0, error(Null diagonal element); end for j = k+1:n A(i,j) = A(i,j) - A(i,k)*A(k,j); end end end end Notice, this function stores an upper triangular matrix in the upper triangular part of A and a lower triangular matrix in the strictly lower triangular part of A (the diagonal elements of L are 1). To test this function we may solve the linear system Ax = b with 0.370 0.050 0.050 0.070 2 0.050 0.116 0 0 0.050 A= b= 0.050 0 . 0 0.116 0.050 0.070 0.050 0.050 0.202 0 To solve, we rst nd the LU decomposition and then apply the backward and forward substitution algorithms in the following way. A = lu_gauss(A); [n,n] = size(A); y(1) = b(1); for i=2:n; y = [y;b(i)-A(i,1:i-1)*y(1:i-1)]; end x(n) = y(n)/A(n,n); for i=n-1:-1:1; x(i) = (y(i)-A(i,i+1:n)*x(i+1:n))/A(i,i); end The result is x = (8.1172, 5.9893, 5.9893, 5.7779)T . To further test your function, try solving a linear system with 1 1 3 5 A= 2 2 2 b= 6 3 6 4 13 for various values of epsilon. What is the problem that occurs for = 0? Using your function on the nonsingular matrix 1 1 + 0.5 1015 3 A= 2 2 20 , 3 6 4 show that the dierence A LU is not the zero matrix. What happens to cause this error? 1

MATLAB Worksheet

Computer Problem: The MATLAB function linsolve nds the solution of a linear system using Gaussian elimination with row pivoting, if the matrix is square. Use this command to solve the linear system Ax = b, where A is the Hilbert matrix having the elements ai,j = 1 , i+j 1 for i, j = 1, 2, . . . , n

and b is chosen so that the exact solution is x = (1, 1, . . . , 1)T . Plot the error as a function of n. Method of Solution: First, write a function to create the matrix A and the vectors x and b. function [A,x,b] = HilbertSys(n) for i = 1:n for j = 1:n A(i,j) = 1/(i+j-1); end end x = ones(1,n); b = A*x; Check to make sure your function is working, then create an m-le containing the commands. clear nn = 2:10:400; k = 0; for n = nn k = k+1; [A,x,b] = HilbertSys(n); xhat = linsolve(A,b); error(k) = norm(x-xhat); end semilogy(nn,error) Supposing you named this le HilbertSol.m, you can check the results by typing HilbertSol at the MATLAB command line. The program you have created uses the function HilbertSys to set up the linear system, then solves the system using the MATLAB function linsolve. Since we know the exact solution in this case, we can check the numerical solution error, and we do this by plotting the error as a function of the size of the system. Notice the way in which the error is plotted. Log plots are often very useful for plotting error and should be used when appropriate. Try using the plot commands plot and loglog and compare the results. Notice also the warnings that appear when we solve this system using linsolve. This is because the matrix is ill-conditioned, meaning small changes in the input for the linear system will lead to large changes in the output. This is the reason for the large error in our numerical results when n is large. As a result, what do you expect the results to be if we solve this system using the method of the previous exercise, or a modied version with row pivoting? In the same way, what do you expect the results to be if we solve the systems of the previous exercise with the method used in this exercise? Use MATLAB to test your hypotheses. To learn more about MATLABs capabilities with regard to matrices and linear systems, type help matfun for a list of various commands.

Vous aimerez peut-être aussi