Pivot matrix function help

46 visualizaciones (últimos 30 días)
Luis Gonzalez
Luis Gonzalez el 7 de Mzo. de 2022
Respondida: Siraj el 4 de Dic. de 2023
hi i wanna make a code that does the Pivot matrix thing from the Gauss method please
i need help with a pivot matrix gauss method code
function [A,b]=pivotea(A,b)
n = size(A, 1);
r = zeros(n, 1);
for i = 1 : 1 : n
r(i) = i;
end
x = zeros(n, 1);
for k = 1 : 1 : n
max = abs(A(r(k), r(k)));
max_pos = k;
for l = k : 1 : n
if abs(A(r(l), r(k))) > max
max = abs(A(r(l), r(k)));
max_pos = l;
end
end
temp_r = r;
r(k) = temp_r(max_pos);
r(max_pos) = temp_r(k);
for i = 1 : 1 : n
if i ~= k
zeta = A(r(i), k) / A(r(k), k);
for j = k : 1 : n
A(r(i), j) = A(r(i), j) - A(r(k), j) * zeta;
end
b(r(i)) = b(r(i)) - b(r(k)) * zeta;
end
end
end
for i = 1 : 1 : n
x(i) = b(r(i)) / A(r(i), i);
end
end
look i have this now problem is whenever i try pivot a matrix some spaces are left on 0 like here
Anueva =
0 -0.3636 0 0
-11.0000 0 0 -0.0000
0 0 0 -3.0486
0 0 61.7500 0
when it should be like
  2 comentarios
Matt J
Matt J el 7 de Mzo. de 2022
It does not appear that you have begun anything. That makes it difficult for us to help you finish.
Luis Gonzalez
Luis Gonzalez el 9 de Mzo. de 2022
i need help with a pivot matrix gauss method code
function [A,b]=pivotea(A,b)
n = size(A, 1);
r = zeros(n, 1);
for i = 1 : 1 : n
r(i) = i;
end
x = zeros(n, 1);
for k = 1 : 1 : n
max = abs(A(r(k), r(k)));
max_pos = k;
for l = k : 1 : n
if abs(A(r(l), r(k))) > max
max = abs(A(r(l), r(k)));
max_pos = l;
end
end
temp_r = r;
r(k) = temp_r(max_pos);
r(max_pos) = temp_r(k);
for i = 1 : 1 : n
if i ~= k
zeta = A(r(i), k) / A(r(k), k);
for j = k : 1 : n
A(r(i), j) = A(r(i), j) - A(r(k), j) * zeta;
end
b(r(i)) = b(r(i)) - b(r(k)) * zeta;
end
end
end
for i = 1 : 1 : n
x(i) = b(r(i)) / A(r(i), i);
end
end
look i have this now problem is whenever i try pivot a matrix some spaces are left on 0 like here
Anueva =
0 -0.3636 0 0
-11.0000 0 0 -0.0000
0 0 0 -3.0486
0 0 61.7500 0
when it should be like

Iniciar sesión para comentar.

Respuestas (1)

Siraj
Siraj el 4 de Dic. de 2023
Hi!
It seems that you are attempting to solve a system of linear equations using Gaussian elimination with partial pivoting. However, there are a couple of issues in the code. Firstly, all row swapping and elimination operations necessary to achieve the echelon form should be applied to the augmented matrix instead of the matrix "A". Secondly, to calculate the value of "x", the process should begin with the last row, a method known as back propagation or back substitution.
Please refer to the modified code below which uses an Augmented matrix "Aug" which serves as the main structure for pivoting operations (Row swapping and Gaussian Elimination).
A = [0.02 0.01 0 0; 1 2 1 0; 0 1 2 1; 0 0 100 200];
b = [0.02;1;4;800];
[reducedA, reduced_b] = pivot(A,b)
A = 4×4
0.0200 0.0100 0 0 1.0000 2.0000 1.0000 0 0 1.0000 2.0000 1.0000 0 0 100.0000 200.0000
b = 4×1
0.0200 1.0000 4.0000 800.0000
Aug = 4×5
0.0200 0.0100 0 0 0.0200 1.0000 2.0000 1.0000 0 1.0000 0 1.0000 2.0000 1.0000 4.0000 0 0 100.0000 200.0000 800.0000
x = 4×1
1 0 0 4
reducedA = 4×4
1.0000 2.0000 1.0000 0 0 1.0000 2.0000 1.0000 0 0 100.0000 200.0000 0 0 0 -0.0500
reduced_b = 4×1
1.0000 4.0000 800.0000 -0.2000
function [A,b]=pivot(A,b)
% Get the length of b
n = length(b);
% Initialize x with zeros
x = zeros(n,1);
% Create the augmented matrix
Aug = [A,b];
%Display
A
b
Aug
% Loop for partial pivoting and echelon form
for j = 1:n-1
% partial pivoting
% Find the maximum value and its position for partial pivoting
[maxi, max_pos] = max(abs(Aug(j:n,j)));
% Swap the rows for partial pivoting
C = Aug(j,:);
Aug(j,:) = Aug(max_pos+j-1,:);
Aug(max_pos+j-1,:) = C;
% echelon form
for i = j+1:n
% Calculate zeta for echelon form
zeta = Aug(i,j)/Aug(j,j);
% Perform row operations for echelon form
Aug(i,:) = Aug(i,:) - zeta*Aug(j,:);
end
end
% Back substitution
% Calculate x(n) using back substitution
x(n) = Aug(n,n+1)/Aug(n,n);
% Loop for back substitution
for k = n-1:-1:1
% Calculate x(k) using back substitution
x(k) = (Aug(k,n+1)-Aug(k,k+1:n)*x(k+1:n))/Aug(k,k);
end
% Display the result
x
% Separate A and b from the augmented matrix
A = Aug(:,1:end-1 );
b = Aug(:,end);
end
You can see the algorithm in action by visiting the following link:
You can explore various methods for solving a system of linear equations in MATLAB by referring to the following link:
To obtain the reduced row echelon form of a matrix in MATLAB, you can utilize the "rref" function. For more information about this function, please visit the following link:
Hope this helps

Categorías

Más información sobre Linear Algebra en Help Center y File Exchange.

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by