This is a sample that I have so far Please someone help http
This is a sample that I have so far. Please someone help! http://s000.tinyupload.com/index.php?file_id=00668741971586412236
http://s000.tinyupload.com/index.php?file_id=80005851781061129146
Part 1 (30 pts): Write a MATLAB function of the form [L,U, P] - get_lu (A) that accepts a square matrix A and returns the LU decomposition of A with the If A is rectangular or singular, then you must notify the user by throwing a suitable error message using MATLAB function error or assert. Your function must perform row exchanges, if necessary, and the pivots must be chosen to reduce roundoff error using the technique of partial pivoting. (Hint: You\'ll need to set a tolerance to determine when a pivot position value is \"zero\" and when A is singular.) permutation matrix P Part 2 (15 pts): Write a MATLAB function named x - get_sol (A,b) that accepts a square matrix A and vector b, and calls your get_lu function to obtain the LU decomposition of A, which is then used to solve the system Ax-b and return the solution vector in x. (Hint: You\'ll need to use forward substitution with L, then backward substitution with U to find the solution.) Part 3 (5 pts): Modify the functions you created in Part 1 and Part 2 to optionally utilize the LDU decom- position. You must only modify your existing functions-do not create new or separate functions! Modify get_lu to be of the form [L, U,P, D] -get_lu (A), where output argument D is optional. If D is omitted in the call to get_lu, then the LU decomposition is performed as in Part 1, and nothing is returned in D. However, if D is included in the function call, then the LDU decomposition is performed, and the D matrix is returned, along with L, P and the corresponding U Modify get_sol to employ either the LU or LDU decomposition to find the solution to Ax - b, depend- ing on which is returned by your modified get_lu function. In other words, if output argument D (from your call to get_lu) exists (see the exist function), then get_sol must use the LDU decomposition; otherwise, get_sol behaves as it did in Part 2, using the LU decomposition.Solution
Code:
Part 1:
function [ L,U,P ] = get_lu(A)
% Check A is square matrix
s = size(A);
if s(1)~=s(2)
fprintf(\'A is not square matrix \ \');
clear x;
return;
end
n = s(1);
L = eye(n);
U = eye(n);
P = A;
for i=1:s(1)
% Reducing rows
if P(i,i)==0
maximum = max(abs(P(i:end,1)));
for k=1:n
if maximum == abs(P(k,i))
temp = P(1,:);
P(1,:) = P(k,:);
P(k,:) = temp;
temp = L(:,1);
L(1,:) = L(k,:);
L(k,:) = temp;
end
end
end
if P(i,i)~=1
temp = eye(n);
temp(i,i)=P(i,i);
U = U * temp;
P(i,:) = P(i,:)/P(i,i); %Ensuring pivots are 1.
end
if i~=s(1)
for j=i+1:length(U)
temp = eye(n);
temp(j,i) = U(j,i);
U = U * temp;
P(j,:) = P(j,:)-P(j,i)*P(i,:);
end
end
end
P = P\';
end
Part2:
function[L,U,X]=LU(A,b)
[m n]=size(A);
if (m ~= n )
disp ( \'LR2 error: Square Matrix needed\' );
return;
end
L=zeros(n,n);
U=zeros(n,n);
for i=1:n
% Code to find L
for k=1:i-1
L(i,k)=A(i,k);
for j=1:k-1
L(i,k)= L(i,k)-L(i,j)*U(j,k);
end
L(i,k) = L(i,k)/U(k,k);
end
% Code to find U
for k=i:n
U(i,k) = A(i,k);
for j=1:i-1
U(i,k)= U(i,k)-L(i,j)*U(j,k);
end
end
end
for i=1:n
L(i,i)=1;
end
