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

PROJECT REPORT MATRIX COMPUTATION I

[Problem Set B - Solution]

Lecturer:
1. Prof. Edmond Chow
2. Prof. Tsung-Min Hwang
3. Prof. Matthew M. Lin
4. Prof. Weichung Wang

By:
1. Irfan Nurhidayat [60640035S]
2. Lee Chen Han [60640007S]
3. Shih Hsiang Ren [60640006S]

NATIONAL TAIWAN NORMAL UNIVERSITY


TAIPEI CITY
2017
Problem set B

[CONTENTS]

B1 p.2

B2 p.4

B3 p.13

B4 p.15

B5 p.16

B6 p.17

B7 p.18

B8 p.19

[OUTLINE]

In this paper, we answer all of problem set B [3]. We use a laptop ASUS with specification:
Windows 7, Processor: Intel (R) Celeron (R) CPU1007U@1.59GHz 1.50GHz, system
type 32-bit Operating system, and MATLAB.

Irfan Nurhidayat, Lee Chen Han, and Shih Hsiang Ren (NTNU Students) Page 1
[B1] 【Back to top】

Syntax Figure
g = numgrid('S',8);
a = delsq(g);
spy(a);
a;
p = symrcm(a);
a2 = a(p, p);
r = chol(a);
r2 = chol(a2);
nnz(r);
title('symrcm, shape S with n=8');

g = numgrid('S',16);
a = delsq(g);
spy(a);
a;
p = symamd(a);
a2 = a(p, p);
r = chol(a);
r2 = chol(a2);
nnz(r);
title('symamd, shape S with n=16');

Irfan Nurhidayat, Lee Chen Han, and Shih Hsiang Ren (NTNU Students) Page 2
g = numgrid('N',8);
a = delsq(g);
spy(a);
a;
p = symrcm(a);
a2 = a(p, p);
r = chol(a);
r2 = chol(a2);
nnz(r);
title('symrcm, shape N with n=8');

g = numgrid('N',16);
a = delsq(g);
spy(a);
a;
p = symamd (a);
a2 = a(p, p);
r = chol(a);
r2 = chol(a2);
nnz(r);
title('symamd, shape N with n=16');

B = bucky;
p = symamd(B);
R = (p,p);
subplot(1,2,1), spy(B), title('B');
subplot(1,2,2), spy(R), title('B(p,p)')

B = bucky;
p = symrcm(B);
R = B(p,p);
subplot(1,2,1),spy(B),title('B');
subplot(1,2,2),spy(R),title('B(p,p)')

Irfan Nurhidayat, Lee Chen Han, and Shih Hsiang Ren (NTNU Students) Page 3
[B2]
>> load bcsstm22.mat
>> whos
Name Size Bytes Class Attributes

Problem 1x1 3456 struct


a 3562x3562 1933184 double sparse
a2 3562x3562 1933172 double sparse
ans 1x1 8 double
p 1x3562 28496 double
q 1x3562 28496 double
r 3562x3562 5723648 double sparse
r2 3562x3562 7408616 double sparse

>> Problem

Problem =

title: 'SYMMETRIC MASS MATRIX - TEX...'


A: [138x138 double]
name: 'HB/bcsstm22'
id: 72
date: '1984'
author: 'J. Lewis'
ed: 'I. Duff, R. Grimes, J. Lewis'
kind: 'structural problem'

>> a=Problem.A;
>> p = symrcm(a);
a2 = a(p, p);
r = chol(a);
r2 = chol(a2);
nnz(r)

ans =

138

>> spy(r)

Irfan Nurhidayat, Lee Chen Han, and Shih Hsiang Ren (NTNU Students) Page 4
>> spy(r2)

>> p = symrcm(a);
q = p(end : -1: 1);
r = chol(a(p, p)); nnz(r)
r2 = chol(a(q, q)); nnz(r2)

ans =

138

ans =

Irfan Nurhidayat, Lee Chen Han, and Shih Hsiang Ren (NTNU Students) Page 5
138

>> spy(r2)

>> spy(r)

>> load bcsstk25.mat


>> whos
Name Size Bytes Class Attributes

Problem 1x1 3089898 struct


a 138x138 2224 double sparse
Irfan Nurhidayat, Lee Chen Han, and Shih Hsiang Ren (NTNU Students) Page 6
a2 138x138 2212 double sparse
ans 1x1 8 double
p 1x138 1104 double
q 1x138 1104 double
r 138x138 2212 double sparse
r2 138x138 2212 double sparse

>> Problem

Problem =

title: 'SYMMETRIC STIFFNESS MATRIX - 76 ST...'


A: [15439x15439 double]
name: 'HB/bcsstk25'
id: 47
date: '1984'
author: 'J. Lewis'
ed: 'I. Duff, R. Grimes, J. Lewis'
kind: 'structural problem'

>> a=Problem.A;
>> p = symrcm(a);
a2 = a(p, p);
r = chol(a);
r2 = chol(a2);
nnz(r)

ans =

2940220

>> spy(r)

Irfan Nurhidayat, Lee Chen Han, and Shih Hsiang Ren (NTNU Students) Page 7
>> spy(r2)

>> p = symrcm(a);
q = p(end : -1: 1);
r = chol(a(p, p)); nnz(r)
r2 = chol(a(q, q)); nnz(r2)

ans =
2518867

Irfan Nurhidayat, Lee Chen Han, and Shih Hsiang Ren (NTNU Students) Page 8
ans =

2979293

>> spy(r2)

>> spy(r)

>> load bcsstk16.mat


>> whos
Name Size Bytes Class Attributes

Problem 1x1 3505312 struct


a 15439x15439 3088664 double sparse
a2 15439x15439 3088652 double sparse
Irfan Nurhidayat, Lee Chen Han, and Shih Hsiang Ren (NTNU Students) Page 9
ans 1x1 8 double
p 1x15439 123512 double
q 1x15439 123512 double
r 15439x15439 30288164 double sparse
r2 15439x15439 35813276 double sparse

>> Problem

Problem =

title: 'S STIFFNESS MATRIX - CORP. OF ENG...'


A: [4884x4884 double]
name: 'HB/bcsstk16'
id: 38
date: '1985'
author: 'M. Will'
ed: 'I. Duff, R. Grimes, J. Lewis'
kind: 'structural problem'

>> a=Problem.A;
>> p = symrcm(a);
a2 = a(p, p);
r = chol(a);
r2 = chol(a2);
nnz(r)

ans =

610800

>> spy(r)

Irfan Nurhidayat, Lee Chen Han, and Shih Hsiang Ren (NTNU Students) Page 10
>> spy(r2)

>> p = symrcm(a);
q = p(end : -1: 1);
r = chol(a(p, p)); nnz(r)
r2 = chol(a(q, q)); nnz(r2)

ans =

627487

ans =

673335

>> spy(r2)

Irfan Nurhidayat, Lee Chen Han, and Shih Hsiang Ren (NTNU Students) Page 11
>> spy(r)

【Back to top】

Irfan Nurhidayat, Lee Chen Han, and Shih Hsiang Ren (NTNU Students) Page 12
[B3]

a) Here
The given Laplacian 4x4 grid in natural ordering using 5-point stencil
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
0 * * *
1 * * * *
2 * * * *
3 * * *
4 * * * *
5 * * * * *
6 * * * * *
7 * * * *
8 * * * *
9 * * * * *
10 * * * * *
11 * * * *
12 * * *
13 * * * *
14 * * * *
15 * * *

Array of values (a)


1st pass 2nd Pass 3rd pass 4th pass
[* * * * * * * * * * * * * *][* * * * * * * * * * * * *][* * * * * * * * * * * * * * *][* * * * * * * * *
*][* * * *
5th pass
* * * * * * * *]

Col Index (j): corresponding array of column indices


[0 0 0 1 2 3 4 5 6 7 8 9 10 11][1 1 1 2 4 5 6 8 9 10 12 13 14][2 2 3 4 5 6 7 8 9 10 11 12 13 14 15][3
5 6 7 9 10 11 13 14 15][4 5 6 7 8 9 10 11 12 13 14 15]
P: (array) mapping between original row number and sorted row number = [0 14 27 42 52]
N: number of rows in matrix = 16
M: number of columns = 5
Len: array length of each column = [14, 13, 15, 10, 12]

b) Write a code or a pseudocode for multiplying a sparse matrix in JD format by a dense vector,
i.e. y=Ax.
The following code is a matrix-vector multiplication code in JD format
for (int j = 0; j < rho_max; j++) //fetch the upper index
{
upper = jdptr[j+1];

for (int i = jdptr[j]; i < upper; i++)


{
col = colind[i];

Irfan Nurhidayat, Lee Chen Han, and Shih Hsiang Ren (NTNU Students) Page 13
b[1] = b[1]+value[1]* v[col];
}
}

Note: There is a need for an-m matrix which functions as the receptacle of the original row
order.
This is another possible answer

【Back to top】

Irfan Nurhidayat, Lee Chen Han, and Shih Hsiang Ren (NTNU Students) Page 14
[B4]

For each i > 1 there exists 𝑥𝑖 is connect to 𝑥𝑖−1 .


Thus to solve 𝑥𝑖 for i > 1, we need to solve 𝑥𝑖−1 first.
For example, we need to solve 𝑥2 before 𝑥3 , solve 𝑥3 before 𝑥4 ……
That means we can only solve one node each time.
Therefore, there are 25 levels for this grid and if node 1 is in the level 1 then node 9 is in
the level 9.

【Back to top】

Irfan Nurhidayat, Lee Chen Han, and Shih Hsiang Ren (NTNU Students) Page 15
[B5]

Question Graph
Can we combine 1 and
2? Yes 8 6
Can we add 3? Yes
Can we add 4? Yes
Can we add 5? Yes 2 3
Can we add 6? Yes
Can we add 7? No

1 10 4 7

5 9

Can we combine 7 and


8? Yes 10 9
Can we add 9? Yes
Can we add 10? Yes
Can we add 11? Yes
Can we add 12? Yes
7 11

8 12

So we have two partitions : (L1, L2, L3, L4, L5, L6) and (L7, L8, L9, L10, L11, L12)
So the results that the number of this partition (two) is less than the original number of
partition (five)

【Back to top】

Irfan Nurhidayat, Lee Chen Han, and Shih Hsiang Ren (NTNU Students) Page 16
[B6]
A=zeros(7,7);
for i=1:7
A(i,i)=2;
for j=1:7
if j==i+1 || j==i-1
A(i,j)=-1;
end
end
end
b1=zeros(7,1)
b2=zeros(7,1)
b3=zeros(7,1)
b4=zeros(7,1)
b5=zeros(7,1)
b6=zeros(7,1)
b7=zeros(7,1)

b1(1,1)=1
b2(2,1)=1
b3(3,1)=1
b4(4,1)=1
b5(5,1)=1
b6(6,1)=1
b7(7,1)=1

m1=(A'*A)^(-1)*(A'*b1)
m2=(A'*A)^(-1)*(A'*b2)
m3=(A'*A)^(-1)*(A'*b3)
m4=(A'*A)^(-1)*(A'*b4)
m5=(A'*A)^(-1)*(A'*b5)
m6=(A'*A)^(-1)*(A'*b6)
m7=(A'*A)^(-1)*(A'*b7)
M=zeros(7,7)
M(:,1)=m1
M(:,2)=m2
M(:,3)=m3
M(:,4)=m4
M(:,5)=m5
M(:,6)=m6
M(:,7)=m7
0.8750 0.7500 0.6250 0.5000 0.3750 0.2500 0.1250
0.7500 1.5000 1.2500 1.0000 0.7500 0.5000 0.2500
0.6250 1.2500 1.8750 1.5000 1.1250 0.7500 0.3750
A−1 = M = 0.5000 1.0000 1.5000 2.0000 1.5000 1.0000 0.5000
0.3750 0.7500 1.1250 1.5000 1.8750 1.2500 0.6250
0.2500 0.5000 0.7500 1.0000 1.2500 1.5000 0.7500
(0.1250 0.2500 0.3750 0.5000 0.6250 0.7500 0.8750)

【Back to top】
Irfan Nurhidayat, Lee Chen Han, and Shih Hsiang Ren (NTNU Students) Page 17
[B7]

U n 1  ( S (2) ) 1 S (1)  ( S (2) ) 1 g 


g1  (( A(1) ( A(1) ) 1 f )  f )
g 2  (( A(2) ( A(2) ) 1 f )  f )
g   g1  g 2

>> 1=[2 -1 0; -1 2 -1; 0 -1 1];


>> A2=[1 -1 0; -1 2 -1; 0 -1 2];
>> f=[1 ; 1 ; 1];
>> u0=[0 ; 0 ; 0];
>> S1=schur(A1);
>> S2=schur(A2);
>> g1=-((A1*inv(A1)*f)-f);
>> g2=-((A2*inv(A2)*f)-f);
>> g=g1+g2;
>> U=(inv(S2)*S1*u0)+(inv(S2)*g)

U=

1.0e-15 *

0
0.6426
0

【Back to top】

Irfan Nurhidayat, Lee Chen Han, and Shih Hsiang Ren (NTNU Students) Page 18
[B8]
function schwarz
% Schwarz examples

A = laplacian1(8);
a = [
2 -1 0 0
-1 2 -1 0
0 -1 2 -1
0 0 -1 2
];

b = [
2 -1 0 0 0
-1 2 -1 0 0
0 -1 2 -1 0
0 0 -1 2 -1
0 0 0 -1 2
];
f=rand(8,1); % rhs

%%%%%%%%%%%%%%%%%%%%
% first implementation of multiplicative Schwarz
fprintf('\n first implementation of multiplicative schwarz\n');
u = zeros(8,1); % initial guess

for i=1:10
f1 = [f(1) f(2) f(3) f(4)+u(5)]';
u1 = a\f1;
u(1) = u1(1);
u(2) = u1(2);
u(3) = u1(3);
u(4) = u1(4);

f2 = [f(4)+u(3) f(5) f(6) f(7) f(8)]';


u2 = b\f2;
u(4) = u2(1);
u(5) = u2(2);
u(6) = u2(3);
u(7) = u2(4);
u(8) = u2(5);
fprintf('%2d %e\n', i, norm(f-A*u));
end

%%%%%%%%%%%%%%%%%%%%
% second implementation of multiplicative Schwarz
fprintf('\n second implementation of multiplicative schwarz\n');
u = zeros(8,1); % initial guess
a1 = zeros(8,8); a1(1:4,1:4) = inv(a);
a2 = zeros(8,8); a2(4:8,4:8) = inv(b);
for i=1:10
r = f-A*u;
u = u + a1*r;
r = f-A*u;
u = u + a2*r;
fprintf('%2d %e\n', i, norm(f-A*u));
end

Irfan Nurhidayat, Lee Chen Han, and Shih Hsiang Ren (NTNU Students) Page 19
%%%%%%%%%%%%%%%%%%%%
% additive schwarz
fprintf('\n additive schwarz\n');
u = zeros(8,1); % initial guess
a1 = zeros(8,8); a1(1:4,1:4) = inv(a);
a2 = zeros(8,8); a2(4:8,4:8) = inv(b);
for i=1:10
r = f-A*u;
u = u + (a1+a2)*r;
fprintf('%2d %e\n', i, norm(f-A*u));
end

%%%%%%%%%%%%%%%%%%%%
% restricted additive schwarz
fprintf('\n restricted additive schwarz\n');
u1 = zeros(8,1); % local solution on 1, use only 1:4
u2 = zeros(8,1); % local solution on 2, use only 4:8
u = zeros(8,1); % global solution constructed from u1 and u2

for i=1:10
u1old = u1;

% solve local problem on 1 with boundary value from 2


f1 = [f(1) f(2) f(3) f(4)+u2(5)]';
u1(1:4) = a\f1;

% solve local problem on 2 with boundary value from 1


f2 = [f(4)+u1old(3) f(5) f(6) f(7) f(8)]';
u2(4:8) = b\f2;

% compute global solution


u = u1 + u2;
u(4) = u(4)/2;

fprintf('%2d %e\n', i, norm(f-A*u));


end

function a = laplacian1(m)
% a = laplacian1(m)
% returns tridiagonal matrix of order m, with entries [-1,2,-1] in each
row
e = ones(m,1);
a = spdiags([-e 2*e -e], [-1,0,1], m, m);

【Back to top】

Irfan Nurhidayat, Lee Chen Han, and Shih Hsiang Ren (NTNU Students) Page 20
REFERENCES

[1] Burden, R.L., and Faires, J.D., (2011). Numerical Analysis. 9ed. Cengage Learning.
[2] -------------------------------------, (2016). Numerical Analysis. 10ed. Cengage
Learning.
[3] https://drive.google.com/open?id=0B-K3Obw7S6dbMEZ5dGN0UHZxbVU

Irfan Nurhidayat, Lee Chen Han, and Shih Hsiang Ren (NTNU Students) Page 21

You might also like