8. (Back and Forward Substitution: Matlab Program) Write two programs, one that
ID: 3908532 • Letter: 8
Question
8. (Back and Forward Substitution: Matlab Program) Write two programs, one that performs back substitution on an upper triangular matrix and another that performs forward substitution on a lower triangular matrix (you may assume that the diagonal entries are all 1). Both files should begin function [x] forwardsub(L, b) n-length(b); (your code here) b and A is lower triangular. Test your code on the following In the above, Lx systems: 1 0 0 1 2-11 0 0 2 Remember, in Matlab you can solve matrix equations as follows (assuming you have defined the matrix A and the rhs vector b): >>Alb Print and hand-in the text file containing your program.Explanation / Answer
Hello Student!
I am happy to help you!
Save the following code as backwardSubstitution
function ans1 = backSubstitution(Upper,b)
n = size(Upper,1);
ans1 = zeros(n,1);
for j=n:-1:1
if (Upper(j,j)==0)
error('Matrix is singular!');
end;
ans1(j) = b(j)/Upper(j,j);
b(1:j-1) = b(1:j-1)-Upper(1:j-1,j)*ans1(j);
end
Save the following code as forwardSubstitution.m
function ans1 = forwardSubstitution(Lower,b)
n = size(Lower,1);
ans1 = zeros(n,1);
for j=1:n
if (Lower(j,j)==0)
error('Matrix is singular!');
end;
ans1(j) = b(j)/Lower(j,j);
b(j+1:n) = b(j+1:n)-Lower(j+1:n,j)*ans1(j);
end
Save it as test.m
clear all;
lower_mat_A = [[1, 0, 0]; [2, 1, 0] ;[3, 4, 1]];
lower_mat_B = [[-1]; [0]; [1]];
disp('Back Substitution :');
disp(backSubstitution(lower_mat_A, lower_mat_B));
upper_mat_A = [[1, 2, -1]; [0, 3, -1] ;[0, 0, 2]];
upper_mat_B = [[-1]; [0]; [1]];
disp('Forward Substitution :');
disp(forwardSubstitution(upper_mat_A,upper_mat_B));
Output :
Back Substitution :
-1
0
1
Forward Substitution :
-1.0000
0
0.5000
Thank you. Feel free to ask anything. Please upvote, if you like the answer. Thank you again.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.