0% found this document useful (0 votes)
10 views2 pages

Clear All

This document contains MATLAB code to perform LU decomposition on matrices A and B. It initializes several matrices including L, U, P, D, X, and C to be used in the LU decomposition algorithm. It then loops through the rows and columns of matrix U, finding the maximum pivot element on each row and performing row swaps and row operations to transform the matrix into lower and upper triangular form. It uses the decomposed L and U matrices to solve the system of equations PA=LU and PB=LU for the vectors D and X.
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)
10 views2 pages

Clear All

This document contains MATLAB code to perform LU decomposition on matrices A and B. It initializes several matrices including L, U, P, D, X, and C to be used in the LU decomposition algorithm. It then loops through the rows and columns of matrix U, finding the maximum pivot element on each row and performing row swaps and row operations to transform the matrix into lower and upper triangular form. It uses the decomposed L and U matrices to solve the system of equations PA=LU and PB=LU for the vectors D and X.
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/ 2

clear all

clc

A=input('ingrese la matriz A: ')


B=input('ingrese la matriz B: ')

[n,n]=size(A);
X=zeros(n,1);
D=zeros(n,1);
C=zeros(1,n);
Cp=zeros(1,n);
L=eye(n);
P=eye(n);
U=zeros(n,n);
U=A;

for q=1:n-1

[y,j]=max(abs(U(q:n,q)))
C=U(q,:)
U(q,:)=U(j+q-1,:)
U(j+q-1,:)=C

Cp=P(q,:);
P(q,:)=P(j+q-1,:);
P(j+q-1,:)=Cp;

if A(q,q)==0
'A es singular'
break
end

%Calculo del multiplicador,

for k=q+1:n
m=U(k,q)/U(q,q)
L(k,q) = m
U(k,q:n)=U(k,q:n)-m*U(q,q:n)
end
end
h=L(2,1);
L(2,1)=L(3,1);
L(3,1)=h;

L
%comprobacion

PA=P*A
LU=L*U
PB=P*B

D(1)=PB(1)/L(1,1);
for k=2:n
D(k)=(PB(k)-L(k,1:k-1)*D(1:k-1))/L(k,k);
end
X(n)=D(n)/U(n,n);

for k=n-1:-1:1
X(k)=(D(k)-U(k,k+1:n)*X(k+1:n))/U(k,k)
end

Segunda pregunta
function det = determinante(A)
if size(A,1) == size(A,2)
switch size(A,1)
case 1
det = A(1,1);
case 2
det = A(1,1)*A(2,2) - A(1,2)*A(2,1);
otherwise
det = 0;
for i = 1:1:size(A,1)
%Funcion menor
for k = 1:1:size(A,1)
for j = 1:1:size(A,1)
if ~(k == 1 || i == j)
C(k,j)=A(k,j);
end
end
end
C(1,:) = [];

if(i ~= size(A,1))
C(:,i) = [];
end
det = det + ((-1)^(1 + i))*A(1,i)*determinante(C);
end
end
else
disp('Solo es para matrices cuadradas')
end
end

You might also like