problem with back-substitution code

30 次查看(过去 30 天)
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

采纳的回答

Alan Stevens
Alan Stevens 2020-8-20
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

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Characters and Strings 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by