function x=nma_BackSub(U,v) %function x=nma_BackSub(U,v) % %Backward substitution that solves the lower triangular system %Ux=v for x % % %INPUT: % U: an nxn U, upper tirangular square matrix % v: vector of size n % %OUTPUT: % x: the solution to U*x=v % By Nasser M. Abbasi % HW 4, MATH 501, CSUF % % Backsubstition matlab function. % Copyright (C) 2007 Nasser Abbasi % % This program is free software; you can redistribute it and/or % modify it under the terms of the GNU General Public License % as published by the Free Software Foundation; either version 2 % of the License, or (at your option) any later version. % % This program is distributed in the hope that it will be useful, % but WITHOUT ANY WARRANTY; without even the implied warranty of % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the % GNU General Public License for more details. % % You should have received a copy of the GNU General Public License % along with this program; if not, write to the Free Software % Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. if nargin ~= 2 error 'Only two inputs are required' end if ~(isnumeric(U)&isnumeric(v)) error 'input must be numeric' end [nRow,nCol]=size(v); if nRow>1 & nCol>1 error 'v must be a vector not a matrix' end [nRow,nCol]=size(U); if nRow ~= nCol error 'Matrix U must be square' end if length(v) ~= nRow error 'v length does not match U matrix dimension' end x=zeros(nRow,1); x(nRow)=v(nRow)/U(nRow,nRow); v=v(:); for n=(nRow-1):-1:1 x(n)=(v(n)-(U(n,n+1:end)*x(n+1:end))) / U(n,n); end end