Why do I keep getting a zero for x(9) for Backward subsitution?
1 次查看(过去 30 天)
显示 更早的评论
This is my code for backward subsitution. I keep getting zero for x(9) each run. I'm not sure what I'm doing wrong.
%% b
U = triu(randi([0,20],10,10));
b = randi([0,20],10,1);
x = backward_sub(U,b)
%%
function [x] = backward_sub(U,b) %Initializing function
n= length(b); % Gets the length of b
x = zeros(n,1);
x(n) = b(n) / U(n,n); % gets the known value
for i = n-1:-1:1 % loops backward over the compents of U
sum = 0; %intializing the sum
for j = 1:i-1 %loops over the coments of the sum
sum = sum + (U(i,j)*x(j)); % takes the sum of the components
end
x(j) = (b(j) - sum) / U(i,i); % finds x(j) for every fixed k.
end
end
0 个评论
采纳的回答
Torsten
2023-2-14
编辑:Torsten
2023-2-14
Don't generate random numbers that can become 0 on the diagonal. Or use your code to check if all U(i,i) are not equal 0.
Further see the changes made in backward_sub.
%% b
U = triu(randi([1,20],10,10))
b = randi([0,20],10,1);
x = backward_sub(U,b)
U*x-b
%%
function [x] = backward_sub(U,b) %Initializing function
n= length(b); % Gets the length of b
x = zeros(n,1);
x(n) = b(n) / U(n,n); % gets the known value
for i = n-1:-1:1 % loops backward over the compents of U
sum = 0; %intializing the sum
for j = i+1:n %loops over the coments of the sum
sum = sum + (U(i,j)*x(j)); % takes the sum of the components
end
x(i) = (b(i) - sum) / U(i,i); % finds x(j) for every fixed k.
end
end
5 个评论
Torsten
2023-2-14
You must take the terms to the right of the diagonal to the right-hand side of the i-th equation and then divide by the diagonal element U(i,i) to get x(i). That's why you first sum the elements to the right of the diagonal (from i+1 to n), subtract them from the right-hand side b(i) (b(i)-sum) and then divide by the diagonal element U(i,i).
You should just right down the programmed operations - then you'll see what's going on.
更多回答(1 个)
John D'Errico
2023-2-14
First: NEVER use the variable name sum as a variable name. Why not? Becaause one time you will want to use the FUNCTION sum. And then you will post an anquished question on Answers asking why sum no longer works for you.
As far as why your code does not work? It has a bug in it. Correctly written code will work. ;-)
U = triu(randi([0,20],10,10));
b = randi([0,20],10,1);
What solution do we expect?
U\b
Does my corrected version work?
x = backward_sub(U,b)
You might see there are quite a few changes I made. Two spelling changes in comment, because you were not spelling the word component correctly, and it bugged me. I also refused to name a variable sum. But more importantly, I used the correct loops. And you seem to have been using j in a few places where you needed to use i.
function [x] = backward_sub(U,b) %Initializing function
n= length(b); % Gets the length of b
x = zeros(n,1);
x(n) = b(n) / U(n,n); % gets the known value
for i = n-1:-1:1 % loops backward over the components of U
sumi = 0; %intializing the sum
for j = i+1:n %loops over the components of the sum
sumi = sumi + (U(i,j)*x(j)); % takes the sum of the components
end
x(i) = (b(i) - sumi) / U(i,i); % finds x(i)
end
end
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Operating on Diagonal Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!