Download as pdf or txt
Download as pdf or txt
You are on page 1of 27

Team:

1. Marisnelvys Cabreja Consuegra


2. Dannys García Miranda
Answers
11.1 A=fix(randn(10000));
x=fix(randn(10000,1));

b=A*x;

11.2 A=fix(randn(10000));
x=fix(randn(10000,1));

b=A*x;

tic;[L,U]=lu(A);t1=toc;

% As A*x1=b and A=L*U; so X1= U\(L\b) or L*U*x1=b; U*x1=L\b; y=L\b; x=U\y;

tic;y=L\b ;t2=toc;

tic;x1=U\y ;t3=toc;

tlu=t1+t2+t3

11.3

%QR factorization;
tic; [Q,R] = qr(A);t4=toc;

% As A*x1=b and A=Q*R; so X1= R\(Q\b) or Q*R*x1=b; R*x1=Q\b; y=Q\b; x=R\y;

tic;y2=Q\b ;t5=toc;

tic;x2=R\y ;t6=toc;

tQR=t4+t5+t6

% Relative error;

E1=norm((x-x1),2)/norm(x,2)

E2=norm((x-x2),2)/norm(x,2)
Run 10 times:

A=fix(randn(10000));
x=fix(randn(10000,1));
b=A*x;
tic;[L,U]=lu(A);t1=toc;
% As A*x1=b and A=L*U; so X1= U\(L\b) or L*U*x1=b; U*x1=L\b; y=L\b; x=U\y;
tic;y=L\b ;t2=toc;
tic;x1=U\y ;t3=toc;
tlu=t1+t2+t3
%QR factorization;
tic; [Q,R] = qr(A);t4=toc;
% As A*x2=b and A=Q*R; so X1= R\(Q\b) or Q*R*x1=b; R*x1=Q\b; y2=Q\b; x=R\y2;
tic;y2=Q\b ;t5=toc;
tic;x2=R\y2 ;t6=toc;
tQR=t4+t5+t6
% Relative error;
E1=norm((x-x1),2)/norm(x,2)
E2=norm((x-x2),2)/norm(x,2)

11.4
The execution time of the LU factorization is shorter, since the procedure is
simpler than in the QR factorization, where The construction of an orthonormal
sequence of vectors that generates successive subspaces is performed.

1 2 3 4 5 6 7 8 9 10
tlu = tlu = tlu = tlu = tlu = tlu = tlu = tlu = tlu = tlu =
38.401 39.452 39.915 38.690 39.389 39.857 39.338 38.908 38.870 38.817
tQR = tQR = tQR = tQR = tQR = tQR = tQR = tQR = tQR = tQR =
147.993 159.245 153.067 154.880 152.350 154.946 157.340 146.439 155.449 161.229
E1 = E1 = E1 = E1 = E1 = E1 = E1 = E1 = E1 = E1 =
1,49E-06 5,01E-08 1,91E-06 7,44E-07 8,43E-08 1,68E-06 4,40E-08 1,61E-07 3,30E-07 2,06E-07
E2 = E2 = E2 = E2 = E2 = E2 = E2 = E2 = E2 = E2 =
6,17E-07 5,93E-08 1,01E-06 5,18E-07 1,72E-07 2,71E-06 8,99E-08 4,88E-07 1,93E-07 6,83E-08

11.5
The error is relatively of the same order, sometimes resulting in more accurate QR
factorization.
12.1

n=input(' Write n: ');


A=fix(randn(n)*10);
L=zeros(n); U=zeros(n);
for k=1:n-1
U(k,k:k+1)=A(k,k:k+1)-L(k,1:k-1)*U(1:k-1,k:k+1);
L(k+1,k)=(A(k+1,k)-L(k+1,1:k-1)*U(1:k-1,k))/U(k,k);
L(n,n-1)=(A(n,n-1)-L(n,1:n-2)*U(1:n-2,n-1))/U(n-1,n-1);
U(n,n)=A(n,n)-L(n,1:n-1)*U(1:n-1,n:n);
L(n+1:n,n)=(A(n+1:n,n)-L(n+1:n,1:n-1)*U(1:n-1,n))/U(n,n);
end
L=L+eye(n)
U=U

12.2
To calculate the computational cost of the above code for a tridiagonal nxn
matrix, we will rely on the following code to generate the matrix:

To create a random tridiagonal array

n=input('Write n: ');
A=fix(randn(n)+10);
for k=3:n
A(1:k-2,k)=0;
A(k,1:k-2)=0;
end

If we execute the code of the previous paragraph for n = 10000, we obtain that the
computational cost for the calculation made is:

n=input('Write n: ');
A=fix(randn(n)+10);
for k=3:n
A(1:k-2,k)=0;
A(k,1:k-2)=0;
end
L=zeros(n); U=zeros(n);
tic
for k=1:n-1
U(k,k:k+1)=A(k,k:k+1)-L(k,1:k-1)*U(1:k-1,k:k+1);
L(k+1,k)=(A(k+1,k)-L(k+1,1:k-1)*U(1:k-1,k))/U(k,k);
L(n,n-1)=(A(n,n-1)-L(n,1:n-2)*U(1:n-2,n-1))/U(n-1,n-1);
U(n,n)=A(n,n)-L(n,1:n-1)*U(1:n-1,n:n);
L(n+1:n,n)=(A(n+1:n,n)-L(n+1:n,1:n-1)*U(1:n-1,n))/U(n,n);
end
L=L+eye(n);
toc
For n=10000 we have:
12.3

If T = LU is the LU factorization of a tridiagonal matrix T, the matrices L and U do not


necessarily have to be tridiagonal, since if L is lower triangular the upper diagonal adjacent to
the main diagonal will have zero values and analogously occurs in U.

12.4
D = gallery('dorr',100)
spy(D)
[l u]=lu(D)
spy(1)
spy(u)

For spy(D):
For spy(1):

For spy(u):
Spy plots the sparsity pattern of matrix u. Nonzero values are colored while zero
values are white. The plot displays the number of nonzeros in the matrix, for
example in spy(u), nz = nnz(u)=247, which are the nonzero values in the matrix u
and can be checked:

12.5

The code we have proposed to calculate the factorization lu of any tridiagonal matrix
discarding the factors that are not in the main diagonal and in the adjacent diagonals is:
n=input('Write n: ');
A=fix(randn(n)+10);% This part of the code is to create a tridiagonal matrix any nxn
for k=3:n
A(1:k-2,k)=0;
A(k,1:k-2)=0;
end
L=zeros(n); U=zeros(n);
for k=1:n-1
U(k,k:k+1)=A(k,k:k+1)-L(k,1:k-1)*U(1:k-1,k:k+1);
L(k+1,k)=(A(k+1,k)-L(k+1,1:k-1)*U(1:k-1,k))/U(k,k);
L(n,n-1)=(A(n,n-1)-L(n,1:n-2)*U(1:n-2,n-1))/U(n-1,n-1);
U(n,n)=A(n,n)-L(n,1:n-1)*U(1:n-1,n:n);
L(n+1:n,n)=(A(n+1:n,n)-L(n+1:n,1:n-1)*U(1:n-1,n))/U(n,n);
end
L=L+eye(n);
L=L
U=U
L*U
A

To illustrate the execution of the algorithm in Matlab we have taken n=6:


As seen it works correctly and L*U returns the matrix A.
for the lu factorization:

n=input('Write n: ');
A=fix(randn(n)+10);
[l u]=lu(A);
F=l*u;
for k=3:n
l(1:k-2,k)=0;
l(k,1:k-2)=0;
u(1:k-2,k)=0;
u(k,1:k-2)=0;
F(1:k-2,k)=0;
F(k,1:k-2)=0;
end
l=l
u=u
F
A
As seen it works correctly and l*u returns the matrix A too.
12.6

This is the code to form the tridiagonal matrix starting from the row vectors

n=1000;
b=rand(999,1) ;
a=rand(1000,1) ;
c=rand(999,1);
T=zeros(n);
for k=1:n
T(k,k)=a(k);
end
for k=1:n-1
T(k,k+1)=b(k);
T(k+1,k)=c(k);
end

12.6.1
n=1000;
b=rand(999,1) ;
a=rand(1000,1) ;
c=rand(999,1);
T=zeros(n);
for k=1:n
T(k,k)=a(k);
end
for k=1:n-1
T(k,k+1)=b(k);
T(k+1,k)=c(k);
end
tic
L=zeros(n); U=zeros(n);
for k=1:n-1
U(k,k:k+1)=T(k,k:k+1)-L(k,1:k-1)*U(1:k-1,k:k+1);
L(k+1,k)=(T(k+1,k)-L(k+1,1:k-1)*U(1:k-1,k))/U(k,k);
L(n,n-1)=(T(n,n-1)-L(n,1:n-2)*U(1:n-2,n-1))/U(n-1,n-1);
U(n,n)=T(n,n)-L(n,1:n-1)*U(1:n-1,n:n);
L(n+1:n,n)=(T(n+1:n,n)-L(n+1:n,1:n-1)*U(1:n-1,n))/U(n,n);
end
L=L+eye(n);
toc
C=fix(L*U*10000)==fix(T*10000);
prod(prod(C))

the prod command multiplies the elements of each column and returns a row vector,
if we reapply the prod returns the multiplication of the rows, so if the result is
1 L*U=T=ones(n)
tic
[l u]=lu(T);
F=l*u;
toc
for k=3:n
l(1:k-2,k)=0;
l(k,1:k-2)=0;
u(1:k-2,k)=0;
u(k,1:k-2)=0;
F(1:k-2,k)=0;
F(k,1:k-2)=0;
end
D=fix(F*10000)==fix(T*10000);
prod(prod(D))
Execution times are very approximate.

E1=norm((T-(L*U)),2)/norm(T,2)
E2=norm((T-(l*u)),2)/norm(T,2)
12.7

n=10000;
b=rand(9999,1) ;
a=rand(10000,1) ;
c=rand(9999,1);
T=zeros(n);
for k=1:n
T(k,k)=a(k);
end
for k=1:n-1
T(k,k+1)=b(k);
T(k+1,k)=c(k);
end
tic
L=zeros(n); U=zeros(n);
for k=1:n-1
U(k,k:k+1)=T(k,k:k+1)-L(k,1:k-1)*U(1:k-1,k:k+1);
L(k+1,k)=(T(k+1,k)-L(k+1,1:k-1)*U(1:k-1,k))/U(k,k);
L(n,n-1)=(T(n,n-1)-L(n,1:n-2)*U(1:n-2,n-1))/U(n-1,n-1);
U(n,n)=T(n,n)-L(n,1:n-1)*U(1:n-1,n:n);
L(n+1:n,n)=(T(n+1:n,n)-L(n+1:n,1:n-1)*U(1:n-1,n))/U(n,n);
end
L=L+eye(n);
toc
C=fix(L*U*10000)==fix(T*10000);
prod(prod(C))
tic
[l u]=lu(T);
F=l*u;
toc
for k=3:n
l(1:k-2,k)=0;
l(k,1:k-2)=0;
u(1:k-2,k)=0;
u(k,1:k-2)=0;
F(1:k-2,k)=0;
F(k,1:k-2)=0;
end
D=fix(F*10000)==fix(T*10000);
prod(prod(D))
12.8

The times are close, but with respect to the previous paragraph increase about 100
times approximately, this is because I increase the order of the matrix
12.9
D = gallery('dorr',100);
n=100;
tic
L=zeros(n); U=zeros(n);
for k=1:n-1
U(k,k:k+1)=D(k,k:k+1)-L(k,1:k-1)*U(1:k-1,k:k+1);
L(k+1,k)=(D(k+1,k)-L(k+1,1:k-1)*U(1:k-1,k))/U(k,k);
L(n,n-1)=(D(n,n-1)-L(n,1:n-2)*U(1:n-2,n-1))/U(n-1,n-1);
U(n,n)=D(n,n)-L(n,1:n-1)*U(1:n-1,n:n);
L(n+1:n,n)=(D(n+1:n,n)-L(n+1:n,1:n-1)*U(1:n-1,n))/U(n,n);
end
L=L+eye(n);
toc
C=fix(L*U*10000)==fix(D*10000);
prod(prod(C))
tic
[l u]=lu(D);
F=l*u;
toc
for k=3:n
l(1:k-2,k)=0;
l(k,1:k-2)=0;
u(1:k-2,k)=0;
u(k,1:k-2)=0;
F(1:k-2,k)=0;
F(k,1:k-2)=0;
end
G=fix(F*10000)==fix(D*10000);
prod(prod(G))

E1=norm((D-(L*U)),2)/norm(full(D),2)
E2=norm(full(D-(l*u)),2)/norm(full(D),2)
the LU factorization is more accurate by the first code, very close

You might also like