Numerical Assignment 2

  • Upload
    naz-az

  • View
    217

  • Download
    0

Embed Size (px)

Citation preview

  • 7/30/2019 Numerical Assignment 2

    1/25

    Numerical Methods

    Professor Dennis Giannacopoulos

    Assignment 2

    Nassir Abou Ziki

    Monday Nov 12 2012-10-15

  • 7/30/2019 Numerical Assignment 2

    2/25

    Problem 1In 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.

    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 Uis linear overxandy 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 ofaas

    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.

  • 7/30/2019 Numerical Assignment 2

    3/25

    The determinant in the denominator is just twice the area of the triangle denoted2A, then

    Similarly we get an expression for band c:

    Now we can write a, b and c in terms ofU1, U2, and U3 by substituting the previous expressions intothe

    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 unknownsU1, 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:

  • 7/30/2019 Numerical Assignment 2

    4/25

    Such that,

    ( ) Using equation 1.13 and equations 1.7-1.9 we can find the expressions of for i= 1,2,3 and j=1,2,3

    Not that 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:

  • 7/30/2019 Numerical Assignment 2

    5/25

    [

    ]

    The global S-matrix is given by:

    Where the matrix Cis:

    [

    ]

    Thus the global S-matrix is:

    These calculations were done by hand.

  • 7/30/2019 Numerical Assignment 2

    6/25

    Problem 2In 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.

    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 2 A sketch of the coaxial cable with the

    boundary conditions

  • 7/30/2019 Numerical Assignment 2

    7/25

    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 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

    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

  • 7/30/2019 Numerical Assignment 2

    8/25

    Problem 3In 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 finite-difference 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.

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

    A is 19 by 19 coefficient matrix, the column vectorx represents the unknown potentials at the 19 nodesand 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 theAmatrix by hand

    and entered it to Matlab. The data corresponding to this problem is saved in the file data.mat.

    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.

  • 7/30/2019 Numerical Assignment 2

    9/25

    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 thefollowing 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 positivedefinite. To achieve this purpose we left multiply the equation by

    to get:

    Hence now we have a modified system:

    , where and Amodand bmodare 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.24112.5890

    4.1194

    5.7050

    6.0612

    1.7373

    3.6842

  • 7/30/2019 Numerical Assignment 2

    10/25

    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 =

  • 7/30/2019 Numerical Assignment 2

    11/25

    50.4665 29.6248

    31.1892 15.0240

    21.5185 9.4059

    17.0146 8.1961

    14.4802 6.1396

    10.9324 5.8752

    10.0132 7.5790

    11.9813 5.3248

    10.3042 6.1069

    8.4821 4.5521

    7.2796 2.5955

    6.3425 2.9610

    3.0684 1.3835

    1.8087 0.8348

    2.0684 1.0711

    1.6836 0.7185

    0.5139 0.2248

    0.0140 0.0060

    0.0000 0.0000

    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

    0

    10

    20

    30

    40

    50

    60

    0 5 10 15 20 25

    norm

    Number of Iterations

    The variation of the 2-norm and infinite norm

    as a function of the number of iterations

    2-norm

    infinitenorm

    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

  • 7/30/2019 Numerical Assignment 2

    12/25

  • 7/30/2019 Numerical Assignment 2

    13/25

    Appendix

    App en dix 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

  • 7/30/2019 Numerical Assignment 2

    14/25

    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.

  • 7/30/2019 Numerical Assignment 2

    15/25

    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.

  • 7/30/2019 Numerical Assignment 2

    16/25

    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

    2 0.000

    3 0.000

    4 0.000

    5 0.000

    6 0.000

    7 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

  • 7/30/2019 Numerical Assignment 2

    17/25

    App en dix 2b

    The output file:

    Input node list

    n x y

    1 0.00000 0.00000

    2 0.02000 0.00000

    3 0.04000 0.00000

    4 0.06000 0.00000

    5 0.08000 0.00000

    6 0.10000 0.00000

    7 0.00000 0.02000

    8 0.02000 0.02000

    9 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

  • 7/30/2019 Numerical Assignment 2

    18/25

    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 j k Source

    2 7 1 .00000E+00

    2 8 7 .00000E+00

    3 8 2 .00000E+00

    3 9 8 .00000E+00

    4 9 3 .00000E+00

    4 10 9 .00000E+00

  • 7/30/2019 Numerical Assignment 2

    19/25

  • 7/30/2019 Numerical Assignment 2

    20/25

    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

    1 0.00000

    2 0.00000

    3 0.00000

    4 0.00000

    5 0.00000

  • 7/30/2019 Numerical Assignment 2

    21/25

  • 7/30/2019 Numerical Assignment 2

    22/25

    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

  • 7/30/2019 Numerical Assignment 2

    23/25

    App en dix 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 An=size(A,1);

    %set iteration to zeroiter=0;

    %start with zero as an initial guessx=zeros(n,1);

    %find the residual vectorr=b-(mat_times(A,x));

    %set the initial direction vector to be equal to the first residualp=r;

    %loop until we are donewhile(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 byx=x+(alpha*p);

    %find the new residualr=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 normres(iter+1,1)=two_norm(r);res(iter+1,2)=infiniteNorm(r);

    %check if we are doneiter=iter+1;if iter>n || max(r)

  • 7/30/2019 Numerical Assignment 2

    24/25

    ------------------------------------------------------------------------------------------------------------

    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 symmetricif 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

    endend

    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));endy(i)=(b(i)-tmp)/L(i,i);

    end

    % Finding from y=L'x

  • 7/30/2019 Numerical Assignment 2

    25/25

    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)));endx(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));endm=sqrt(s);

    end--------------------------------------------------------------------------------------------------------------------------------------

    function m=infiniteNorm(V)%this functions computes the infinite norm of a column vector Vn=size(V,1);m=0;for i=1:n

    y=abs(V(i));if y > m

    m=y;end

    end

    end