the program writes 20 and 200 times the last value to the array. How can you make it so that x is in the range of -2.2 with a step of 0.1 and 0.01, and at the same time the values are correctly written to the array?
do j=0,200
do x = -2, 2, 0.01
Yb(j) = exp(-x/5)
end do
end do
module integration_methods
implicit none
contains
real function trapezoid_rule(x, n)
real :: f
real :: a, b
integer, intent(in) :: n
real, dimension(n), intent(inout) :: x
integer :: k
real :: s
s = 0
do k=1, n-1
s = s + ((x(k)+x(k+1))/2*n)
end do
trapezoid_rule = s/N
end function trapezoid_rule
end module integration_methods
program integration
use integration_methods
implicit none
real, external :: s
integer :: j, i, m = 200, n = 20
Real, Allocatable, Dimension(:) :: Ya
Real, Allocatable, Dimension(:) :: Yb
real :: x, integral
ALLOCATE(Ya(1:n),Yb(1:m))
do i=0, 20
do x = -2, 2, 0.1
Ya(i) = exp(-x/5)
end do
end do
do j=0,200
do x = -2, 2, 0.01
Yb(j) = exp(-x/5)
end do
end do
integral=trapezoid_rule(Ya, 20)
write (*,*) 'Trapezoid rule = ', integral
integral=trapezoid_rule(Yb, 200)
write (*,*) 'Trapezoid rule = ', integral
deallocate(Ya,Yb)
end program integration
xfromiand removing thexloopxwould be something likex = -2.2 + i *.1maybe you also need to update the end counter f the loop.