Writing a recursive matlab code that can perform column-oriented substitution
9 views (last 30 days)
Show older comments
I'm trying to write a code that can perform forward substitution for the equation Ax=b where A is a lower triangular matrix and x and b are column vectors.
I have the pseudo code figured out, but I can't make the script work!! I'm new to Matlab, but i'm trying to write a program that takes a lower triangular matrix and a column vector (the b vector) and performs the necessary forward substitution, then outputs then solution. Here's the blueprint:
for k = 1,...,n
[if g_jj = 0, set error flag (A is singular), exit
b_(k,1) = b_(k.1)*a_(k,k)
for m = k+1,...,n (but not executed when k=n)
[b_(m,1) = b_(m,1) - g(m,k)*b_(k,1)
can somebody help me figure out how to get this code in a format where It can be ran as a matlab script? Thank you!
0 Comments
Answers (1)
Shubham Rawat
on 27 Jan 2021
Hi Michael,
I can't figure out the what are your variables for and working of your algorithm. But I have written a functional code for that. Before that you have to initialize the variables.
for k=1:n
if g(k,k) == 0 %if this g(k,k) becomes 0 it will display and then exit
disp('A is singular');
return;
end
b(k) = b(k)*a(k,k);
for m = k+1:n
if(k == n) % if k becomes n it will exit
return;
end
b(m) = b(m)-g(m,k)*b(k);
end
end
If you are doing forward substitution. Here is the function for forward which take lower triangular matrix L and column vector b and give result vector x:
function x=forwardsubstitution(L,b)
n = size(L,1);
for i=1:n
for j=1:i-1
b(i)=b(i)-L(i,j)*b(j);
end
b(i) = b(i)/L(i,i);
end
x=b;
end
Hope this helps!
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!