Advertisement
Guest User

Untitled

a guest
May 3rd, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ******Integrable function.
  2.       REAL*4 FUNCTION fun(x)
  3.       IMPLICIT NONE
  4.       REAL*4 x
  5.       fun = x**2
  6.       END
  7.  
  8.  
  9. ******Reading parameters from a file.
  10.       SUBROUTINE InputParams
  11.       IMPLICIT NONE
  12.       REAL*4 a, b, dx
  13.       INTEGER n_steps
  14.       COMMON/params/a, b, dx, n_steps
  15.       OPEN(1, FILE = 'input.txt')
  16.       READ(1, *)a, b, n_steps
  17.       dx = (b - a)/n_steps
  18.       CLOSE(1)
  19.       END    
  20.      
  21.      
  22. ******Creating a grid.
  23.       SUBROUTINE CreateGrid(grid, n_steps)
  24.       IMPLICIT NONE
  25.       REAL*4 grid
  26.       INTEGER n_steps, i
  27.       DIMENSION grid(n_steps + 1)
  28.       REAL*4 a, b, dx, point
  29.       COMMON/params/a, b, dx
  30.       n_steps = n_steps + 1
  31.       point = a
  32.       grid(1) = point
  33.       DO i = 2, n_steps
  34.         point = point + dx
  35.         grid(i) = point
  36.       END DO
  37.       END
  38.  
  39.  
  40. ******Method of trapeziums.
  41.       REAL*4 FUNCTION Trap(grid, n_steps)
  42.       IMPLICIT NONE
  43.       REAL*4 grid
  44.       INTEGER n_steps, i
  45.       DIMENSION grid(n_steps)
  46.       REAL*4 a, b, dx, sum, fun
  47.       COMMON/params/a, b, dx
  48.       sum = 0.0
  49.       DO i = 2, n_steps - 1
  50.         sum = sum + fun(grid(i))
  51.       END DO
  52.       Trap = (sum + (fun(grid(1)) + fun(grid(n_steps)))/2.0)*dx
  53.       END  
  54.      
  55.      
  56. ******Gauss2 method.
  57.       REAL*4 FUNCTION Gauss2(grid, n_steps)
  58.       IMPLICIT NONE
  59.       REAL*4 grid, q, x
  60.       INTEGER n_steps, i, j
  61.       DIMENSION grid(n_steps), x(2)
  62.       REAL*4 sum, sum_int, a, b, h, x1, fun
  63.       COMMON/params/a, b
  64.       DATA x / -0.5773503, 0.5773503 /
  65.       q = 1.0
  66.       h = (b-a)/(n_steps - 1)
  67.       DO i = 1,2
  68.         sum_int = 0.0
  69.         DO j = 2, n_steps
  70.             x1 = (grid(j-1)+grid(j)+x(i)*h)/2
  71.             sum_int=sum_int+h*fun(x1)
  72.         END DO
  73.         sum = sum + q * sum_int
  74.       END DO
  75.       Gauss2 = sum/2.0
  76.       END    
  77.      
  78.      
  79. ******Main function.
  80.       PROGRAM LAB4
  81.       IMPLICIT NONE
  82.       REAL*4 array
  83.       DIMENSION array(1000000)
  84.       REAL*4 a, b, dx, ans, Trap, Gauss2
  85.       INTEGER n_steps
  86.       COMMON/params/a, b, dx, n_steps
  87.      
  88.       CALL InputParams
  89.      
  90.       CALL CreateGrid(array(1), n_steps)
  91.      
  92.       ans = Trap(array(1), n_steps)
  93.       PRINT *,'Method of trapeziums:'
  94.       PRINT *, ans
  95.            
  96.       ans = Gauss2(array(1), n_steps)
  97.       PRINT *,'Gauss2 method:'
  98.       PRINT *, ans  
  99.      
  100.       PAUSE
  101.       END
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement