% Script to test backSubstitutionr.m for
% Solving an upper triangular system by back-substitution
% U x = b
% using row access.
% Input matrix UPPER is an n by n upper triangular matrix
% Input vector Bvec is n by 1
% Input scalar n specifies the dimensions of the arrays
% Output vector XvecR is the computed solution
% K. Ming Leung, 01/26/03, 03/09/05 (version 2, better variable names)

clear;
UPPER = [1  2  2
         0 -4 -6
         0  0 -1];
Bvec = [3; -6; 1];  % solution = [-1; 3; -1];
% another test case
%UPPER = [ 10 -7 0; 0 2.5 5; 0 0 6.2]; Bvec = [7; 2.5; 6.2]; % solution = [0; -1; 1];

n = length(Bvec); 

% backsubstitution using row access
XvecR = backSubstitutionR(UPPER,Bvec,n),
% backsubstitution using MATLAB '\' operator
XvecMatlab = UPPER\Bvec,

% maximum error
error = max(abs(XvecR-XvecMatlab)),
