jefrosho
11-20-2009, 01:04 PM
here is the code?
program integration
!this program will aim to integrate a function supplied by the user, using different methods, within a set of 2 boundaries
implicit none
integer n1, n2
real x5, x4, x3, x2, x1, c, a1, b1, a2, b2
!file for results to be written in is opened
open (10,file= 'integration_methods.txt')
!user inputs values assigned to each coefficient in the function
print*, 'enter x^5 value'
read(*,*) x5
print*, 'enter x^4 value'
read(*,*) x4
print*, 'enter x^3 value'
read(*,*) x3
print*, 'enter x^2 value'
read(*,*) x2
print*, 'enter x value'
read(*,*) x1
print*, 'enter c value'
read(*,*) c
!*******************
!user inputs values for case 1
print*, 'boundaries for problem 1'
print*, 'enter initial x value (a)'
read(*,*) a1
print*, 'enter final x value (b)'
read(*,*) b1
print*, 'enter the n value (n)'
read(*,*) n1
!values for n's must be even integers there for an if gate is used to ensure that if an incorrect n is entered
! it will return an error
if (mod(n1,2)/=0) then
print*, 'value for n1 must be an even integer'
end if
!*******************
!user inputs values for case 2
print*, 'boundaries for problem 2'
print*, 'enter initial x value (a)'
read(*,*) a2
print*, 'enter final x value (b)'
read(*,*) b2
print*, 'enter the n value (n)'
read(*,*) n2
if (mod(n2,2)/=0) then
print*, 'value for n2 must be an even integer'
end if
!*******************
!next line will write the function as the user has entered it into the program, to ensure it is entered correctly
write(10,*) 'function to be considered is', x5, 'x**5', '+', x4, 'x**4', '+', x3, 'x**3', '+', x2, 'x**2', '+', x1, 'x', '+', c
!*******************
!subroutines are called in order
call sub_ana(x5, x4, x3, x2, x1, c)
call sub_trap1(a1, b1, n1)
call sub_trap2(a2, b2, n2)
call sub_simps1(a1, b1, n1)
call sub_simps2(a2, b1, n2)
!file where results are written is closed
close(10)
!*************************************************************************************************** ************
contains
!this subroutine will calculate the analytical solution for integration, performed on the initial function
subroutine sub_ana(x5, x4, x3, x2, x1, c)
real, intent(in) :: x5, x4, x3, x2, x1, c
double precision x5i, x4i, x3i, x2i, x1i
real num_ana1, num_ana2
!constants for each coefficient of the integrated function are calculated below
x5i=x5/6
x4i=x4/5
x3i=x3/4
x2i=x2/3
x1i=x1/2
!numerical answer for the analytical solution
num_ana1=(func(b1)-func(a1))
num_ana2=(func(b2)-func(a2))
!analytical solution, and numerical solutions for both boundaries are both written and printed
print*, 'the analytical solution for the function is'
print*, x5i, 'x**6', '+', x4i, 'x**5', '+', x3i, 'x**4', '+', x2i, 'x**3', '+', x1i, 'x**2', '+', c, 'x', '+', 'C'
print*, 'the numerical value for the analytical solution is', num_ana1
print*, 'the numerical value for the analytical solution is (a1, b1)', num_ana2
!*******************
write(10,*) 'the analytical solution for the function is'
write(10,*) x5i, 'x**6', '+', x4i, 'x**5', '+', x3i, 'x**4', '+', x2i, 'x**3', '+', x1i, 'x**2', '+', c, 'x', '+', 'C'
write(10,*) 'the numerical value for the analytical solution is', num_ana1
write(10,*) 'the numerical value for the analytical solution is (a2, b2)', num_ana2
end subroutine sub_ana
!*************************************************************************************************** ************
subroutine sub_trap1(a1, b1, n1)
!subroutine performs the first of 2 trapeziodal rules on the function with respect to a1&b1
real, intent(in) :: a1, b1
integer, intent(in) :: n1
real :: h1, s1, num_trap1
!h1 is found, step value
h1=(b1-a1)/n1
!num_trap1 is initialised
num_trap1=0
num_trap1=h1*(0.5*(func(a1)+func(b1)))
do s1=1,n1-1
num_trap1=num_trap1+func(a1+s1*h1)
end do
!***********************
!answer is both written and printed
print*, 'the numerical value for area according to the trapeziodal rule is', num_trap1
write(10,*) 'the numerical value for area according to the trapeziodal rule is', num_trap1
end subroutine
!*************************************************************************************************** ***********
subroutine sub_trap2(a2, b2, n2)
!same subroutine is carried out for problem 2
real, intent(in) :: a2, b2
integer, intent(in) :: n2
real :: h2, s2, num_trap2
h2=(b2-a2)/n2
num_trap2=0
num_trap2=h2*(0.5*(func(a2)+func(b2)))
do s2=1,n2-1
num_trap2=num_trap2+func(a2+s2*h2)
end do
!***********************
print*, 'the numerical value for area according to the trapeziodal rule is', num_trap2
write(10,*) 'the numerical value for area according to the trapeziodal rule is', num_trap2
end subroutine
!*************************************************************************************************** ********
subroutine sub_simps1(a1, b1, n1, num_simps1)
!subroutine calculates numerical value according to simpsons rule for a1 & b1
real, intent(in) :: a1, b1
integer, intent(in) :: n1
real :: h3, z, num_simps1
integer s3
num_simps1=0
h3=(b1-a1)/n1
num_simps1=func(a1)+func(b1)
!odd values only
do s3=1, n1-1, 2
z=a1+s3*h3
num_simps1=num_simps1+(4*func(z))
end do
!***********************
!even values only
do s3=2, n1-2, 2
z=a1+s3*h3
num_simps1=num_simps1+(2*func(z))
end do
num_simps1=num_simps1*(h3/3)
!***********************
!values both printed and written
print*, 'the numeical value for area accoring to simpsons rule is (a1, b1)', num_simps1
write(10,*) 'the numeical value for area accoring to simpsons rule is (a1, b1)', num_simps1
end subroutine
!*************************************************************************************************** ******
subroutine sub_simps2(a2, b2, n2, num_simps2)
!sam subroutine carried out for a2 & b2
real, intent(in) :: a2, b2
integer, intent(in) :: n2
real :: h4, z1, num_simps2
integer s4
num_simps2=0
h4=(b2-a2)/n2
num_simps2=func(a2)+func(b2)
do s4=1, n2-1, 2
z1=a2+s4*h4
num_simps2=num_simps2+(4*func(z1))
end do
!***********************
do s4=2, n2-2, 2
z1=a2+s4*h4
num_simps2=num_simps2+(2*func(z1))
end do
num_simps2=num_simps2*(h4/3)
!***********************
print*, 'the numeical value for area accoring to simpsons rule is (a2, b2)', num_simps2
write(10,*) 'the numeical value for area accoring to simpsons rule is (a2, b2)', num_simps2
end subroutine
!*************************************************************************************************** ******
real function func(x)
real x
!here the function to be used is compiled from the data which the user entered
func(x)=((x**5)*(x5))+((x**4)*(x4))+((x**3)*(x3))+((x**2)*(x2))+(x*x1)+c
end function
!*************************************************************************************************** ******
end program integration
sorry it makes it a bit difficult to read when it doesnt take the format from fortran
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.