Write a function in Matlab that takes as input a size n and

Write a function in Matlab that takes as input a size n and a tridiagonal matrix given as three vectors: n × 1 vector v representing the main diagonal, (n 1) × 1 vector w representing the upper diagonal, and (n 1) × 1 vector z representing the lower diagonal. Have this function output the LU factorization with the U as two vectors and the L as one vector representing the diagonals. Also output the number of flops used. Use only basic programming.

(a) Write out or print out your function.

(b) Run the case with n = 10, v the vector of 2’s, w and z the vector of 1’s, and b the vector of 1’s. Write down your results for the diagonals of L and U.

(c) Run the case with n = 50 and n = 100 with v the vector of 2’s, w and z the vector of 1’s. Write down your results for the number of flops used

Solution

Part a) Matlab Function LU_factor.m

function[L,U1,U2,flp]= LU_factor(n,v,w,z)
    flp = 0; % initially flp is zeros
    U2 = w; % Upper diag of U is same as w
    U1(1) = v(1); % first element in diag of U is same
    for i = 2:n % for the computation of elements 2 to n
        L(i-1) = z(i-1)/U1(i-1); % the lower diag of L
        U1(i) = v(i)-L(i-1)*U2(i-1); % The giad of U
        flp = flp +3; % 1:/, 1:-, 1:*
    end
end

Part b)

>> n = 10;
>> v = 2*ones(1,n);
>> w = -ones(1,n-1);
>> z = w;
>> [L,U1,U2,flp]= LU_factor(n,v,w,z);
Attempted to access z(10); index out of bounds because numel(z)=9.

Error in LU_factor (line 10)
        L(i) = z(i)/U1(i);

>> [L,U1,U2,flp]= LU_factor(n,v,w,z);
>> l = diag(ones(1,n))+diag(L,-1);
>> u = diag(U1)+diag(U2,1);
>> l*u

ans =

     2    -1     0     0     0     0     0     0     0     0
    -1     2    -1     0     0     0     0     0     0     0
     0    -1     2    -1     0     0     0     0     0     0
     0     0    -1     2    -1     0     0     0     0     0
     0     0     0    -1     2    -1     0     0     0     0
     0     0     0     0    -1     2    -1     0     0     0
     0     0     0     0     0    -1     2    -1     0     0
     0     0     0     0     0     0    -1     2    -1     0
     0     0     0     0     0     0     0    -1     2    -1
     0     0     0     0     0     0     0     0    -1     2

>> L

L =

Columns 1 through 8

   -0.5000   -0.6667   -0.7500   -0.8000   -0.8333   -0.8571   -0.8750   -0.8889

Column 9

   -0.9000

>> U1

U1 =

Columns 1 through 8

    2.0000    1.5000    1.3333    1.2500    1.2000    1.1667    1.1429    1.1250

Columns 9 through 10

    1.1111    1.1000

>> U2

U2 =

    -1    -1    -1    -1    -1    -1    -1    -1    -1

Part c)

>> n=50;
>> v = 2*ones(1,n);
>> w = -ones(1,n-1);
>> z = w;
>> [L,U1,U2,flp]= LU_factor(n,v,w,z);
>> flp

flp =

   147

>> n=100;
>> v = 2*ones(1,n);
>> w = -ones(1,n-1);
>> z = w;
>> [L,U1,U2,flp]= LU_factor(n,v,w,z);
>> flp

flp =

   297


Remark

Number of flops is less then 3n

Write a function in Matlab that takes as input a size n and a tridiagonal matrix given as three vectors: n × 1 vector v representing the main diagonal, (n 1) ×
Write a function in Matlab that takes as input a size n and a tridiagonal matrix given as three vectors: n × 1 vector v representing the main diagonal, (n 1) ×

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site