Integration of cos(x)
We can find analytical value as below,
The solution from the "I" in above range through the Simpson rule we can get 0.00000 as an approximation value.
There we can’t calculate the absolute relative true error. Because the actual value of the integration of cos(x) from 0 to 180 degrees it gets zero. If we will find absolute relative approximation error there should be happened division by zero.
The most optimum value of the “h” should be an average value such as “0.01”. It means we should select average number of segments as 48 or 54. Because if we select very large h it decreases the number of segments. As a result we can’t get accurate value. And if we select very small “h” it increases the number of segments. But it should affect to increase the round-off error. That is the reason for we must have to select average value of number of segments to get “h”.
Implementation in FORTRAN
1.)Do the compilation as below.
gfortran -ffree-form simpsoncos.f95
./a.out
2.)User must have to input the lower limit and upper limit of the integrations(in degrees).
E.g.: Lower limit : 0
Upper limit : 180
3.)Select the choice Simpson1/3(enter-1) or Simpson3/8(enter-2)
E.g.: Enter the choice : 1
4.)Enter the number of sub intervals.
E.g.: Enter the number of sub intervals : 50
5.)Now you can see the outputs on a Terminal.
E.g.: Answer of cos(x) is : 0.00000
gfortran -ffree-form simpsoncos.f95
./a.out
2.)User must have to input the lower limit and upper limit of the integrations(in degrees).
E.g.: Lower limit : 0
Upper limit : 180
3.)Select the choice Simpson1/3(enter-1) or Simpson3/8(enter-2)
E.g.: Enter the choice : 1
4.)Enter the number of sub intervals.
E.g.: Enter the number of sub intervals : 50
5.)Now you can see the outputs on a Terminal.
E.g.: Answer of cos(x) is : 0.00000
simpsoncos.f95 Code
program simpsoncos
Integer :: interval, i, choice
real :: a, b, rslt, h, pi
real :: evensum = 0.0, oddsum = 0.0, total = 0.0
!Take the inputs from the user to identify the integration range
print *, ''
print *, "...Enter the lower limit and upper limit of the integrations (in degrees)..."
print *, ''
write(*,2,advance="no") "Lower limit : "
read *, a
write(*,2,advance="no") "Upper limit : "
read *, b
print *,"------------------------Choose an option---------------------------------"
print *, '1. Simpson 1/3 rule'
print *, '2. Simpson 3/8 rule'
print *,"-------------------------------------------------------------------------"
!Take the choice from the user
write (*,2,advance="no") "Enter the choice : "
2 format('',A20)
read *,choice
!Take the number of sub segments from the user
write (*,3,advance="no") "Enter the number of sub intervals : "
3 format('',A37)
read *, interval
pi = 4.0 * atan(1.0) !Get the pi value
!Convert a and b in to pi values
a = a*pi/180
b = b*pi/180
h = (b-a)/interval !Calculation of the step size(h)
if(choice == 1 .and. mod(interval,2) == 0)then
rslt = simp13(a,b) !Call to the simpson 1/3 rd rule
else if(choice == 2 .and. mod(interval,3) == 0)then
rslt = simp38(a,b) !Call to the simpson 3/8 rule
else
print *, "Invalid input.........."
stop
end if
!Print output on the console
print *,"-------------------------------------------------------------------------"
write(*,20)rslt
20 format(1x,'Answer of cos(x) is : ',F10.5)
print *,"-------------------------------------------------------------------------"
contains
!Simpson 1/3 rd function
function simp13(a,b)
real :: simp13, a, b
do i=1, (interval-1)
if(mod(i,2) == 0)then
evensum = evensum + f((a+(i*h)))
else
oddsum = oddsum + f((a+(i*h)))
end if
end do
!Simpson 1/3 rd formula
simp13 = (h/3) * (f(a) + 2*evensum + 4*oddsum + f(b))
end function simp13
!Simpson 3/8 function
function simp38(a,b)
real :: simp38, a, b
do i=1, (interval-1)
if(mod(i,3) == 0)then
total = total + 2*f((a+(i*h)))
else
total = total + 3*f((a+(i*h)))
end if
end do
!Simpson 3/8 formula
simp38 = (3*h/8) * (f(a) + total + f(b))
end function simp38
end program simpsoncos
!Function of f(x)
function f(x)
implicit none
real :: f, x
f = cos(x)
end function f
Integer :: interval, i, choice
real :: a, b, rslt, h, pi
real :: evensum = 0.0, oddsum = 0.0, total = 0.0
!Take the inputs from the user to identify the integration range
print *, ''
print *, "...Enter the lower limit and upper limit of the integrations (in degrees)..."
print *, ''
write(*,2,advance="no") "Lower limit : "
read *, a
write(*,2,advance="no") "Upper limit : "
read *, b
print *,"------------------------Choose an option---------------------------------"
print *, '1. Simpson 1/3 rule'
print *, '2. Simpson 3/8 rule'
print *,"-------------------------------------------------------------------------"
!Take the choice from the user
write (*,2,advance="no") "Enter the choice : "
2 format('',A20)
read *,choice
!Take the number of sub segments from the user
write (*,3,advance="no") "Enter the number of sub intervals : "
3 format('',A37)
read *, interval
pi = 4.0 * atan(1.0) !Get the pi value
!Convert a and b in to pi values
a = a*pi/180
b = b*pi/180
h = (b-a)/interval !Calculation of the step size(h)
if(choice == 1 .and. mod(interval,2) == 0)then
rslt = simp13(a,b) !Call to the simpson 1/3 rd rule
else if(choice == 2 .and. mod(interval,3) == 0)then
rslt = simp38(a,b) !Call to the simpson 3/8 rule
else
print *, "Invalid input.........."
stop
end if
!Print output on the console
print *,"-------------------------------------------------------------------------"
write(*,20)rslt
20 format(1x,'Answer of cos(x) is : ',F10.5)
print *,"-------------------------------------------------------------------------"
contains
!Simpson 1/3 rd function
function simp13(a,b)
real :: simp13, a, b
do i=1, (interval-1)
if(mod(i,2) == 0)then
evensum = evensum + f((a+(i*h)))
else
oddsum = oddsum + f((a+(i*h)))
end if
end do
!Simpson 1/3 rd formula
simp13 = (h/3) * (f(a) + 2*evensum + 4*oddsum + f(b))
end function simp13
!Simpson 3/8 function
function simp38(a,b)
real :: simp38, a, b
do i=1, (interval-1)
if(mod(i,3) == 0)then
total = total + 2*f((a+(i*h)))
else
total = total + 3*f((a+(i*h)))
end if
end do
!Simpson 3/8 formula
simp38 = (3*h/8) * (f(a) + total + f(b))
end function simp38
end program simpsoncos
!Function of f(x)
function f(x)
implicit none
real :: f, x
f = cos(x)
end function f
Comments
Post a Comment