- Forward Elimination Corrected: Properly calculates the factor (k) for each element to be eliminated, ensuring correct elimination of elements below the pivot.
- Back Substitution Implemented: Solves for variables from the bottom up after converting (A) to an upper triangular form, ensuring accurate calculation of solution vector (x).
The formula doesn't calculate
10 次查看(过去 30 天)
显示 更早的评论
I am calculating a matrix using Gaussian elimination, but the calculation does not work under the following conditions.
A=[2 0 1; -2 4 1; -1 -1 3] b=[8 ; 0 ;2 ] x=[x1; x2; x3]
I think it doesn't work because there is a 0 in row 1 and column 2 of A. How should I change the code? Have a nice day everyone:)
here is a code
clc; clear all; close all;
A = [2 0 1 ; -2 4 1 ;-1 -1 3];
b = [8 0 2]';
%b = [7; 8 ;3];
sz = size(A,1);
disp ([A b]);
for i = 2 :1: sz
for j = 1:1:i-1
k = A(j,j)/A(i,j);
A(i,:) = k * A(i,:) - A(j,:);
b(i) = k * b(i) - b(j);
disp([A b]);
end
end
for i = sz-1:-1:1
for j = sz:-1:i+1
k = A(j,j)/A(i,j);
A(i,:) = k*A(i,:)-A(j,:);
b(i) = k* b(i) - b(j);
disp([A b]);
end
end
x = b./diag(A);
disp([A b]./diag(A));
disp(x);
0 个评论
采纳的回答
Pooja Kumari
2024-4-12
编辑:Pooja Kumari
2024-4-12
Hello,
The issue with your code is not because there is a 0 in row 1 and column 2 of A. Key corrections in your code is as follows:
clc; clear all; close all;
A = [2 0 1 ; -2 4 1 ;-1 -1 3];
b = [8; 0; 2];
sz = size(A,1);
disp ([A b]);
% Forward elimination
for i = 1:sz-1
for j = i+1:sz
k = A(j,i)/A(i,i);
A(j,:) = A(j,:) - k * A(i,:);
b(j) = b(j) - k * b(i);
end
end
% Back substitution
x = zeros(sz,1); % Initialize solution vector
for i = sz:-1:1
x(i) = (b(i) - A(i,i+1:sz)*x(i+1:sz))/A(i,i);
end
disp(x);
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Data Type Conversion 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!