Programming with Functions

  1. Compute voltage across a capacitor using the formula \(v(t)=(t+0.1)e^{\sqrt(t)}\)
  2. c ---------------------------------------------- c Main Code c ---------------------------------------------- program volcap implicit none c This program computes the voltage across a capacitor between start c and finish times c Declare the variables real time0, timef, v, volt, step, time c Format Descriptors 20 format(1X, ' Time Voltage ') 30 format(1X, '=======================') 40 format(3X, F5.2, F12.4) c Ask for the input print *, 'Please the starting times, ending time and the' print *, 'step size to produce the table' read *, time0, timef, step print *, '' print *, '' print *, '' print 20 print 30 do 10 time = time0, timef, step c Call the function to compute the voltage v = volt(time) c Print the answer print 40, time, v 10 continue end program volcap c ---------------------------------------------- c Function Definitions c ---------------------------------------------- real function volt(time) real time c The equation to compute the voltage volt = (time + 0.1)*exp(sqrt(time)) end function volt



    Sample Run:
    Please the starting times, ending time and the
    step size to produce the table
    0, 2, 0.125
    
        Time     Voltage    
     =======================
        0.00      0.1000
        0.12      0.3204
        0.25      0.5771
        0.38      0.8763
        0.50      1.2169
        0.62      1.5984
        0.75      2.0208
        0.88      2.4846
        1.00      2.9901
        1.12      3.5381
        1.25      4.1294
        1.38      4.7648
        1.50      5.4453
        1.62      6.1717
        1.75      6.9453
        1.88      7.7669
        2.00      8.6378 
    
    
  3. Compute the value of poisson probability using the formula \(P(n)=\frac{\lambda^{n}e^{-\lambda}}{n!}\) where
    \(\lambda\) = Average number of occurences of a phenomenon per unit time
    \(n\) = Number of occurences of that phenomenon in that period of time
  4. c ----------------------------------------------
    c     Main Code
    c ----------------------------------------------
    
          program poison
    
    c     This program computes the value of poisson probability
    
          implicit none
    
    c     Declare the variables
          real lambda, nocur, factor, pdf
    
    c     Take inputs      
          print 30
      30  format('Enter Lambda and N')
          read*, lambda, nocur  
    
    c     Call the function that computes the poisson probability
    c     and print the answer      
          print 20, pdf(lambda,nocur)
      20  format('pdf = ', F6.4)
    
          
          end program poison
    
    
    
    c ----------------------------------------------
    c     Function Definitions
    c ----------------------------------------------
    
    c     This function computes the poisson probability
          function pdf(L,N)
    
          implicit none
    
          real L,N, pdf, factor
    
          pdf = ((L**N)*exp(-L))/factor(N)
    
          end function pdf
    
    
    c     This function computes the factorial of a number
          function factor(num)
    
          implicit none
    
          real num, factor
    
          factor = 1
    
          do 10 num = 1, num, 1
             factor = factor*num
      10  continue
    
          end function factor                       
    
    
    Enter Lambda and N
    4, 6
    pdf = 0.1042
    
    
  5. Computer code to compute deflection of a cantilever beam using the formulae

    case 1: End Load
    \(\delta = \frac{-W}{6EI}(x^3-3l^{2}x+2l^3)\)

    case 2: Uniform Load
    \(\delta = \frac{-W}{24EIl}(x^4-4l^{3}x+3l^4)\)

    case 3: Intermediate Load

    for load between \(A\) and \(B\)
    \(\frac{-W}{6EI}(-a^{3} + 3a^{2}l-3a^{2}x)\)
    for load between \(B\) and \(C\)
    \(\frac{-W}{6EI}[(x-b)^{3} - 3a^{2}(x-b)+2a^3\)]

  6. 
    c ----------------------------------------------
    c     Main Code
    c ----------------------------------------------
    
          program beamdf
    
    c     This program computes cantilever beam deflelection 
    c     for three cases of loading
    c     ------------------------------------------------
    c     1. End load
    c     2. Intermediate Loading
    c     3. Uniform loading
    
          implicit none
    
          real W, E, I, x, L, delta, deflel, step, deflul, xld, deflil
          integer choice
    
    c     Basic Material properties of the beam
          print *, 'Please enter the Modulus of Elasticity, Moment of'
          print*, 'inertia and length of beam'
          read*, E,I,L
    
    c     The Magnitude of Load
          print*, 'Please enter the magnitude of the Load'
          read*, W
    
    c     The step size of the length increment
          print*, 'Please enter the step size of the length increment'
          read*, step
    
    c     Ask the user for the type of loading
          print *, 'Please make a choice:'
          print *, '1: End Load'
          print *, '2: Uniform Load'
          print *, '3: Uniform Load'
          read *, choice
    
          if (choice == 3) then
             print*, 'How far is the load from the support ?'
             read*, xld
          end if 
    
    
          print 30
      30  format('   X          DEF')
    
          print 40
      40  format('======================')
    
      10  format (F6.2,E15.4) 
    
          do 20 x=0,L,step
    
             if (choice == 1) then
                delta = deflel(W,x,L,I,E)
             else if (choice == 2) then   
                delta = deflul(W,x,L,I,E)
             else if (choice == 3) then   
                delta = deflil(W,x,L,I,E,xld)
             else 
                print *, 'Invalid Choice'
             end if
    
             print 10, x, delta
    
      20  continue  
    
          end program beamdf
    
    
    c ----------------------------------------------
    c     Function Definitions
    c ----------------------------------------------
    
    c     The function that calculates the deflection due to end load      
          function deflel(W,x,L,I,E)
    
             implicit none
    
             real W, E, I, x, L, deflel
    
             deflel = (-W/(6*E*I))*(x**(3) - 3*L**(2)*x + 2*L**(3))
    
          end function deflel
    
    
    c     The function that calculates the deflection due to uniform load      
          function deflul(W,x,L,I,E)
    
             implicit none
    
             real W, E, I, x, L, deflul
    
             deflul = (-W/(24*E*I*L))*(x**(4) - 4*L**(3)*x + 3*L**(4))
    
          end function deflul
    
    
    c     The function that calcs the deflection due to intermediate load      
          function deflil(W,x,L,I,E,xld)
    
             implicit none
    
             real W, E, I, x, L, deflil,xld
             real a,b
    
             a = xld
             b = L - xld
    
             if (x .le. a) then
                deflil = (-W/(6*E*I))*(-a**(3) + 3*a**(2)*L-3*a**(2)*x)
             else
                deflil = (-W/(6*E*I))*((x-b)**(3) - 3*a**(2)*(x-b) +
         +         2*a**(3))
             end if
    
          end function deflil
    
    Sample Run:
    Please enter the Modulus of Elasticity, Moment of inertia and length of beam
    2.05E11, 4.15E-8 1.0
    
    Please enter the magnitude of the Load
    125
    
    Please enter the step size of the length increment
    0.1
    
    Please make a choice:
    1: End Load
    2: Uniform Load
    3: Uniform Load
    1
    
       X          DEF
    ======================
      0.00    -0.4898E-02
      0.10    -0.4165E-02
      0.20    -0.3448E-02
      0.30    -0.2760E-02
      0.40    -0.2116E-02
      0.50    -0.1531E-02
      0.60    -0.1019E-02
      0.70    -0.5951E-03
      0.80    -0.2743E-03
      0.90    -0.7102E-04