Cleve Moler is the author of the first MATLAB, one of the founders of MathWorks, and is currently Chief Mathematician at the company. He is the author of two books about MATLAB that are available online. He writes here about MATLAB, scientific computing and interesting mathematics.
This post is about two MATLAB programs included in the collection from Numerical Computing with MATLAB. The program golub generates badly conditioned integer test matrices. The program lugui is an interactive graphical interface that allows you to experiment with pivot strategies in Gaussian elimination.
A 6-by-6 example
Set the random number generator to the state it has when MATLAB starts and generate a 6-by-6 example.
In a 1998 paper Divakar Viswanath and Nick Trefethen demonstrate that the 2-norm condition number of an n -by- n triangular matrix with normally distributed entries below the diagonal and ones on the diagonal grows exponentially almost surely at a rate c^n with c = 1.30568...
I want my matrices to have integer entries so I use randomly distributed integers, but the exponential behavior must be nearly the same.
Here is the code for golub.
function A = golub(n)
%GOLUB Badly conditioned integer test matrices.
% GOLUB(n) is the product of two random integer n-by-n matrices,
% one of them unit lower triangular and one unit upper triangular.
% LU factorization without pivoting fails to reveal that such
% matrices are badly conditioned.
% See also LUGUI.
% Copyright 2014 Cleve Moler
% Copyright 2014 The MathWorks, Inc.
s = 10;
L = tril(round(s*randn(n)),-1)+eye(n);
U = triu(round(s*randn(n)),1)+eye(n);
A = L*U;
Experiment with pivoting
Lugui lets you use the mouse to select the pivot elements for Gaussian elimination. Or, you can choose diagonal pivoting, partial pivoting or complete pivoting. Here are animated gifs of these three choices.
Diagonal pivoting actually defies the conventional wisdom and picks the smallest possible element at each stage as the pivot, namely 1.0. But this choice means that division by the pivot involves no roundoff. Moreover the choice leads to the reconstruction of the original triangular factors.
To leave a comment, please click here to sign in to your MathWorks Account or create a new one.