Question

In: Advanced Math

Need a MATLAB code for LU factorization(both partial and complete pivoting) of 10 random matrices of...

Need a MATLAB code for LU factorization(both partial and complete pivoting) of 10 random matrices of order 5x5.

Please do not comment like I don't have computer or I don't know matlab. If you please answer otherwise you can skip.

Solutions

Expert Solution

%%% Matlab code LU decomposition with complete pivoting

clc;
close all;
clear all;
tol=10^(-8);
A=rand(5,5);
[n m] = size(A);
% pivot vectors
p = (1:n)';
q = (1:m)';
% temp storage
rt = zeros(m,1); % row temp
ct = zeros(n,1); % col temp
t = 0; % scalar temp
for k = 1:min(n-1,m)
% determine pivot
[cv ri] = max(abs(A(k:n,k:m)));
[rv ci] = max(cv);
rp = ri(ci)+k-1;
cp = ci+k-1;
  
% swap row
t = p(k);
p(k) = p(rp);
p(rp) = t;
rt = A(k,:);
A(k,:) = A(rp,:);
A(rp,:) = rt;
  
% swap col
t = q(k);
q(k) = q(cp);
q(cp) = t;
ct = A(:,k);
A(:,k) = A(:,cp);
A(:,cp) = ct;
  
if abs(A(k,k)) >= tol
rows = (k+1):n;
cols = (k+1):m;
A(rows,k) = A(rows,k)/A(k,k);
A(rows,cols) = A(rows,cols)-A(rows,k)*A(k,cols);
else
% stop factorization if pivot is too small
break
end
end
% final column swap if m > n
if m > n
% determine col pivot
[cv ci] = max(abs(A(n,n:m)));
cp = ci+n-1;
  
% swap col
t = q(n);
q(n) = q(cp);
q(cp) = t;
ct = A(:,n);
A(:,n) = A(:,cp);
A(:,cp) = ct;
end
% produce L and U matrices
% these are sparse if L and U are sparse
l = min(n,m);
L = tril(A(1:n,1:l),-1) + speye(n,l);
U = triu(A(1:l,1:m));
disp('Matrix is : ');
A
disp('L matrix is :');
L
disp('U matrix is :');
U

OUTPUT:

Matrix is :

A =

0.9340 0.9172 0.5678 0.4733 0.3371
0.0578 0.7778 0.7244 0.1693 0.5494
0.5683 0.0823 0.3714 -0.0318 0.2326
0.8342 -0.2769 0.2891 0.2773 -0.1844
0.0812 0.3563 -0.0495 0.9003 0.0844

L matrix is :

L =

1.0000 0 0 0 0
0.0578 1.0000 0 0 0
0.5683 0.0823 1.0000 0 0
0.8342 -0.2769 0.2891 1.0000 0
0.0812 0.3563 -0.0495 0.9003 1.0000

U matrix is :

U =

0.9340 0.9172 0.5678 0.4733 0.3371
0 0.7778 0.7244 0.1693 0.5494
0 0 0.3714 -0.0318 0.2326
0 0 0 0.2773 -0.1844
0 0 0 0 0.0844

%%%% Partial pivoting

clc;
close all;
clear all;
A=rand(5,5);
[m, n] = size(A);
L=eye(n);
P=eye(n);
U=A;
for k=1:m-1
pivot=max(abs(U(k:m,k)));
for j=k:m
if(abs(U(j,k))==pivot)
ind=j;
break;
end
end
U([k,ind],k:m)=U([ind,k],k:m);
L([k,ind],1:k-1)=L([ind,k],1:k-1);
P([k,ind],:)=P([ind,k],:);
for j=k+1:m
L(j,k)=U(j,k)/U(k,k);
U(j,k:m)=U(j,k:m)-L(j,k)*U(k,k:m);
end
  
end

disp('Matrix is : ');
A
disp('L matrix is :');
L
disp('U matrix is :');
U

OUTPUT:

Matrix is :

A =

0.4173 0.4893 0.7803 0.1320 0.2348
0.0497 0.3377 0.3897 0.9421 0.3532
0.9027 0.9001 0.2417 0.9561 0.8212
0.9448 0.3692 0.4039 0.5752 0.0154
0.4909 0.1112 0.0965 0.0598 0.0430

L matrix is :

L =

1.0000 0 0 0 0
0.9555 1.0000 0 0 0
0.4417 0.5960 1.0000 0 0
0.0526 0.5817 0.6577 1.0000 0
0.5195 -0.1474 -0.1958 -0.2738 1.0000

U matrix is :

U =

0.9448 0.3692 0.4039 0.5752 0.0154
0 0.5472 -0.1442 0.4065 0.8065
0 0 0.6878 -0.3644 -0.2527
0 0 0.0000 0.9150 0.0495
0 0 0 0 0.1179

>>


Related Solutions

Need a python code for LU factorization( for partial pivoting and complete pivoting) of a random...
Need a python code for LU factorization( for partial pivoting and complete pivoting) of a random matrix size 5x5.
Solve the following system of equations using LU factorization without partial pivoting: 2x1 - 6x2 -...
Solve the following system of equations using LU factorization without partial pivoting: 2x1 - 6x2 - x3 = -38 -3x1 - x2 + x3 = -34 -8x1 + x2 - 2x3 = -20
Solve the following set of equations with LU factorization with pivoting: 3x1 -2x2 + x3 =...
Solve the following set of equations with LU factorization with pivoting: 3x1 -2x2 + x3 = -10 2x1 + 6x2- 4x3 = 44 -8x1 -2x2 + 5x3 = -26 Please show all steps
Question1 (50 pts): LU Factorization Code for Square Matrices without Row Exchange I want you to...
Question1 (50 pts): LU Factorization Code for Square Matrices without Row Exchange I want you to write an LU decomposition program in Matlab for square matrices (n×n) where row exchange is not necessary (that is no pivot is 0). Here are some hints and requirements for your matlab code. You should write comments for every procedure. Make sure that your code is well indexed (see below). Otherwise it will be hard for me to follow and bad programming practice for...
Implement Gaussian elimination(with partial pivoting) and backward substitu- tion in MATLAB. You need to submit your...
Implement Gaussian elimination(with partial pivoting) and backward substitu- tion in MATLAB. You need to submit your code on moodle page.
Implement the recursive LU factorization algorithm in Python. Use plenty of comments to explain your code....
Implement the recursive LU factorization algorithm in Python. Use plenty of comments to explain your code. While you are coding, it is helpful to break up your code into sub-functions and test the sub-functions as you go along.
i need matlab code of heat equation in 1D with convection ?
i need matlab code of heat equation in 1D with convection ?
pls, I need Matlab code for, OFDM modulation (Matlab demo by at least 4 carriers)
pls, I need Matlab code for, OFDM modulation (Matlab demo by at least 4 carriers)
Hello, I need the Matlab code of the Fourier Transform without using the Matlab functions fft...
Hello, I need the Matlab code of the Fourier Transform without using the Matlab functions fft and dft. Applied to discrete signals. If you can with an example.Thank you!!
I need a working MATLAB CODE for the Gram Schimdt process Please give the code fast...
I need a working MATLAB CODE for the Gram Schimdt process Please give the code fast Its urgent The code must run on any matrix given It should be a generic code Dont answer any wrong code or else i will badly dislike
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT