Você está na página 1de 6

Ejercicio 9

Velázquez Flores Rubén

6 de septiembre de 2019

1. Programa en octave modificado


function x = ainvb(A,b)
%
% function x = ainvb(A,b)
%
% solve Ax = b
if rank(A)==length(A)
[p,LU]=plu(A);
y = forsub (LU,b,p);
x = backsub (LU,y);
else
disp(’La matriz es singular’)
x=NaN;
end
function [p,A]=plu(A)
%
% function [p,A] = plu (A)
%
% Perform LU decomposition with partial pivoting.
% Upon return the coefficients of L and U replace those
% of the input n by n nonsingular matrix A. The row interchanges
% performed are recorded in the 1D array p.
n = size(A,1);
% initialize permutation vector p
p = 1:n;
% LU decomposition with partial pivoting
for k = 1:n-1
% find row index of relative maximum in column k
[val,q] = max(abs(A(k:n,k)));

1
q = q + k-1;
% interchange rows k and q and record this in p
A([k,q],:)=A([q,k],:);
p([k,q])=p([q,k]);
% compute the corresponding column of L
J=k+1:n;
A(J,k) = A(J,k) / A(k,k);
% update submatrix by outer product
A(J,J) = A(J,J) - A(J,k) * A(k,J);
end
function y = forsub (A,b,p)
%
% function y = forsub (A,b,p)
%
% Given a unit lower triangular, nonsingular n by n matrix A,
% an n-vector b, and a permutation p,
% return vector y which solves Ay = Pb
n = length(b);
% permute b according to p
b=b(p);
% forward substitution
y(1) = b(1);

for k = 2:n
y(k) = b(k) - A(k,1:k-1) * y(1:k-1).’;
end
y=y.’;
function x = backsub (A,b)
%
% function x = backsub (A,b)
%
% Given an upper triangular, nonsingular n by n matrix A and
% an n-vector b, return vector x which solves Ax = b
n = length(b); x = b;
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

2
2. Codigo para resolver los ejercicios
a

%(a)%
A=[1 1 0 1; 2 1 -1 1; 4 -1 -2 2; 3 -1 -1 1]
b=[2; 1; 0; -3]

x=ainvb(A,b)

%(b)%
A=[1 1 0 1; 2 1 -1 1; 4 -1 -2 2; 3 -1 -1 2]
b=[2; 1; 0; -3]

x=ainvb(A,b)

%(C)%
A=[1 1 1; 1 1+10^(-15) 2; 1 2 2]
b=[1; 2; 1]

x=ainvb(A,b)

%(d)%
A=[1 1 1; 10^(20) 10^(20)+10^(5) 2*10^(20); 1 2 2]
b=[1; 2; 1]

x=ainvb(A,b)

%(e)%
A=[1 2 3; 1 2 3 ; 1 2 3]
b=[1; 2; 1]

x=ainvb(A,b)

%(f)%
A=[1 2 3; 0 0 0 ; 3 2 1]
b=[1; 2; 1]

x=ainvb(A,b)

3
3. Resultados
A =

1 1 0 1
2 1 -1 1
4 -1 -2 2
3 -1 -1 1

b =

2
1
0
-3

x =

-2.66667
0.66667
-1.66667
4.00000

A =

1 1 0 1
2 1 -1 1
4 -1 -2 2
3 -1 -1 2

b =

2
1
0
-3

La matriz es singular
x = NaN
A =

4
1.0000 1.0000 1.0000
1.0000 1.0000 2.0000
1.0000 2.0000 2.0000

b =

1
2
1

x =

1.0000
-1.0000
1.0000

A =

1.0000e+00 1.0000e+00 1.0000e+00


1.0000e+20 1.0000e+20 2.0000e+20
1.0000e+00 2.0000e+00 2.0000e+00

b =

1
2
1

La matriz es singular
x = NaN
A =

1 2 3
1 2 3
1 2 3

b =

1
2
1

5
La matriz es singular
x = NaN
A =

1 2 3
0 0 0
3 2 1

b =

1
2
1

La matriz es singular
x = NaN

Você também pode gostar