Home > K25104 > LinearSystems > Backward.m

Backward

PURPOSE ^

Solves the upper triangular system of equations Ax = b

SYNOPSIS ^

function [x]=Backward(A,b)

DESCRIPTION ^

 Solves the upper triangular system of equations Ax = b
 Input arguments:
   A, square upper triangular matrix
   b, Ax = b
 Output arguments:
   x solution

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function [x]=Backward(A,b)
0002 % Solves the upper triangular system of equations Ax = b
0003 % Input arguments:
0004 %   A, square upper triangular matrix
0005 %   b, Ax = b
0006 % Output arguments:
0007 %   x solution
0008 
0009 [n,m]=size(A); % finding the size of A
0010 if n~= m
0011     error('input is not a square matrix');
0012 end
0013 if size(b,1) ~= n
0014     error('input dimensions do not match');
0015 end
0016 if ~istriu(A)
0017     error('matrix is not upper triangular')
0018 end
0019 
0020 x = zeros(n,1); % initialize x to the same dimension
0021 if abs(A(n,n)) > 1e-12 % not comparing to zero because of possible
0022                        % rounding errors
0023     x(n) = b(n)/A(n,n); % solve for the last element of x
0024 else
0025     error('input singular'); % A is singular if any of the diagonal
0026                             % elements are zero
0027 end
0028 for k=n:-1:1 % the loop considers one row after the other backwards
0029     if abs(A(k,k))>1e-12 % not comparing to zero because of possible
0030                          % rounding errors
0031         temp = 0;
0032         for j=n:-1:k+1
0033             temp = temp + A(k,j) * x(j); % Multiply the elements of
0034                                      % the k-th row of A after the
0035                                      % diagonal by the elements of x
0036                                      % already calculated
0037         end
0038         x(k) = (b(k)-temp)/A(k,k); % solve for the k-th element of x
0039     else
0040         error('input singular'); % A is singular if any of the diagonal
0041                                 % elements are zero
0042     end
0043 end

Generated on Mon 18-Jan-2016 10:25:49 by m2html © 2005