Numerical Methods

Numerical Integration using Trapezoidal Rule


Program in Fortran 95
===================================================================================
!Program to find Integral of a function using Trapezoidal rule
!LANGUAGE :: FORTRAN 95
!Compiler :: GNU Fortran
!===================================================================================
PROGRAM trapezoidal
      IMPLICIT NONE
      INTEGER j,n
      REAL x,upper,lower,Intg,summ,h, func
      WRITE(*,*)"INTEGRAL OF A FUNCTION BY USING TRAPEZOIDAL RULE"
      WRITE(*,*)('-',j=1,44)
      WRITE(*,*)'Enter the lower limit'
      READ*,lower
      WRITE(*,*)'Enter the upper limit'
      READ*,upper
      WRITE(*,*)'Enter the no of intervals'
      READ*,n
      h=(upper-lower)/n
      x=lower
      summ=0
      WRITE(*,10)
10    FORMAT (8X,"x",18x,"f(x)")
      WRITE(*,*)('-',j=1,32)
      DO j=0,n
          IF ((j.EQ.0).OR.(j.EQ.n)) THEN
             summ=summ+func(x)
          ELSE
             summ=summ+(2*func(x))
          END IF
          WRITE(*,*)x,'  ',func(x)
          x=x+h
      END DO
      Intg=h*summ/2
      WRITE(*,*)('-',j=1,32)
      WRITE(*,*)'Integral is : ',Intg
      STOP
      END PROGRAM

!----------------------FUNCTION SUBPROGRAM---------------------
      REAL FUNCTION func(x1)
      func=1/(1+x1)
      RETURN
      END


 --------------------OUTPUT----------------------
 INTEGRAL OF A FUNCTION BY USING TRAPEZOIDAL RULE
 ------------------------------------------------
 Enter the lower limit
 0
 Enter the upper limit
 1
 Enter the no of intervals
 10
        x                  f(x)
 --------------------------------
   0.00000000          1.00000000
  0.100000001         0.909090936
  0.200000003         0.833333313
  0.300000012         0.769230783
  0.400000006         0.714285731
  0.500000000         0.666666687
  0.600000024         0.625000000
  0.700000048         0.588235259
  0.800000072         0.555555522
  0.900000095         0.526315749
   1.00000012         0.499999970
 --------------------------------
 Integral is :   0.693771362
  
Program in C
=================================================================================== Program to Find Integral by Trapezoidal Rule LANGUAGE :: C Compiler :: GNU GCC Program By:: G.R. Mohanty , www.numericalmethods.in ===================================================================================
#include <stdio.h> double func(double x){ double y; y=1/(1+x); return y; } int main(){ int j,n; double x,upper,lower,intg,summ,h; printf("INTEGRAL OF A FUNCTION BY USING TRAPEZOIDAL RULE"); printf("\n------------------------------------------------"); printf("\nEnter the lower limit:\t"); scanf("%lf",&lower); printf("\nEnter the upper limit:\t"); scanf("%lf",&upper); printf("\nEnter the number of intervals:\t"); scanf("%d",&n); h=(upper-lower)/n; x=lower; summ=0; printf("\n%8s\t%8s","x","f(x)"); for(j=0;j<n+1;j++){ if((j==0) || (j==n)){ summ=summ+func(x); } else{ summ=summ+(2*func(x)); } printf("\n%lf\t%lf",x,func(x)); x=x+h; }
========================= OUTPUT ============================
INTEGRAL OF A FUNCTION BY USING TRAPEZOIDAL RULE
------------------------------------------------
Enter the lower limit:  0

Enter the upper limit:  1

Enter the number of intervals:  10

       x            f(x)
0.000000        1.000000
0.100000        0.909091
0.200000        0.833333
0.300000        0.769231
0.400000        0.714286
0.500000        0.666667
0.600000        0.625000
0.700000        0.588235
0.800000        0.555556
0.900000        0.526316
1.000000        0.500000

The integral is:        0.693771
Program in Scilab
======================================================================
 Program to Find Integral by Trapezoidal Rule										
 LANGUAGE  : SCILAB 
 Program By:: G.R. Mohanty , www.numericalmethods.in
=======================================================================

function y = func(x)
    y = 1 / (1 + x);
endfunction

printf("INTEGRAL OF A FUNCTION BY USING TRAPEZOIDAL RULE\n");
printf("------------------------------------------------\n");

lower = input("Enter the lower limit: ");
upper = input("Enter the upper limit: ");
n = input("Enter the number of intervals: ");

h = (upper - lower) / n;
summ = 0;

printf("%8s\t%8s\n", "x", "f(x)");

for j = 0:n
    x = lower + j * h;
    if (j == 0 | j == n) then
        summ = summ + func(x);
    else
        summ = summ + 2 * func(x);
    end
    printf("%8.6f\t%8.6f\n", x, func(x));
end

intg = h * summ / 2;
printf("\n\nThe integral is: %8.6f\n", intg);



========================= OUTPUT ============================
INTEGRAL OF A FUNCTION BY USING TRAPEZOIDAL RULE
------------------------------------------------
Enter the lower limit: 0

Enter the upper limit: 1

Enter the number of intervals: 10

       x	    f(x)
0.000000	1.000000
0.100000	0.909091
0.200000	0.833333
0.300000	0.769231
0.400000	0.714286
0.500000	0.666667
0.600000	0.625000
0.700000	0.588235
0.800000	0.555556
0.900000	0.526316
1.000000	0.500000


The integral is: 0.693771