Solves the lower triangular system of equations Ax = b Input arguments: A, square lower triangular matrix b, Ax = b Output argument: x solution
0001 function [x] = Forward(A,b) 0002 % Solves the lower triangular system of equations Ax = b 0003 % Input arguments: 0004 % A, square lower triangular matrix 0005 % b, Ax = b 0006 % Output argument: 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 ~istril(A) 0017 error('matrix is not lower triangular') 0018 end 0019 0020 x = zeros(n,1); % initialize x to the same dimension 0021 if abs(A(1,1)) > 1e-12 % not comparing to zero because of possible 0022 % rounding errors 0023 x(1) = b(1)/A(1,1); % solve for the first element of x 0024 else 0025 disp('input singular'); % A is singular if any of the diagonal 0026 % elements are zero 0027 return; 0028 end 0029 for k=2:n % the loop considers one row after the other 0030 if abs(A(k,k))>1e-12 % not comparing to zero because of possible 0031 % rounding errors 0032 temp = 0; 0033 for j=1:k-1 0034 temp = temp + A(k,j) * x(j); % Multiply the elements of 0035 % the k-th row of A before the 0036 % diagonal by the elements of x 0037 % already calculated 0038 end 0039 x(k) = (b(k)-temp)/A(k,k); % solve for the k-th element of x 0040 else 0041 disp('input singular'); % A is singular if any of the 0042 % diagonal elements are zero 0043 return; 0044 end 0045 end