! This is a basic starting shell for your programs MODULE const INTEGER, PARAMETER :: dp = KIND(1.0d0), sp = KIND(1.0e0) REAL(KIND=dp), PARAMETER :: dpi = 3.141592653589793d0 END MODULE const ! ! Program to implement the trapezoidal rule for ! y = sin(x) + cos(x) ! on [0,dpi] using array variables ! PROGRAM prog USE const IMPLICIT NONE ! NOTE: Use NMAX to size the arrays. This eliminates the need to ! change the array sizes in numerous locations INTEGER,PARAMETER :: NMAX = 2001 INTEGER :: n,i REAL(kind=dp) :: a,b,h,total,error,exact ! Declare array variables vecx and vecy. These will hold the table ! values REAL(kind=dp) :: vecx(NMAX),vecy(NMAX) ! Set up table parameters a = 0 b = dpi WRITE(*,*) 'input n' READ(*,*) n ! Get table spacing h = (b-a)/n ! Generate just the x coordinates DO i = 1,n+1 vecx(i) = a + (i-1)*h ENDDO ! Generate the corresponding y coordinates DO i = 1,n+1 vecy(i) = SIN(vecx(i)) + COS(vecx(i)) ENDDO ! Do trap rule estimate total = 0 DO i = 1,n+1 IF((i == 1).OR.(i == n+1)) THEN total = total + vecy(i) ELSE total = total + 2*vecy(i) ENDIF ENDDO total = total*h/2 exact = -COS(b)+SIN(b) exact = exact - (-COS(a)+SIN(a)) error = (total - exact)/exact WRITE(*,*) 'error = ',error END PROGRAM prog