problem with back-substitution code
30 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Sienna Phillips
am 20 Aug. 2020
Kommentiert: Shaima Al-Shalwi
am 5 Sep. 2022
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
0 Kommentare
Akzeptierte Antwort
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
1 Kommentar
Weitere Antworten (0)
Siehe auch
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!