Ed Overman wrote:
>
> Is there an actual MATLAB command for doing back substitution. I realize
> this is part of Gaussian elimination, but I have been showing my students
> how to solve under and overdetermined systems by the QR decomposition.
> The final linear system they have to solve is upper triangular and so I
> would like them to use back substitution directly rather than \ which is
> just regular Gaussian elimination.
If you don't want to use \, I don't know that MATLAB has a command that
directly computes backward substitution. However, it is simple to write
a function to do this. The following function is a bit slower than
using \, but it does the job.
HTH,
Nathan Cahill
%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function x=backsub(A,y);
% backsub: solves upper triangular system A*x = y using backwards
substitution
% usage: x=backsub(A,y);
%
% arguments:
% A (nxn)  upper triangular matrix
% y (nx1)  vector
%
% x (nx1)  solution to A*x = y
% check to see that A is upper triangular
if any(any(tril(A,1)))
error('A must be upper triangular')
end
% check dimensions of A and y
[n,m]=size(A);
if n~=m
error('A must be square');
end
if any([n 1]~=size(y))
error('y must be a vector with the same # of rows as A');
end
% perform backwards substitution
x=zeros(n,1);
x(n)=y(n)/A(n,n);
for i=n1:1:1
x(i)=(y(i)A(i,i+1:n)*x(i+1:n))/A(i,i);
end
