Question

Using MATLAB, develop an M-file to determine LU factorization of a square matrix with partial pivoting. That is, develop a function called mylu that is passed the square matrix [A] and returns the triangular matrices [L] and [U] and the permutation P. You are not to use MATLAB built-in function lu in your codes. Test your function by using it to solve a system of equations listed below in part 3. Confirm that your function is working properly by verifying that [L][U]=P[A] and by using the MATLAB built-in function lu.

Using MATLAB, develop an M-file to determine matrix inverse based on the LU factorization method above. That is, develop a function called myinv that is passed the square matrix [A] and utilizing codes of part 1 above to return the inversed matrix. You are not to use MATLAB built-in function inv or left-division in your codes. Test your function by using it to solve a system of equations listed below in part 3. Confirm that your function is working properly by verifying that [A]ሾܣሿିଵ=[I] and by using the MATLAB built-in function inv.

Using MATLAB, develop an M-file for the Gauss-Seidel Method to solve the system of equations listed below until the percent relative error falls below ߝ௦ = 5%.

Please solve all parts.

1. Using MATLAB, develop an M-file to determine LU factorization of a square matrix with partial pivoting. That is, develop a function called mylu that is passed the square matrix [A] and returns the triangular matrices [L] and [U] and the permutation P. You are not to use MATLAB built-in function lu in your codes. Test your function by using it to solve a system of equations listed below in part 3. Confirm that your function is working properly by verifying that [L]lU] P[A] and by using the MATLAB built-in function lu 2. Using MATLAB, develop an M-file to determine matrix inverse based on the LU factorization method above. That is, develop a function called myinv that is passed the square matrix [A] and utilizing codes of part 1 above to return the inversed matrix. You are not to use MATLAB built-in function inv or left-division in your codes. Test your function by using it to solve a system of equations listed below in part 3. Confirm that your function is working properly by verifying that [A][Al- and by using the MATLAB built-in function inv. 3. Using MATLAB, develop an M-file for the Gauss-Seidel Method to solve the system of equations listed below until the percent relative error falls below 5% X1 + X2 + 5X3 -21.5 3X1-6X2 + 2X3 =-61.5 10X1 + 2X2-X3-27 Hints: Your M-file codes for all 3 problems must be able to handle generally a square matrix of general size n x n (i.e. not just limited to the 3 x 3 matrix in the example above)

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Part 1: Program Screen Shot:

Sample Output:

Part 1 Program Code to Copy :

% create a function mylu
% it is passed the square matrix A
% returns to the triangular matrix
% functio [L], [U] and permutation p.
function [L,U,P] = myLu(A)

% store the length of square matrix A
s=length(A);

% A matrix is assigned to U
U=A;

L=zeros(s,s);
PivVot=(0:s-1)';
for j=1:s,


% Check out the pivot voting
% The maximum numeric value in the column first.
[~,inditi]=max(abs(U(j:s,j)));
inditi=inditi+(j-1);
tri=PivVot(j); PivVot(j)=PivVot(inditi); PivVot(inditi)=tri;
tri=L(j,1:j-1); L(j,1:j-1)=L(inditi,1:j-1); L(inditi,1:j-1)=tri;
tri=U(j,j:end); U(j,j:end)=U(inditi,j:end); U(inditi,j:end)=tri;

% Check out the LU factorization
L(j,j)=1;

% loop to check out the L and U
for i=(1+j):size(U,1)
c= U(i,j)/U(j,j);
U(i,j:s)=U(i,j:s)-U(j,j:s)*c;
L(i,j)=c;
end
end

% Pivot is the matrix
P=zeros(s,s);
P(PivVot(:)*s+(1:s)')=1;
end

Part 2: Program Screen Shot:

Sample output:

octave:5 A*Ainv ans[1.0000 -0.0000 0 0.0000 1.0000 0.0000 0.0000 0.0000 1.0000] 1.0001e+00 -1.1736e-04 1.1980e-05 6.7930e-05

Part 2: Program code to copy:

% create the Ainv inverse function
function Ainv=myinv(A)

% used to utilizing the part 1 function to find the LU decomposition
[L,U,P] = myLu(A);

% Check out the identity matrix
I =eye(size(A));
s=size(A,1);
Ainv=zeros(size(A));
for i=1:s
b=I(:,i);

% A inverse function of triangular forward and backward sub matrix.
Ainv(:,i)=TriBackS(U,TriForS(L,P*b));
end

% create a function of Triangular for submatrix.
function C=TriForS(L,b)
s=length(b);
C=zeros(s,1);
C(1)=b(1)/L(1,1);
for j=2:s
C(j)=(b(j) -sum(L(j,1:j-1)'.*C(1:j-1)))/L(j,j);
% end of the funtion
end

% Create a function of triangular backward Submatrix
function C=TriBackS(U,b)
s=length(b);
C=zeros(s,1);
C(s)=b(s)/U(s,s);

% loop to enter the triangual backward Submatrix element
for j=(s-1):-1:1
C(j)=(b(j) -sum(U(j,j+1:end)'.*C(j+1:end)))/U(j,j);

% end of the function
end

Part 3: Program code to Screen Shot:

Sample Output:

Part 3: Program Code to Copy:

% clear all the memory

clear all

% close all the previous function.

close all

% clear all the screen

clc

% Input the any A square matrix

A = [1 1 5;

-3 -6 2;

10 2 -1];

% The constant vector of coefecient matrix M

M = [-21.5;-61.5;27];

n = length(M);

X = zeros(n,1);

Error = ones(n,1);

% loop to verify the matrix is diagnol

for i = 1:n

j = 1:n;

j(i) = [];

B = abs(A(i,j));

% Verify the diagnol value of the matrix and check is greater that or not

% with remaining row values.

Check(i) = abs(A(i,i)) - sum(B);

if Check(i) < 0

fprintf(' Matrix is diagonally strict %2i\n\n',i)

end

end

% Initialize iteration of the method

iteration = 0;

% Check the relative error falls below the 5%

while max(Error) > 0.05

% goes to the etration step by step

iteration = iteration + 1;

Z = X;

% loop to enter the lement of Gauess_seidel Method

for i = 1:n

j = 1:n;

% Unknown remaining coefiecient is eliminate

j(i) = [];

Xtemp = X;

Xtemp(i) = [];

% coefiecient matrix store

X(i) = (M(i) - sum(A(i,j) * Xtemp)) / A(i,i);

end

% evluauvate the eroor from the iteration

Xsolution(:,iteration) = X;

Error= sqrt((X - Z).^2);

end

% The Gauss_Seidel Method

GaussSeidelTable = [1:iteration;Xsolution]';

MatrixSquare = [A X M]

Add a comment
Know the answer?
Add Answer to:
Using MATLAB, develop an M-file to determine LU factorization of a square matrix with partial pivoting....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT