You are on page 1of 25

Numerical Methods

Professor Dennis Giannacopoulos

Assignment 2
Nassir Abou Ziki
Monday Nov 12 2012-10-15

Problem 1
In this problem we consider two first-order triangular finite elements used to solve the Laplace equation
for electrostatic potential. We first find a local S-matrix for each triangle, and then the global S-matrix
for the mesh which consists of just these two triangles. Figure 1 shows the local (disjoint) and global
(conjoint) node-numberings respectively along with the (x, y)-coordinates of the element vertices in
meters.

Figure 1: This figure shows the position of the nodes in meters as well as the
numbering of the nodes for the disjoint (a) elements and the conjoint (b) elements.

First we will derive the equations that will allow us to calculate the local S-matrix for each triangle. Since
we are assuming the potential U is linear over x and y then we can write:

For a given triangle (assume vertices are number 1 through 3), then at vertex 1 we have:

We get similar expressions at vertices 2 and 3 and we can write:

][ ]

Using Cramers rule we can write an expression of a as

The determinant in the denominator is just twice the area of the triangle denoted 2A, then
[

Similarly we get an expression for b and c:


[

Now we can write a, b and c in terms of U1, U2, and U3 by substituting the previous expressions into the
following equation,
[

][ ]

And thus we can now write

Where we have,
[

Notice that equation (1.6) for the total potential is now given in terms of the three unknowns U1, U2,
and U3 only since we eliminated a, b and c by substitution.
Now that we have an expression of the potential of one triangle (e) we look at the contribution to the
energy from that triangle, which is given by,
|

This can be written in matrix form:

Such that,
[

Using equation 1.13 and equations 1.7-1.9 we can find the expressions of

[
[

Not that

for i= 1,2,3 and j=1,2,3

]
]

is in fact symmetric thus equations 1.14 through 1.19 are sufficient to find

Now we can find the local matrices, let the lower triangle be triangle 1 and let the upper triangle be
triangle 2, then we have:
[

]
[

The global S-matrix is given by:

Where the matrix C is:

Thus the global S-matrix is:

These calculations were done by hand.

Problem 2
In this problem we use a program Simple2D to solve for the electrostatic potential for one-quarter of
the cross-section of a rectangular coaxial cable shown in figure 2. The Simple2D uses the finite element
method to solve for the static potential.

Figure 2 A sketch of the coaxial cable with the


boundary conditions

We used the two-element mesh shown in Figure 1(b) as a building block to construct a finite element
mesh for one-quarter of the cross section of the coaxial cable, the mesh is shown in Figure 3. We then
used that mesh to write the input file for Simple2D. The first part of the input file defines the position of
the nodes. Then the elements are entered in the input file where they were defined by going through
the nodes in a counter-clockwise fashion. Finally, the last part of the input file defines the Dirchelet
Boundary Conditions. The input file is named inputfile.txt it can be found in the assignment directory.
Also I inserted the input file in Appendix 2a for reference.

Figure 3 This figure shows the finite element mesh configuration with the node
numberings and the boundary conditions. Note that the width of the mesh is 0.1
meters and the length is 0.1 meters. Hence we are looking at the lower left
quarter of the initial problem

Once we wrote the input file, I created a directory and placed in it the input file, an empty output file
labeled outputfile.txt along with the Simple2D executable file. Then I ran Simple2D using the following
command in command prompt:
C:\path to directory\ simple2D <inputfile.txt > outputfile.txt
The program solves for the potential at the nodes of the mesh and saves the result into the output file,
which can also be found in the assignment folder as well as in Appendix 2b. To determine the potential
at (x,y) = (0.06, 0.04) we just need to find the node associated with these coordinates in the output file.
Notice from Figure 3 that the point (x,y) = (0.06, 0.04), is in fact node number 16. Therefore the
potential at (x,y) = (0.06, 0.04) is given by:
V=3.68423 volts

Problem 3
In this problem we are going to solve the same problem as described in question number 2. However,
we will use different methods to get the solution. We will be using the Conjugate Gradient method as
well as Cholesky decomposition. In order, to solve this problem we define a mesh using a finitedifference node spacing, h=0.02m in x and y directions for the same one-quarter cross-section of the
system shown in Figure 2. The mesh is shown in Figure 4.

Figure 4 This figure shows the finite element mesh with the boundary
conditions as well as the numbering of the nodes at which we need to solve
for the electric potential.

We used a five-point difference formula to write the problem in the form of:

A is 19 by 19 coefficient matrix, the column vector x represents the unknown potentials at the 19 nodes
and the column vector b is given by the problem. Note that most of the entries of the vector b are zero
(except at 9, 10, 13, 16 and 19 where these entries take the value of -10). I wrote the A matrix by hand
and entered it to Matlab. The data corresponding to this problem is saved in the file data.mat.

I wrote a program implementing the conjugate gradient method (un-preconditioned), the code is
included under the function conjugateGrad in Appendix 3. Also the code needed to solve the
problem using Cholesky Decomposition from Assignemnt 1 is included in Appendix 3 (functions
cholDecomposition, solveLinear and solve_helper). The matrix operation functions
are not included in the Appendix for simplicity also because these functions are trivial, they are found in
the assignment directory.

a. We test the matrix A using the Cholesky Decompostion program. We basically run in Matlab the
following command
cholDecomposition(A)
The output is a 19 by 19 lower triangular matrix with complex entries. Therefore, therefore A is not
positive definite. We would like to have a system in the form of
where A is symmetric positive
definite. To achieve this purpose we left multiply the equation by
to get:

Hence now we have a modified system:


, where

and

Amod and bmod are saved in the data.mat file. The modified matrix is now guaranteed to be symmetric
and positive definite.
b. Solving using Cholesky matlab code and output:
>> load('data.mat')
>> solveLinear(Amod,bmod)
ans =
0.6381
1.3111
2.0175
2.6394
2.8350
1.2411
2.5890
4.1194
5.7050
6.0612
1.7373
3.6842

6.1661
2.0240
4.2445
6.8608
2.1143
4.4090
7.0327
Solving using conjugate gradient method, matlab code and output:
>> [x,iter,res]=conjugateGrad(Amod,bmod,0)
x=
0.6381
1.3111
2.0175
2.6394
2.8350
1.2411
2.5890
4.1194
5.7050
6.0612
1.7373
3.6842
6.1661
2.0240
4.2445
6.8608
2.1143
4.4090
7.0327

iter =
20

res =

50.4665
31.1892
21.5185
17.0146
14.4802
10.9324
10.0132
11.9813
10.3042
8.4821
7.2796
6.3425
3.0684
1.8087
2.0684
1.6836
0.5139
0.0140
0.0000
0.0000

29.6248
15.0240
9.4059
8.1961
6.1396
5.8752
7.5790
5.3248
6.1069
4.5521
2.5955
2.9610
1.3835
0.8348
1.0711
0.7185
0.2248
0.0060
0.0000
0.0000

Note that the solution from the Cholesky Decomposition method is almost identical to the solution
given by the conjugate gradient method (vector x in the output).
c. The plots of the norms

The variation of the 2-norm and infinite norm


as a function of the number of iterations
60
50

norm

40
30

2-norm

20

infinite
norm

10
0
0

10

15

20

25

Number of Iterations
Plot 1 This plot shows the variation of the 2-norm and the infinite nor as a function of the number of
iterations of the conjugate gradient method. Note that both go to zero and thus we have convergence

The variation of the 2-norm and infinite norm as


a function of the number of iterations (log-scale)
100

10

norm

1
2-norm
0.1

infinite
norm

0.01

0.001
0

10

15

20

25

Number of Iterations
Plot 2 This plot shows the variation of the 2-norm and the infinite nor as a function of the
number of iterations of the conjugate gradient method on a log scale.

d. To determine the potential at (x,y) = (0.06, 0.04) we just need to find the entry associated with
these coordinates in the solution vector. Notice from Figure 4 that the point (x,y) = (0.06, 0.04),
is in fact node number 12. Therefore the potential at (x,y) = (0.06, 0.04) is given by the 12th entry
in the solution vector for both the Cholesky method and the Conjugate gradient method. Note
that both vectors are included in part b of this question. The table below shows the values of the
potential at (x,y) = (0.06, 0.04) for the Cholesky method and the Conjugate gradient method as
well as the solution we got in number 2 using the finite element method:
Conjugate Gradient
3.6842 volts

Cholesky Decomposition
3.6842 volts

Finite Element Method


3.68423 volts

SOR Method
(Assignment 1)
3.6842 volts

Appendix
Appendix 2a
The input file:
0.000

0.000

0.020

0.000

0.040

0.000

0.060

0.000

0.080

0.000

0.100

0.000

0.000

0.020

0.020

0.020

0.040

0.020

0.060

0.020

0.080

0.020

0.100

0.020

0.000

0.040

0.020

0.040

0.040

0.040

0.060

0.040

0.080

0.040

0.100

0.040

0.000

0.060

0.020

0.060

0.040

0.060

0.060

0.060

0.080

0.060

0.100

0.060

0.000

0.080

0.020

0.080

0.040

0.080

0.060

0.080

0.080

0.080

0.100

0.080

0.000

0.100

0.020

0.100

0.040

0.100

0.060

0.100

2 7 1

0.

2 8 7

0.

3 8 2

0.

3 9 8

0.

4 9 3 0.
4 10 9 0.
5 10 4

0.

5 11 10

0.

6 11 5

0.

6 12 11

0.

8 13 7

0.

8 14 13

0.

9 14 8

0.

9 15 14

0.

10 15 9

0.

10 16 15

0.

11 16 10

0.

11 17 16

0.

12 17 11

0.

12 18 17

0.

14 19 13

0.

14 20 19

0.

15 20 14

0.

15 21 20

0.

16 21 15

0.

16 22 21

0.

17 22 16

0.

17 23 22

0.

18 23 17

0.

18 24 23

0.

20 25 19

0.

20 26 25

0.

21 26 20

0.

21 27 26

0.

22 27 21

0.

22 28 27

0.

23 28 22

0.

23 29 28

0.

24 29 23

0.

24 30 29

0.

26 31 25

0.

26 32 31

0.

27 32 26

0.

27 33 32

0.

28 33 27

0.

28 34 33

0.

/
1

0.000

0.000

0.000

0.000

0.000

0.000

0.000

13 0.000
19 0.000
25 0.000
31 0.000
28 10.00
29 10.00
30 10.00
34 10.00

Appendix 2b
The output file:
Input node list
n

0.00000

0.00000

0.02000

0.00000

0.04000

0.00000

0.06000

0.00000

0.08000

0.00000

0.10000

0.00000

0.00000

0.02000

0.02000

0.02000

0.04000

0.02000

10

0.06000

0.02000

11

0.08000

0.02000

12

0.10000

0.02000

13

0.00000

0.04000

14

0.02000

0.04000

15

0.04000

0.04000

16

0.06000

0.04000

17

0.08000

0.04000

18

0.10000

0.04000

19

0.00000

0.06000

20

0.02000

0.06000

21

0.04000

0.06000

22

0.06000

0.06000

23

0.08000

0.06000

24

0.10000

0.06000

25

0.00000

0.08000

26

0.02000

0.08000

27

0.04000

0.08000

28

0.06000

0.08000

29

0.08000

0.08000

30

0.10000

0.08000

31

0.00000

0.10000

32

0.02000

0.10000

33

0.04000

0.10000

34

0.06000

0.10000

Input element list


i

Source

1 .00000E+00

7 .00000E+00

2 .00000E+00

8 .00000E+00

3 .00000E+00

4 10

9 .00000E+00

5 10

4 .00000E+00

5 11 10 .00000E+00
6 11

5 .00000E+00

6 12 11 .00000E+00
8 13

7 .00000E+00

8 14 13 .00000E+00
9 14

8 .00000E+00

9 15 14 .00000E+00
10 15

9 .00000E+00

10 16 15 .00000E+00
11 16 10 .00000E+00
11 17 16 .00000E+00
12 17 11 .00000E+00
12 18 17 .00000E+00
14 19 13 .00000E+00
14 20 19 .00000E+00
15 20 14 .00000E+00
15 21 20 .00000E+00
16 21 15 .00000E+00
16 22 21 .00000E+00
17 22 16 .00000E+00
17 23 22 .00000E+00
18 23 17 .00000E+00
18 24 23 .00000E+00
20 25 19 .00000E+00

20 26 25 .00000E+00
21 26 20 .00000E+00
21 27 26 .00000E+00
22 27 21 .00000E+00
22 28 27 .00000E+00
23 28 22 .00000E+00
23 29 28 .00000E+00
24 29 23 .00000E+00
24 30 29 .00000E+00
26 31 25 .00000E+00
26 32 31 .00000E+00
27 32 26 .00000E+00
27 33 32 .00000E+00
28 33 27 .00000E+00
28 34 33 .00000E+00

Input fixed potentials


node

value

0.00000

0.00000

0.00000

0.00000

0.00000

0.00000

0.00000

13

0.00000

19

0.00000

25

0.00000

31

0.00000

28

10.00000

29

10.00000

30

10.00000

34

10.00000

Error in routine MESHIN, error number

Final solution
i

potential

0.00000 0.00000

0.00000

0.02000 0.00000

0.00000

0.04000 0.00000

0.00000

0.06000 0.00000

0.00000

0.08000 0.00000

0.00000

0.10000 0.00000

0.00000

0.00000 0.02000

0.00000

0.02000 0.02000

0.63805

0.04000 0.02000

1.24108

10

0.06000 0.02000

1.73734

11

0.08000 0.02000

2.02403

12

0.10000 0.02000

2.11426

13

0.00000 0.04000

0.00000

14

0.02000 0.04000

1.31112

15

0.04000 0.04000

2.58895

16

0.06000 0.04000

3.68423

17

0.08000 0.04000

4.24452

18

0.10000 0.04000

4.40899

19

0.00000 0.06000

0.00000

20

0.02000 0.06000

2.01747

21

0.04000 0.06000

4.11938

22

0.06000 0.06000

6.16611

23

0.08000 0.06000

6.86082

24

0.10000 0.06000

7.03266

25

0.00000 0.08000

0.00000

26

0.02000 0.08000

2.63936

27

0.04000 0.08000

5.70500

28

0.06000 0.08000

10.00000

29

0.08000 0.08000

10.00000

30

0.10000 0.08000

10.00000

31

0.00000 0.10000

0.00000

32

0.02000 0.10000

2.83499

33

0.04000 0.10000

6.06125

34

0.06000 0.10000

10.00000

Appendix 3
% This function is an implentation of the non pre-conditioned conjugate
% gradient method to solve a system of equations Ax= b.
function [x,iter,res]=conjugateGrad(A,b,es)
%get the size of the square matrix A
n=size(A,1);
%set iteration to zero
iter=0;
%start with zero as an initial guess
x=zeros(n,1);
%find the residual vector
r=b-(mat_times(A,x));
%set the initial direction vector to be equal to the first residual
p=r;
%loop until we are done
while(1)
%find alpha using p, r and A
alpha=mat_times(mat_transpose(p),r)/mat_times(mat_times(mat_transpose(p),A),p
);
%the new minimum point is given by
x=x+(alpha*p);
%find the new residual
r=b-mat_times(A,x);
%set column one of res to be the 2-norm at each iter and column two to
%be the infinite norm
res(iter+1,1)=two_norm(r);
res(iter+1,2)=infiniteNorm(r);
%check if we are done
iter=iter+1;
if iter>n || max(r) <es
break;
end
%find the new search direction according to conjugate gradient method
beta=mat_times(mat_transpose(p),mat_times(A,r))/mat_times(mat_transpose(p),mat_tim
es(A,p)); %beta is a scalar
p=r+beta*p;
end
end

-----------------------------------------------------------------------------------------------------------function L=cholDecomposition(X)
%Runs the Cholesky Decomposition Algorithm on Matrix X and saves the lower
% matrix in L.
% Check that the matrix is a square matrix
[m,n]=size(X);
if m~=n
error ('The matrix is not a square matrix!!');
end
%Check if the matrix is symmetric
if X ~= mat_transpose(X)
error ('The matrix is not symmetric!!');
end

% The cholesky algortithm to find L


L= zeros(n,n);
for j=1:n
L(j,j)=+sqrt(X(j,j));
for i = j+1:n
L(i,j)=X(i,j)/L(j,j);
for k = j+1:i
X(i,k)=X(i,k)-(L(i,j)*L(k,j));
end
end
end

end

-----------------------------------------------------------------------------------------------------------------------------------function x=solve_helper(L,b)
%This function solves the system Ax=b by taking as an argument the matrix L
%which is the lower matrix resulting from the cholesky decomposition of A
%and the second argument is the vector b. The function returns the vector x
L_trans=mat_transpose(L);
[m,n]=size(L);
y=zeros(n,1);
y(1)=b(1)/L(1,1);
for i=2:n
tmp=0;
for j=1:i-1
tmp=tmp+(L(i,j)*y(j));
end
y(i)=(b(i)-tmp)/L(i,i);
end
% Finding from y=L'x

x=zeros(n,1);
x(n)=y(n)/L_trans(n,n);
for i=n-1:-1:1
tmp=0;
for j=i+1:n
tmp=tmp+(L_trans(i,j)*(x(j)));
end
x(i)=(y(i)-tmp)/L_trans(i,i);
end
end

------------------------------------------------------------------------------------------------------------------------------------function x = solveLinear(A,b)
%this function takes a matrix and a vector and solves the system Ax=b using
%a cholesky deomposition
L=cholDecomposition(A);
x=solve_helper(L,b);
end

-------------------------------------------------------------------------------------------------------------------------------------function m= two_norm(V)
% This function takes a column vector V and returns the 2-norm of the
% vector which is just the euclidean norm.
n=size(V,1);
s=0;
for i=1:n
s=s+((V(i)^2));
end
m=sqrt(s);
end

-------------------------------------------------------------------------------------------------------------------------------------function m=infiniteNorm(V)
%this functions computes the infinite norm of a column vector V
n=size(V,1);
m=0;
for i=1:n
y=abs(V(i));
if y > m
m=y;
end
end
end

You might also like