Upload
shivani-agarwal
View
225
Download
1
Embed Size (px)
Citation preview
MATLAB Worksheet for Solving Linear Systems by Gaussian Elimination
Brian E. Moore, Department of Mathematics, University of Central Florida
These examples are taken from from the book Scientific Computing with MATLAB (2003) by Quarteroni and Saleri.
Computer Problem: Write a program to find 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 alower 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
A =
−0.370 0.050 0.050 0.0700.050 −0.116 0 0.0500.050 0 −0.116 0.0500.070 0.050 0.050 −0.202
b =
−2000
.
To solve, we first find the LU decomposition and then apply the backward and forward substitutionalgorithms 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 alinear system with
A =
1 1− ε 32 2 23 6 4
b =
5− ε
613
for various values of epsilon. What is the problem that occurs for ε = 0? Using your function onthe nonsingular matrix
A =
1 1 + 0.5 × 10−15 32 2 203 6 4
,
show that the difference A− LU is not the zero matrix. What happens to cause this error?
1
2 MATLAB Worksheet
Computer Problem: The MATLAB function linsolve finds the solution of a linear systemusing Gaussian elimination with row pivoting, if the matrix is square. Use this command to solvethe 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-file 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 file HilbertSol.m, you can check the results by typing HilbertSol atthe MATLAB command line. The program you have created uses the function HilbertSys to setup the linear system, then solves the system using the MATLAB function linsolve. Since weknow the exact solution in this case, we can check the numerical solution error, and we do thisby plotting the error as a function of the size of the system. Notice the way in which the error isplotted. 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 becausethe matrix is ill-conditioned, meaning small changes in the input for the linear system will lead tolarge 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 methodof the previous exercise, or a modified version with row pivoting? In the same way, what do youexpect the results to be if we solve the systems of the previous exercise with the method used inthis exercise? Use MATLAB to test your hypotheses. To learn more about MATLAB’s capabilitieswith regard to matrices and linear systems, type help matfun for a list of various commands.