Escolar Documentos
Profissional Documentos
Cultura Documentos
6 de septiembre de 2019
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 =
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