(Since do loops normally start from I = 1,n, the notation has been switched from x0, x1…… to x1, x2….) |
c These programs are for well defined functions. Same logic will
c apply for equally spaced data: x(i), y(i), i = 1,2,......N (N odd)
c If n is even, use trapezoidal rule for two points either at the end
c or at the beginning, wherever the values of y(i) are small
c SIMPSON’S METHOD OD INTEGRATION (ARRAY GIVEN)
PROGRAM Simpson
cSimpson's method of integration
dimension x(1000),y(1000)
write(*,*)'To find the value of integral of a function using
1 Simpsons rule. '
write(*,*)'Enter the limits of integration:'
read(*,*)a,b
write(*,*)'Enter the number of subintervals(an even no.) :'
read(*,*)n
h=(b-a)/n
x(1)=a
y(1)=f(x(1))
DO 10 i=2,n+1
x(i)=x(i-1)+h
y(i)=f(x(i))
10 continue
s=y(1)+y(n+1)
DO 20 i=2,n,2
s=s+4*y(i)
20 continue
DO 30 i=3,n-1,2
s=s+2*y(i)
30 continue
s=h*s/3
write(*,40)s
40 format(1x,'The value of the integral is: ',F10.4)
STOP
END
function f(x)
f=sin(x)
c This can be generalised to any other function or simply
c a set of data points x(i) and y(i), i = 1, n
c After testing for a few functions, you can use the program
c for any set of data
return
END |