Numerical Integration using Simpson's 1/3 Rule
Program in Fortran 95
===================================================================================
!Program to find Integral of a function using Simpson's 1/3 rule
!LANGUAGE :: FORTRAN 95
!Compiler :: GNU Fortran
!===================================================================================
PROGRAM simpson1_3rd
INTEGER j,n
REAL x,upper,lower,I,summ,h
PRINT*,'PROGRAM TO FIND THE INTEGRAL OF 1/(1+X) BY SIMPSON''S 1/3 METHOD'
PRINT*,('-',j=1,65)
PRINT*,'Enter the lower limit'
READ*,lower
PRINT*,'Enter the upper limit'
READ*,upper
PRINT*,'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 IF (MOD(j,2).NE.0) THEN
summ=summ+(4*func(x))
ELSE IF (MOD(j,2).EQ.0) THEN
summ=summ+(2*func(x))
END IF
PRINT*,x,' ',func(x)
x=x+h
END DO
I=h*summ/3
PRINT*,'Integral is : ',I
STOP
END PROGRAM
!------------------FUNCTION SUBPROGRAM----------------
REAL FUNCTION func(x1)
func=1/(1+x1)
RETURN
END
------------------------------OUTPUT-----------------------------
PROGRAM TO FIND THE INTEGRAL OF 1/(1+X) BY SIMPSON'S 1/3 METHOD
-----------------------------------------------------------------
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.693150222
Program in C
==================================================================================================
Program to find Integral of a function using Simpson's 1/3 rule
LANGUAGE :: C
Compiler :: GNU GCC
Program By:: G.R. Mohanty , www.numericalmethods.in
==================================================================================================
#include <stdio.h>
float func(float x) {
return 1.0/(1.0 + x);
}
int main() {
int j, n;
float x, upper, lower, I, summ, h;
printf("PROGRAM TO FIND THE INTEGRAL OF 1/(1+X) BY SIMPSON'S 1/3 METHOD\n");
for (j = 1; j <= 65; j++) {
printf("-");
}
printf("\n");
printf("Enter the lower limit: ");
scanf("%f", &lower);
printf("Enter the upper limit: ");
scanf("%f", &upper);
printf("Enter the number of intervals: ");
scanf("%d", &n);
h = (upper - lower)/n;
x = lower;
summ = 0.0;
/*Simpson's 1/3 Rule Calculation: Uses a loop to apply Simpson's 1/3 rule.
Depending on whether j is at the boundaries (j == 0 or j == n) or is odd/even,
it adds func(x) multiplied by appropriate coefficients (1, 4, or 2) to summ.*/
printf("\nx f(x)\n");
for (j = 0; j <= n; j++) {
if (j == 0 || j == n) {
summ += func(x);
} else if (j % 2 != 0) {
summ += 4*func(x);
} else {
summ += 2*func(x);
}
printf("%8.6f %8.6f\n", x, func(x));
x += h;
}
I = h*summ/3.0;
printf("\nIntegral is : %8.6f\n", I);
return 0;
}
========================= OUTPUT ============================
PROGRAM TO FIND THE INTEGRAL OF 1/(1+X) BY SIMPSON'S 1/3 METHOD
-----------------------------------------------------------------
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
Integral is : 0.693252
Program in Scilab
======================================================================
Program to find Integral of a function using Simpson's 1/3 rule
LANGUAGE : SCILAB
Program By:: G.R. Mohanty , www.numericalmethods.in
=======================================================================
function f = func(x)
f = 1/(1+x);
endfunction
printf("PROGRAM TO FIND THE INTEGRAL OF 1/(1+X) BY SIMPSON''S 1/3 METHOD\n");
printf("----------------------------------------------------------------");
lower = input("Enter the lower limit:");
upper = input("Enter the upper limit:");
n = input("Enter the number of intervals:");
h = (upper-lower)/n;
x = lower;
summ = 0.0;
disp("x f(x)");
for j = 0:n
if (j == 0 | j == n) then
summ = summ + func(x);
elseif modulo(j,2) <> 0 then
summ = summ + 4*func(x);
else
summ = summ + 2*func(x);
end
printf("%8.6f %8.6f\n", x, func(x));
x = x + h;
end
I = h*summ/3.0;
printf("Integral is :%f ",I);
========================= OUTPUT ============================
PROGRAM TO FIND THE INTEGRAL OF 1/(1+X) BY SIMPSON'S 1/3 METHOD
----------------------------------------------------------------
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
Integral is :0.693150
PROGRAM simpson1_3rd
INTEGER j,n
REAL x,upper,lower,I,summ,h
PRINT*,'PROGRAM TO FIND THE INTEGRAL OF 1/(1+X) BY SIMPSON''S 1/3 METHOD'
PRINT*,('-',j=1,65)
PRINT*,'Enter the lower limit'
READ*,lower
PRINT*,'Enter the upper limit'
READ*,upper
PRINT*,'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 IF (MOD(j,2).NE.0) THEN
summ=summ+(4*func(x))
ELSE IF (MOD(j,2).EQ.0) THEN
summ=summ+(2*func(x))
END IF
PRINT*,x,' ',func(x)
x=x+h
END DO
I=h*summ/3
PRINT*,'Integral is : ',I
STOP
END PROGRAM
!------------------FUNCTION SUBPROGRAM----------------
REAL FUNCTION func(x1)
func=1/(1+x1)
RETURN
END
==================================================================================================
Program to find Integral of a function using Simpson's 1/3 rule
LANGUAGE :: C
Compiler :: GNU GCC
Program By:: G.R. Mohanty , www.numericalmethods.in
==================================================================================================
#include <stdio.h>
float func(float x) {
return 1.0/(1.0 + x);
}
int main() {
int j, n;
float x, upper, lower, I, summ, h;
printf("PROGRAM TO FIND THE INTEGRAL OF 1/(1+X) BY SIMPSON'S 1/3 METHOD\n");
for (j = 1; j <= 65; j++) {
printf("-");
}
printf("\n");
printf("Enter the lower limit: ");
scanf("%f", &lower);
printf("Enter the upper limit: ");
scanf("%f", &upper);
printf("Enter the number of intervals: ");
scanf("%d", &n);
h = (upper - lower)/n;
x = lower;
summ = 0.0;
/*Simpson's 1/3 Rule Calculation: Uses a loop to apply Simpson's 1/3 rule.
Depending on whether j is at the boundaries (j == 0 or j == n) or is odd/even,
it adds func(x) multiplied by appropriate coefficients (1, 4, or 2) to summ.*/
printf("\nx f(x)\n");
for (j = 0; j <= n; j++) {
if (j == 0 || j == n) {
summ += func(x);
} else if (j % 2 != 0) {
summ += 4*func(x);
} else {
summ += 2*func(x);
}
printf("%8.6f %8.6f\n", x, func(x));
x += h;
}
I = h*summ/3.0;
printf("\nIntegral is : %8.6f\n", I);
return 0;
}
function f = func(x)
f = 1/(1+x);
endfunction
printf("PROGRAM TO FIND THE INTEGRAL OF 1/(1+X) BY SIMPSON''S 1/3 METHOD\n");
printf("----------------------------------------------------------------");
lower = input("Enter the lower limit:");
upper = input("Enter the upper limit:");
n = input("Enter the number of intervals:");
h = (upper-lower)/n;
x = lower;
summ = 0.0;
disp("x f(x)");
for j = 0:n
if (j == 0 | j == n) then
summ = summ + func(x);
elseif modulo(j,2) <> 0 then
summ = summ + 4*func(x);
else
summ = summ + 2*func(x);
end
printf("%8.6f %8.6f\n", x, func(x));
x = x + h;
end
I = h*summ/3.0;
printf("Integral is :%f ",I);