In: Computer Science
MATLAB Function: (Backward substitution) Write a function that takes an upper triangular matrix U, and a vector b as input and returns the solution of the linear system U x = b. Use this function in the case when U is made of the numbers [(3,2,1),(0,5,4),(0,0,6)], and b = [1,1,−6]^T.
Question
MATLAB Function: (Backward substitution) Write a function that takes an upper triangular matrix U, and a vector b as input and returns the solution of the linear system U x = b. Use this function in the case when U is made of the numbers [(3,2,1),(0,5,4),(0,0,6)], and b = [1,1,−6]^T.
Sol:
MATLAB Function
function x=backSubstitution(U,b)
% Solving an upper triangular system by back-substitution
% Input matrix U is an n by n upper triangular matrix
% Input vector b is n by 1, where n specifies the dimensions of the
arrays
% Output vector x is the solution to the linear system
% U x = b
n=length(U);
x=zeros(n,1);
for j=n:-1:1
if (U(j,j)==0) error('Matrix is singular!');
end;
x(j)=b(j)/U(j,j);
b(1:j-1)=b(1:j-1)-U(1:j-1,j)*x(j);
end
MATLAB Main Program - Using the above function
clc;
clear all;
close all;
%Giving the values as in question
U=[3 2 1;0 5 4;0 0 6];
b = [1,1,-6]';
[x]=backSubstitution(U,b)