problem with back-substitution code

30 Ansichten (letzte 30 Tage)
Sienna Phillips
Sienna Phillips am 20 Aug. 2020
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

Akzeptierte Antwort

Alan Stevens
Alan Stevens am 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

Weitere Antworten (0)

Kategorien

Mehr zu Characters and Strings finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by