I want the Matlab program for the matrix A as given above 4 2 1 3 1-1 2 0 5 1. C
ID: 3145027 • Letter: I
Question
I want the Matlab program for the matrix A as given above
4 2 1 3 1-1 2 0 5 1. Consider the n x n (with n-3) matrix A (a) Decompose A into LU (by hand, no pivoting (b) Compare your result against the LU decomposition script written in the last homework assign ment (c) Compare your decomposition against the results of lu(A) in MATLAB. What happened? It may be useful to get more complete output using [L,U,P] = lu (A). 2. MATLAB: Write a MATLAB script that, given A, implements LU decomposition with partial pivoting At each step, make sure not to divide by zero and let the routine display an error if that were about to happen. Return L, U, and P, such that PA LU. Use your script on the above matrix and see whether you get the same result as MATLAB, nowExplanation / Answer
2)
first define a program findpiv ,which is Used by plu to find a pivot for Gaussian elimination.
now in same folder make a new program plu which does the same work as [L,U,P] = lu(A)
function [P, L, U, pivcol, sign] = plu(A)
% plu Rectangular PA=LU factorization *with row exchanges*.
%
% [P, L, U] = plu(A), for a rectangular matrix A, uses Gaussian elimination
% to compute a permutation matrix P, a lower triangular matrix L and
% an upper trapezoidal matrix U so that PA = LU.
% U is the same size as A.
% P and L are square, with as many rows as A.
% sign = det(P); it is 1 or -1.
%
% See also elim, slu, lu, rref, partic, nulbasis, colbasis.
[m, n] = size(A);
P = eye(m, m);
L = eye(m, m);
U = zeros(m, n);
pivcol = [];
tol = sqrt(eps);
sign = 1;
p = 1;
for k = 1:min(m, n)
[r, p] = findpiv(A(k:m, p:n), k, p, tol);
if r ~= k
A([r k], 1:n) = A([k r], 1:n);
if k > 1, L([r k], 1:k-1) = L([k r], 1:k-1); end
P([r k], 1:m) = P([k r], 1:m);
sign = -sign;
end
if abs(A(k, p)) >= tol
pivcol = [pivcol p];
for i = k+1:m
L(i, k) = A(i, p) / A(k, p);
for j = k+1:n
A(i,j) = A(i, j) - L(i, k)*A(k, j);
end
end
end
for j = k:n
U(k, j) = A(k, j) * (abs(A(k, j)) >= tol);
end
if p < n, p = p+1; end
end
if nargout < 4
nopiv = 1:n;
nopiv(pivcol) = [];
if ~isempty(pivcol), disp('Pivots in columns:'), disp(pivcol); end
if ~isempty(nopiv), disp('No pivots in columns:'), disp(nopiv); end
rank = length(pivcol);
if rank > 0
roworder = P*(1:m)';
disp('Pivots in rows:'), disp(roworder(1:rank)'); end
end
L
U
end
using matlab inbuilt function
A =[4 2 1 ; 3 3/2 -1 ; 2 0 5];
>> A
A =
4.0000 2.0000 1.0000
3.0000 1.5000 -1.0000
2.0000 0 5.0000
>> [L,U,P] = lu(A)
L =
1.0000 0 0
0.5000 1.0000 0
0.7500 0 1.0000
U =
4.0000 2.0000 1.0000
0 -1.0000 4.5000
0 0 -1.7500
P =
1 0 0
0 0 1
0 1 0
using matlab program
plu(A)
Pivots in columns:
1 2 3
Pivots in rows:
1 3 2
L =
1.0000 0 0
0.5000 1.0000 0
0.7500 0 1.0000
U =
4.0000 2.0000 1.0000
0 -1.0000 4.5000
0 0 -1.7500
ans =
1 0 0
0 0 1
0 1 0
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.