0% found this document useful (0 votes)
42 views4 pages

Guia Nro 06: Sistema de Ecuaciones Directo: Function

This document provides instructions for solving systems of linear equations using two methods: backward substitution and Gaussian elimination. It includes the following: 1. A MATLAB function for backward substitution to solve the system Ax=b. 2. A description of the Gaussian elimination method along with a MATLAB function to perform Gaussian elimination on a matrix A and vector b to solve the system Ax=b. 3. Examples of using the Gaussian elimination function to solve sample systems of linear equations.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
42 views4 pages

Guia Nro 06: Sistema de Ecuaciones Directo: Function

This document provides instructions for solving systems of linear equations using two methods: backward substitution and Gaussian elimination. It includes the following: 1. A MATLAB function for backward substitution to solve the system Ax=b. 2. A description of the Gaussian elimination method along with a MATLAB function to perform Gaussian elimination on a matrix A and vector b to solve the system Ax=b. 3. Examples of using the Gaussian elimination function to solve sample systems of linear equations.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

GUIA NRO 06: SISTEMA DE ECUACIONES DIRECTO

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

[n,m]=size(A); % determination of size of matrix A


A1=A; % assigning A to A1
s=A1(i,1);
A1(i,:) = A(i,:)/s;
k=[[1:i-1],[i+1:n]];
for j=k
s=A1(j,1);
A1(j,:)=A1(j,:)-A1(i,:)*s;
end % end of for loop
function [A1,err]=gauss_pivot(A) % calling of fucntion
[n,m]=size(A); % finding the size of matrix A
A1=A; % process of assigning
err = 0; % error flag
if A1(1,1) == 0
check = logical(1); % logical(1) - TRUE
i = 1;
while check
i = i + 1;
if i > n
disp('error: matrix is singular');
err = 1;
check = logical(0);
else
if A(i,1) ~= 0 & check
check = logical(0);
b=A1(i,:); % process to change row 1 to i
A1(i,:)=A1(1,:);
A1(1,:)=b;
end
end
end
end

%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]

D=[21.14 -17.82 9.02 17.08

A=[C D]

X = EcuaGaussJordan(A)

C=

4.1000 0.1000 0.2000 0.2000

3.1000 5.3000 0.9000 0.1000

0.2000 0.3000 3.2000 0.2000

0.1000 0.1000 0.2000 -9.1000

You might also like