Computational method for engineers

Embed Size (px)

Citation preview

  • 8/8/2019 Computational method for engineers

    1/21

    1Statement of the problem

    1. Statement of the problem

    Figure 1 shows a laminar incompressible flow in a four-element and three-node network of circular

    pipes. If a quantity Q m3/s of fluid enters and leaves the pipe network, it is necessary to compute the

    fluid nodal pressures and the volume flow rate in each pipe.

    Figure 1: Fluid flow network.

    2. Mathematical modelling

    The fluid resistance for an element is given as

    (1)Where L is the length of the pipe section;D, the diameter of the pipe section and, the dynamic

    viscosity of the fluid and the subscript k, indicates the element number. The mass flux rate entering

    and leaving an element can be written as

    (2)And

    (3)Where pis the pressure, q is the mass flux rate and the subscripts i andj indicate the two nodes of an

    element.

    (a) Using the equations given above, show that the characteristics of the element can be written as and

    based on Eq. 4, construct the characteristics for each of the four elements in Figure 1.

    (4) (5) (6)

  • 8/8/2019 Computational method for engineers

    2/21

    2Solution to the problem

    Now if we want to make a matrix of coefficients, unknowns and constants (we assume qi and qJ to be

    constant) if would be like following equation

    As you can see this is the same equation as equation number (4).in this equation is the pressure atthe starting point of the element, is the pressure at the end point of the element, is the enteringmass flux rate of each element and

    is the leaving mass flux rate of each element.

    For element number 1 we have

    (7)For element number 2 we have

    (8)For element number 3 we have

    (9)

    For element number 4 we have

    (10)These equations show that the mass flux rate entering and leaving an element are equals. And the

    mass flux rate of an element in the forward cycle is equal to the mass flux rate in the backward cycle

    of that element but off course in opposite direction.

    Table 1: Details of pipe network

  • 8/8/2019 Computational method for engineers

    3/21

    3S

    to the proble

    (b) From t

    element equations in Part a, write t

    e nodal equations and t

    ence construct t

    e matri

    form of a system oflinear equations fort

    e flow network.

    hat would you assume to sol e forp and q?

    3. Solu ion to the problem

    3.1analyti al solution1. (11 2. (12 3. (13)4. (14)5.

    (15)

    6. (16)As you can see here we have si equations but we have seven unknowns, it means that we should add

    an extra equation to these equations. If we do so then we will have seven equations and seven

    unknowns and we can solve these equations using a matrix. For doing so we assume thatthe pressure

    in nod numberthree is equalto 1atm.so the last equation is

    7. (17)

    For making these equations in the standard form we should calculate the fluid resistance for all the

    elements. So we rewrite equation (1) to calculate

    (1)Where

    is the diameter of element k; L is the length of pipe section and, the dynamic viscosity ofthe fluid equals to and subscript k, indicates the element number. We shouldcalculate R for all ofthe four elements, so we have

    (18) (19) (20) (21)

  • 8/8/2019 Computational method for engineers

    4/21

    4Solution to the proble

    So we write those 7 equations again but this time with these numerical findings, and we also have

    Q=0.1

    1. (22)2. (23)3. (24)4. (25)5. (26)6. (27)7.

    Now we can make a matrix of coefficients, known constants and unknowns.

    Afterthis step we use Matlab 7.0 to solve this equation and find allthe unknowns. You can see the

    process bellow

    A=

    b=

    and x=

  • 8/8/2019 Computational method for engineers

    5/21

    5Solution to the proble

    Solving this equation in Matlab 7.0, we find these amounts for unknowns.

    3. Numeri al solution

    (c) If water enters the network, shown in Figure 1, at a rate of ( ) with a viscosity of , write a FORTRAN program to iteratively determine the pressure values at all nodes.Write a MATLAB M-file to solve the same and compare the results.

    3. .1 Program code

    A: First solution

    If we want to determine the value of something iteratively one choice is to use Gauss-Siedel or

    Jacobian to do this. But for doing this we must at first write the equations in the standard form that

    can be used in these methods. You can see the standard form of writing the equations bellow. Theseequations arrive from equation 22 to 27. In this part because the main idea is to find the value of

    pressure in every node iteratively and we assume we only need two equations foruse in Gauss-Sidel or Jacobian. You can see these two equations bellow

    1.

    If we put the values of all the fluid resistance and the assuming value of we willhave the following equations

    (28)

  • 8/8/2019 Computational method for engineers

    6/21

    6Solution to the proble

    2. If we putthe values of allthe fluid resistance and the assuming value of we willhave the following equations

    ( (29)So we can rewrite our equations as follow

    MATLAB

    For using these equations in Gauss-Siedel method ofMATLAB we should write them in matrix formso we will have

    , We putthese matrixes in our program and solve the equations iteratively with Gauss-Siedel you can

    see the program and the result bellow. Firstitis an M-file to callthe GaussSiedel program.

    % A = known coe

    matrix

    % b = known constant vector

    % xg = initial (guessed) values of x

    % tol = tolerance

    % maxit = maximum number of iteration allowed

    A=[33.28951435 19.48335369;-20.45307739 39.9364310]

    b=[5347210.954 1974150.813]'

    xg = [00]'

    tol = 1e-8;

    maxit = 100;

    % call gauss-siedel

    X = GaussSiedel(A,b,xg,tol,maxit)

    And here is the Gauss-Siedel program

    function X=GaussSiedel(A,B,P,delta, max1)

    % Input - A is an N x N nonsingular matrix

    % - B is an N x 1 matrix

  • 8/8/2019 Computational method for engineers

    7/21

    7Solution to the problem

    % - P is an N x 1 matrix; the initial guess

    % - delta is the tolerance for P

    % - max1 is the maximum number of iterations

    % Output - X is an N x 1 matrix: the Gauss-Siedel approximation to

    % the solution of AX = B

    N = length(B);

    for k=1:max1

    for j=1:N

    if j==1

    X(1)=(B(1)-A(1,2:N)*P(2:N))/A(1,1);

    elseif j==N

    X(N)=(B(N)-A(N,1:N-1)*(X(1:N-1))')/A(N,N);

    else

    %X contains the kth approximations and P the (k-1)st

    X(j)=(B(j)-A(j,1:j-1)*X(1:j-1)'-A(j,j+1:N)*P(j+1:N))/A(j,j);

    endend

    err=abs(norm(X'-P));

    relerr=err/(norm(X)+eps);

    P=X';

    if (err

  • 8/8/2019 Computational method for engineers

    8/21

    8Solution to the problem

    xg =

    0

    0

    ------------------------------------------- -------------------iter no. p1 p2

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

    17 1.0132500246e+005 1.0132500147e+005

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

    Nima Fouladinejad***[email protected]

    X =

    1.0e+005 *

    1.01325002463959

    1.01325001473900

    FORTRAN

    For using equations 28 and 29 in Gauss-Siedel method ofFORTRAN we should write them in a

    different mode. You can see new equations bellow

    (30) (31)

    And here is the Gauss-Siedel program write in FORTRAN

    c

    c Gauss-Seidel method

    c

    c

    c file: GaussSiedel.f

    c

    dimension x(2)

    data x/0.,0./

    c

    print *

    print *,' Gauss -Seidel method 'print *,' Nima Fouladinejad [email protected] '

    print *

    c

    print *,'iteration p1 p2'

    print 3,0,x

    do 2 k=1,13

    x(1) = (5347210.954-19.48335369*x(2))/33.28951435

    x(2) = (1974150.813+20.45307739*x(1))/39.93643108

  • 8/8/2019 Computational method for engineers

    9/21

    9Solution to the problem

    print 3,k,x

    2 continue

    c

    3 format (3x,i2,2x,2(e23.15,2x))

    stop

    end

    And as you can see here are the results ofthis program

    Gauss-Seidel method

    Nima Fouladinejad [email protected]

    iteration p1 p2

    0 0.000000000000000E+00 0.000000000000000E+00

    1 0.160627500000000E+06 0.131696234375000E+06

    2 0.835496328125000E+05 0.922215078125000E+05

    3 0.106653007812500E+06 0.104053687500000E+06

    4 0.997279843750000E+05 0.100507101562500E+06

    5 0.101803695312500E+06 0.101570156250000E+06

    6 0.101181523437500E+06 0.101251515625000E+06

    7 0.101368015625000E+06 0.101347031250000E+06

    8 0.101312109375000E+06 0.101318398437500E+06

    9 0.101328867187500E+06 0.101326976562500E+06

    10 0.101323851562500E+06 0.101324414062500E+06

    11 0.101325351562500E+06 0.101325179687500E+06

    12 0.101324898437500E+06 0.101324945312500E+06

    13 0.101325039062500E+06 0.101325015625000E+06

    Press any key to continue

    Comparing of results

    If you mention the results that gain from MATLAB and FORTRAN more precisely and compare

    them with our findings in analytical solution, you can see clearly that MATLAB results is much more

    closer to these results and it is interesting that MATLAB is much more easierto program. But off

    course for learning computer programming FORTRAN

    is a better guide because it has all thefundamentals of programming.

  • 8/8/2019 Computational method for engineers

    10/21

    10Solution to the problem

    B: Second solution

    For solving this part we can make a function with one variable then we can solve it by bisection

    method atthis part again we assume that .then we should find a way to write interms of or vase versa, doing this at last we would have a equation just in terms ofor. Thecomputational will be in this order

    By looking in the figure we can see that solving this equation we can have in termsof

    (35) (36) (37) (38)Now we have

    in terms of

    .if we look atthe figure we can see thatin nod number 1 we have this

    equation and so (39)And from our previous calculation we have

    So we can write the equation (28) this way

    (41) (42)In this step we should write in terms of, so we will have (43)

  • 8/8/2019 Computational method for engineers

    11/21

    11Solution to the problem

    (44)And atlast we can show in terms oflike this (45)In the part (c) of the question it says that we should find the pressure values at all the nodesiterativelyto manage this we can have an objective function like this

    (46) (47)Our objective function is now and we should try to find the zero of this objective function by amethod that we have learned in computational method, maybe bisection method is a proper way to do

    this. Forthis project we use bisection method to solve for

    , and , by this method we can findfrom the function and putting this in equation (40) we can find a corresponding to this. Butat first we should give a boundary to the variable ofthe function, as you will see in our program we

    use 101,324 forlower bound and 101,326 for our upper bound, and for ourtolerance we use 0.0001.

    MATLAB

    function [p1,p2,p3,err]=bisect1(f,a,b,delta)

    %producer Nima Fouladine jad MM081053

    %Input - f is the function input as a string 'f'

    % - a and b are the left and right endpoints

    % - delta is the tolerance

    %Output - p2 is the value for pressure in node number 2

    % - p1 is the value for pressure in node number 1% - p3 is the value for pressure in node number 3

    % - err is theerror estimate for p2

    ya=feval(f,a);

    yb=feval(f,b);

    if ya*yb > 0,return,end

    max1=1+round((log(b-a)-log(delta))/log(2));

    fprintf('---------------------------------------------------------------------- \n');

    fprintf('iter no. p1 p2 p3 err \n');

    fprintf('--------------------------------------------------------------- -------\n');

    for k=1:max1

    p2=(a+b)/2;

    yp2=feval(f,p2);if yp2==0

    a=p2;

    b=p2;

    elseif yb*yp2>0

    b=p2;

    yb=yp2;

    else

    a=p2;

  • 8/8/2019 Computational method for engineers

    12/21

    12Solution to the problem

    ya=yp2;

    end

    yp2=feval(f,p2);

    p1=1.952587883*p2-0.952587883*101325;

    p3=101325;

    err=abs(b-a);

    fprintf('%.2d %.10e%.10e%.6e%.6e\n', k, p1, p2, p3, err);if b-a < delta, break,end

    end

    fprintf('---------------------------------------------------------------------- \n');

    fprintf(' Nima Fouladinejad***[email protected]')

    And with this program in MATLAB we achieve the results bellow for our problem

    >> format long

    >> f=@myfun;

    >> [p1,p2,p3,err]=bisect1(f,101324,101326,.0001)

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

    iter no. p1 p2 p3 err----------------------------------------- ------------------------------------------------------------

    01 1.0132500000e+005 1.0132500000e+005 1.013250e+005 1.000000e+000

    02 1.0132597629e+005 1.0132550000e+005 1.013250e+005 5.000000e-001

    03 1.0132548815e+005 1.0132525000e+005 1.013250e+005 2.500000e-001

    04 1.0132524407e+005 1.0132512500e+005 1.013250e+005 1.250000e-001

    05 1.0132512204e+005 1.0132506250e+005 1.013250e+005 6.250000e-002

    06 1.0132506102e+005 1.0132503125e+005 1.013250e+005 3.125000e-002

    07 1.0132503051e+005 1.0132501563e+005 1.013250e+005 1.562500e-002

    08 1.0132501525e+005 1.0132500781e+005 1.013250e+005 7.812500e-003

    09 1.0132500763e+005 1.0132500391e+005 1.013250e+005 3.906250e-003

    10 1.0132500381e+005 1.0132500195e+005 1.013250e+005 1.953125e-003

    11 1.0132500191e+005 1.0132500098e+005 1.013250e+005 9.765625e-004

    12 1.0132500286e+005 1.0132500146e+005 1.013250e+005 4.882813e-004

    13 1.0132500238e+005 1.0132500122e+005 1.013250e+005 2.441406e-004

    14 1.0132500215e+005 1.0132500110e+005 1.013250e+005 1.220703e-004

    15 1.0132500226e+005 1.0132500116e+005 1.013250e+005 6.103516e-005

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

    Nima Fouladinejad *** [email protected]

    p1 =

    1.013250022643536e+005

    p2 =

    1.013250011596680e+005

    p3 =

    101325

    err =

  • 8/8/2019 Computational method for engineers

    13/21

    13Solution to the problem

    6.103515625000000e-005

    Now we have all the data from , and in a table. We can save this data in a file in MATLAB by the suffix. dat . We did this and name the file rasm.dat. After that by using these data in

    MATLAB we can plot

    ,

    ,

    and

    versus the iteration number. You can see these processes

    bellow

    >> load rasm.dat

    >> x=rasm(:,1)

    >> p1=rasm(:,2)

    p1 =

    1.0e+005*

    1.01325000000000

    1.01325976290000

    1.01325488150000

    1.01325244070000

    1.01325122040000

    1.01325061020000

    1.01325030510000

    1.01325015250000

    1.01325007630000

    1.01325003810000

    1.01325001910000

    1.01325002860000

    1.01325002380000

    1.01325002150000

    1.01325002260000

    >> plot(x,p1)

    >>xlabel(iter. Number)

    >>ylabel(p1)

    >>

    rid

    Figure 3-1: p1 versus iteration number (MATLAB)

  • 8/8/2019 Computational method for engineers

    14/21

    14Solution to the problem

    >> p2=rasm(:,3)

    p2 =

    1.0e+005*

    1.01325000000000

    1.01325500000000

    1.01325250000000

    1.013251250000001.01325062500000

    1.01325031250000

    1.01325015630000

    1.01325007810000

    1.01325003910000

    1.01325001950000

    1.01325000980000

    1.01325001460000

    1.01325001220000

    1.01325001100000

    1.01325001160000

    >> plot(x,p2)>>xlabel(iter. Number)

    >>ylabel(p2)

    >>!

    rid

    Figure 3-2: p2 versus iteration number (MATLAB)

    >> p3=rasm(:,4)

    p3=101325

    101325

    101325

    101325

    101325

    101325

    101325

    101325

  • 8/8/2019 Computational method for engineers

    15/21

    15Solution to the problem

    101325

    101325

    101325

    101325

    101325

    101325

    101325>> plot(x,p3)

    >>xlabel(iter. Number)

    >>ylabel(p3)

    >>"

    rid

    Figure 3-3 p3 versus iteration number (MATLAB)

    >> err=rasm(:,5)err =

    1.00000000000000

    0.50000000000000

    0.25000000000000

    0.12500000000000

    0.06250000000000

    0.03125000000000

    0.01562500000000

    0.00781250000000

    0.00390625000000

    0.00195312500000

    0.000976562500000.00048828130000

    0.00024414060000

    0.00012207030000

    0.00006103516000

    >> plot(x,err)

    >>xlabel(iter. Number)

    >>ylabel(error)

  • 8/8/2019 Computational method for engineers

    16/21

    16Solution to the problem

    >>#

    rid

    Figure 3-4: error versus iteration number (MATLAB)

    FORTRAN

    And here is the same program but this time writes with FORTRAN

    C B $ SECTION METHOD

    C producer Nima Fouladinejad MM081053

    CInput - f is the function input as a string % F'

    C - A and B are the left and right endpoints

    C - N is the number of iterations

    COutput - p2 is the value for pressure in node number 2

    C - P1 is the value for pressure in node number 1C - P3 is the value for pressure in node number 3

    C - err is the error estimate for p2

    C

    EXTERNAL F

    DATA AF&

    101324.0/, BF/101326.0/, N/15/

    CALL BISECT(F,AF,BF,N)

    STOP

    END

    FUNCTION F(X)

    F=X-101325.00118

    RETURN

    ENDSUBROUTINE BISECT(F,A,B,N)

    FA =F(A)

    FB=F(B)

    IF( SIGN(1.0,FA) .EQ. SIGN(1.0,FB) ) THEN

    PRINT5,A,B

    RETURN

    ELSE

    PRINT3

  • 8/8/2019 Computational method for engineers

    17/21

    17Solution to the problem

    WIDTH = B - A

    DO 2 I = 1,N

    p2 = A + (B - A)*0.5

    Fp2 = F(p2)

    WIDTH = WIDTH/2.0

    IF( SIGN(1.0,FA) .' (

    .SIGN(1.0,Fp2) ) THEN

    A = p2FA = Fp2

    ELSE

    B = p2

    FB = Fp2

    ENDIF

    p1=1.952587883*p2-(0.952587883*101325)

    p3=11325.0000

    PRINT 4,I,p1,p2,p3,WIDTH

    2 CONTIN)

    E

    RET)

    RN

    ENDIF

    3 FORMAT(//4X,'STEP',10X,'p1',19X,'p2',18X,'p3',11X,'ERROR',//)4 FORMAT(2X,I5,E21.13,E21.13,E21.13,E10.3)

    5 FORMAT(2X,'F)

    NCTION HASSAME SIGN AT',2E22.14)

    END

    And here are the results ofthe problem

    STEP p1 p2 p3 ERROR

    1 0.1013249921875E+06 0.1013250000000E+06 0.1132500000000E+05 0.100E+01

    2 0.1013240156250E+06 0.1013245000000E+06 0.1132500000000E+05 0.500E+00

    3 0.1013245078125E+06 0.1013247500000E+06 0.1132500000000E+05 0.250E+00

    4 0.1013247500000E+06 0.1013248750000E+06 0.1132500000000E+05 0.125E+00

    5 0.1013248750000E+06 0.1013249375000E+06 0.1132500000000E+05 0.625E-01

    6 0.1013249296875E+06 0.1013249687500E+06 0.1132500000000E+05 0.313E-01

    7 0.1013249609375E+06 0.1013249843750E+06 0.1132500000000E+05 0.156E-01

    8 0.1013249765625E+06 0.1013249921875E+06 0.1132500000000E+05 0.781E-02

    9 0.1013249921875E+06 0.1013250000000E+06 0.1132500000000E+05 0.391E-02

    100.1013249921875E+06 0.1013250000000E+06 0.1132500000000E+05 0.195E-02

    110.1013249921875E+06 0.1013250000000E+06 0.1132500000000E+05 0.977E-03

    120.1013249921875E+06 0.1013250000000E+06 0.1132500000000E+05 0.488E-03

    13 0.1013249921875E+06 0.1013250000000E+06 0.1132500000000E+05 0.244E-03

    14 0.1013249921875E+06 0.1013250000000E+06 0.1132500000000E+05 0.122E-03

    15 0.1013249921875E+06 0.1013250000000E+06 0.1132500000000E+05 0.610E-04

    And if we enter these data in MATLAB like the previous program we can plot,, and versus the iteration number as before. So we saved this data in a file fortplan with the suffix of.dat. And again for drawing the plots we have

    >> format long

    >> load fortplan.dat

    >> x=fortplan(:,1)

    x =

    1

    2

  • 8/8/2019 Computational method for engineers

    18/21

    18Solution to the problem

    3

    4

    5

    7

    8

    9

    1112

    13

    14

    15

    >> p1=fortplan(:,2)

    p1 =

    1.0e+005*

    1.01324992187500

    1.01324015625000

    1.01324507812500

    1.01324750000000

    1.0132487

    50000001.01324929687500

    1.01324960937500

    1.01324976562500

    1.01324992187500

    1.01324992187500

    1.01324992187500

    1.01324992187500

    1.01324992187500

    1.01324992187500

    1.01324992187500

    >> plot(x,p1)

    >>xlabel(iteration Number)>>ylabel(p1)

    >>grid

    Figure 3-5: p1 versus iteration number (FORTRAN)

  • 8/8/2019 Computational method for engineers

    19/21

    19Solution to the problem

    >> p2=fortplan(:,3)

    p2 =

    1.0e+005*

    1.01325000000000

    1.01324500000000

    1.01324750000000

    1.013248750000001.01324937500000

    1.01324968750000

    1.01324984375000

    1.01324992187500

    1.01325000000000

    1.01325000000000

    1.01325000000000

    1.01325000000000

    1.01325000000000

    1.01325000000000

    1.01325000000000

    >> plot(x,p2)>>xlabel(iteration Number)

    >>ylabel(p2)

    >>grid

    Figure 3-6: p2 versus iteration number (FORTRAN)

    >> p3=fortplan(:,4)

    p3=

    1132511325

    11325

    11325

    11325

    11325

    11325

    11325

    11325

  • 8/8/2019 Computational method for engineers

    20/21

    20Solution to the problem

    11325

    11325

    11325

    11325

    11325

    11325

    >> plot(x,p3)>>xlabel(iteration Number)

    >>ylabel(p3)

    >>grid

    Figure 3-7: p3 versus iteration number (FORTRAN)

    >> error=fortplan(:,5)error =

    1.00000000000000

    0.50000000000000

    0.25000000000000

    0.12500000000000

    0.06250000000000

    0.03130000000000

    0.01560000000000

    0.00781000000000

    0.00391000000000

    0.00195000000000

    0.000977000000000.00048800000000

    0.00024400000000

    0.00012200000000

    0.00006100000000

    >> plot(x,error)

    >>xlabel(iteration Number)

    >>ylabel(error)

    >>grid

  • 8/8/2019 Computational method for engineers

    21/21

    21Solution to the problem

    Figure 3-8: error versus iteration number (FORTRAN)

    Comparing of results

    If you mention the results that gain from MATLAB and FORTRAN more precisely and compare

    them with our findings in analytical solution, you can see clearly that MATLAB results is much more

    closer to these results and it is interesting that MATLAB is much more easier to program. But off

    course for learning computer programming FORTRAN is a better guide because it has all the

    fundamentals of programming.