Clear Filters
Clear Filters

problem with back-substitution code

4 views (last 30 days)
I want to make an algorithm for back substitution and am testing it on some U and some b. when I call the function though it says U is not defined. Can anyone see where I have made an error? I am very new to MATLAB!
%--INPUTS------------------------------------------------------------------
%
% U - upper triangular matrix
% b - column vector
%
%--OUTPUT------------------------------------------------------------------
%
% b - solution x of Ux=b, stored in b
%
function b=myBackwardSubstitution(U,b)
d=size(U,1);
U=[1 2 3; 4 5 6; 7 8 9];
b=[1;2;3];
for i=1:d
for j=i+1:d
b(i)=b(i)-L(i,j)*b(j);
end
b(i)=b(i)/L(i,i);
end

Accepted Answer

Alan Stevens
Alan Stevens on 20 Aug 2020
Define U and b outside function to which you pass them.
Define L.
%--INPUTS------------------------------------------------------------------
%
% U - upper triangular matrix
% b - column vector
%
%--OUTPUT------------------------------------------------------------------
%
% b - solution x of Ux=b, stored in b
%
% Define U and b outside of function.
U=[1 2 3; 4 5 6; 7 8 9];
b=[1;2;3];
% Call function
b = myBackwardSubstitution(U,b);
function b=myBackwardSubstitution(U,b)
d=size(U,1);
for i=1:d
for j=i+1:d
% You haven't defined L
b(i)=b(i)-L(i,j)*b(j);
end
b(i)=b(i)/L(i,i);
end
end

More Answers (0)

Categories

Find more on Matrices and Arrays in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!