Question

In: Advanced Math

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.

  1. 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 you.

  2. This should be a Matlab Function called slu (don’t name it lu because matlab already has a program called lu):

  3. Your program should be a function in Matlab. So your filename should be slu.m (.m is standard for matlab scripts or functions). In order to be a function you should start with

    function [L,U]=slu=slu(A)

    where A is the input square matrix. Always write what a program does using matlab

comment (%) so that it

  1. First step is to determine the size of the matrix. Use size command to obtain the size of a matrix.

    [n,m]=size(A)

  2. Then if n is not equal to m end the program. If n=m continue

  3. So your code should look like this

    function [L,U]=slu=slu(A)
    %LU factorization of square matrices %with no row exchange
    [n,n]=size(A) %determine the size of A Tol=1.e-6 %tolerance level for zero

    for k=1:n %loop over pivots of A if(A(k,k) < tol) %check for pivot

    disp(‘cannot proceed without row exchange’) end %cannot proceed without row exchange L(k,k)=1;

for i=k+1:n %loop over each line to determine lik determine lik

for j=k:n start from row k+1 and col. k remove (pivot row×Lik) from each row of A

end

for j=k:n
write the pivot line to Ubecause pivot is fixed

end end

due 23/10/2019

Question2(50 pts): Solving matrix equation using LU

Next step is to solve the equation using substitution. So you will need to do the standard procedure for LU. First step is to solve Ly=b. And next is to solve Ux=y. For Ly=b you need to go from top to bottom, but for Ux=y you need to go from bottom xn to x1. This function will use slu function so you need to be on the same directory. Name your file “luslv.m”.

Function x=luslv(A,b)
%Solve Ax=b using L&U from slu(A) %No row exchanges

[L,U]=slu(A) %first decompose A to LU

First do a for loop from k=1:n and another for loop for j=1:k-1 and sum the contributions to b(k) from yk-1, yk-2...y1.

Then y(k)=b(k)-s %forward elim. To solve Ly=b

Then solve Ux=y. You need to start from n and go to 1. In matlab you can do it

for k=n:-1:1 %go backwards from n to 1
Sum up contributions from xk+1, xk+2...xn.

Remove the sum from y(k) and divide by the pivot

Solutions

Expert Solution


%%Matlab code for LU factorization and solving
clear all
close all
%Coefficient matrix A
A=[9 -2 6 1;-10.5 -10 100 -4;-9 -2 3 3;-3 2 250 -2];
b=[18;-2;2.5;2];


%Displaying all matrix
fprintf('The A matrix is \n')
disp(vpa(A,2))

fprintf('The b matrix is \n')
disp(vpa(b,2))

%Calling function lup
[L,U,P]=slu(A);
%loop fo finding all x
X=bsub(U,fsub(L,P*b(:,1)));


%Displaying solution matrix
fprintf('The solution matrix X is \n')
disp(vpa(X,12))

%error in solution
X2=A\b;

fprintf('Error in solution of x is %e \n',norm(X-X2))


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%Matlab function for LU factorization using partial pivoting
function [L, U, P] = slu(A)
%A is the coefficient matrix
%L and U are lu factorized value of A
%P is parmutation matrix
%below is the algorithm for Gaussian elimination
    [m, n] = size(A); L=eye(n); P=eye(n); U=A;
    %initialization of L U and P
    %Loop for LU matrix formation for A matrix
    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
        %updated L U and P values
        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],:);
        %Loop for L and U matrix formation
        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
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%Matlab function for backward subtitution
function x=bsub(U,y)
        %Code for Backward subtitution
        x=zeros(length(y),1);
        for i=length(y):-1:1
        s=0;
        for j=length(y):-1:1
            if i~=j
                s=s+U(i,j)*x(j);
            end
          
        end
        x(i)=(y(i)-s)/U(i,i);
        end
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


%%Matlab function for forward subtitution
function y=fsub(L,b)
        %Code for forward subtitution
        y=zeros(length(b),1);
        for i=1:length(b)
        s=0;
        for j=1:length(b)
            if i~=j
                s=s+L(i,j)*y(j);
            end  
        end
        y(i)=(b(i)-s)/L(i,i);
        end
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

   


Related Solutions

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.
I want the code in the following three PHP files without any CSS. Thank you Create...
I want the code in the following three PHP files without any CSS. Thank you Create a Input.php. Create a PHP script that will perform the following tasks: 1. Divide the content area of this page into two sections (Side by Side) 2. On the left side: create a form that accepts the following information from the user a. Text box, Employee Name b. Text box, Employee ID c. Text box, Telephone Number d. Text box, Email Address e. Radio...
So pretty much I need my code without the arrays, or lists. Please and thank you!...
So pretty much I need my code without the arrays, or lists. Please and thank you! Important: You may not use arrays, lists, or similar for your questions. This will be covered in the next module. The objective is to use conditionals in order to achieve the overall task. Checkpoint 3 is a continuation of the “Quiz” Programming Project. This module week, you will implement repetitive tasks in your program while using conditional and iteration statements in C#. Implement a...
I just want the code blanking led for PIC microprocessor in MBLAB X Thank you
I just want the code blanking led for PIC microprocessor in MBLAB X Thank you
can you please create the code program in PYTHON for me. i want to create array...
can you please create the code program in PYTHON for me. i want to create array matrix Nx1 (N is multiple of 4 and start from 16), and matrix has the value of elements like this: if N = 16, matrix is [ 4 4 4 4 -4 -4 -4 -4 4 4 4 4 -4 -4 -4 -4] if N = 64, matrix is [8 8 8 8 8 8 8 8 -8 -8 -8 -8 -8 -8 -8...
Could you modify my code so it meets the following requirement? (Python Flask) I want the...
Could you modify my code so it meets the following requirement? (Python Flask) I want the user to register for account using email and password, then store that data into a text file. Then I want the data to be read when logging in allowing the user to go to home page. -------------Code-------------------- routes.py from flask import Flask, render_template, redirect, url_for, request, session import json, re app = Flask(__name__) '''@app.before_request def before_request(): if 'visited' not in session: return render_template("login.html") else:...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT