Guia Nro 06: Sistema de Ecuaciones Directo: Function
Guia Nro 06: Sistema de Ecuaciones Directo: Function
Guia Nro 06: Sistema de Ecuaciones Directo: Function
1. PRACTICA DE LABORATORIO
function X= sustitucionRegresiva(A,B)
n=length(B);
X=zeros (n,1);
X(n)= B(n)/A(n,n);
for k=n-1:-1:1
X(k)=(B(k)-A(k,k+1:n)*X(k+1:n))/A(k,k);
end
end
>> A=[1,2,3;0,-4,2;0,0,2]
A=
1 2 3
0 -4 2
0 0 2
>> B=[6;-2;2]
B=
-2
>> X=sustitucionRegresiva(A,B)
X=
1
2. METODO DE ELIMINACION DE GAUS
2.1. EJERCICIO
function [x,err]=gauss_jordan_elim(A,b)
A = [1 2 3;0 -4 2;0 0 2] % input for augmented matrix A
b = [6;-2; 2] % intput for matrix B
[n,m]=size(A); % finding the size of matrix A
err =0; % calculation of error
x=zeros(n,1); % calling fuction zero
if n ~= m
disp('error: n~=m'); % displaying error if found
err = 1;
end % end of the scope of if
if length(b) ~= n % finding the legth of matrix B
disp('error: wrong size of b'); % displaying error, if found
err = 2;
else
if size(b,2) ~= 1
b=b';
end % end of the scope of if-else
if size(b,2) ~= 1
disp('error: b is a matrix'); % displaying erron in matrix B
err = 3;
end
end
if err == 0
Aa=[A,b];
for i=1:n
[Aa(i:n,i:n+1),err]=gauss_pivot(Aa(i:n,i:n+1));
if err == 0
Aa(1:n,i:n+1)=gauss_jordan_step(Aa(1:n,i:n+1),i);
end
end
x=Aa(:,n+1);
end
A=0;
function A1=gauss_jordan_step(A,i) % calling of fuction function
%ejemplo
% A= [61 0 -20; 0 23 -9; -20 -24 64]
% B= [-21 34 12]
% X= gauss_jordan_elim(A, B')
>> gauss_jordan_elim
A =
1 2 3
0 -4 2
0 0 2
b =
-2
ans =
1
2.2. EJERCICIO
function X = EcuaGaussJordan(A)
%datos:
% X: solucion del sistema lineal AX=B,
% A: es una matriz de coeficientes y termino independiente(matriz
ampliada)
% n: numero de ecuaciones
% m: numero de columnas
n = length(A);
m = size(A);
m = m(1,1);
for k= 1:n-1
for i= 1:n-1
if abs(i-k)>0
f= -A(i,k)/A(k,k);
for j= k:n
A(i,j)= f*A(k,j)+ A(i,j);
end
end
end
g=A(k,k);
for j= k:n
A(k,j)= A(k,j)/g;
end
end
for i=1:m
B(i,1)= A(i,n);
end
X= B;
end
%ejemplo 1:
%C=[4.1 0.1 0.2 0.2; 3.1 5.3 0.9 0.1; 0.2 0.3 3.2 0.2; 0.1 0.1 0.2 -9.1]
%D=[21.14 -17.82 9.02 17.08
%A=[C D]
%X = EcuaGaussJordan(A)
>> C=[4.1 0.1 0.2 0.2; 3.1 5.3 0.9 0.1; 0.2 0.3 3.2 0.2; 0.1 0.1 0.2 -9.1]
A=[C D]
X = EcuaGaussJordan(A)
C=