Backward Substitution in MATLAB

Backward Substitution

Backward Substitution

Consider a Upper Triangular matrix U of dimension n, U x = b

[u1,1u1,2u1,n0u2,2u2,n00un,n][x1x2xn]=[b1b2bn]

Solving Procedure:

  1. xn=bnun,n
  2. Moving to n1th row, there are two coefficients related to xn(already known) and xn1 (unkown). un1,n1xn1+un1,nxn=bn1
  3. Solving for unknown xn, we have xn1=bn1un1,nxnun1,n1
  4. Generalizing for ith row, xi=1ui,i(binj=i+1ui,jxj)

A MATLAB function to solve upper triangular system is provided. This process is also known as 'backward substitution'

backsub_example
function X = backwardsubstitution(U,B)
% Input U = Upper triangular matrix
%       B = Right hand vector (force vector)
% Output X = solution vector (output vector)
n = size(U,1);          % n = rank of matrix U
for i = n:-1:1          % loops to move from last row to first row
    sum = 0;
    for j = i+1:n
        sum = sum + U(i,j)*X(j)/U(i,i);
    end
    X(i) = B(i)/U(i,i) - sum;
end


A = [5 2 4; 0 3 2; 0 0 1];
B = [10; -2; -2];
x = backwardsubstitution(A,B)
x =

    3.3333    0.6667   -2.0000

Comments

Popular posts from this blog

Solve Poisson's Equation in Matlab

Solving Poisson's Equation Using 5-Point Stencil In A Rectangular Domain