I can't figure out how to get the right values out of this subroutine, I'm just completely stuck. there's a math error somewhere in the program, or a loop problem... I'm just not certain. How do I fix it?
SUBROUTINE atrap(i)
USE space_data
IMPLICIT NONE
INTEGER :: i, j
REAL :: f_b1, f_b2, f_x1, f_x2, trap_area
REAL :: delta_x
trap_area = 0
f_b1 = lower
f_b2 = delta_x + f_b2
f_x1 = 0
f_x2 = 0
trap_area = 0
DO j = 1, n
delta_x = (upper - lower)/(2**j)
f_b2 = delta_x + f_b1
f_x1 = a*((f_b1)**2) + b*(f_b1) + c
f_x2 = a*((f_b2)**2) + b*(f_b2) + c
trap_area = trap_area + .5*delta_x*(f_x1 + f_x2)
DO i = 2, (2**j)
f_x1 = a*((f_b1)**2) + b*(f_b1) + c
f_x2 = a*((f_b2)**2) + b*(f_b2) + c
f_b1 = f_b2
f_b2 = delta_x + f_b2
trap_area = trap_area + .5*delta_x*(f_x1 + f_x2)
END DO
WRITE(*,'(A, I2, A, I7, A, A, f16.3)') ' 2^',j,' = ',2**j,' :', ' ', trap_area
WRITE(*,*) '---------------------------------'
END DO
END SUBROUTINE atrap
Copyright © 2024 QUIZLS.COM - All rights reserved.
Answers & Comments
Verified answer
You did not initialize your variables, maybe?
f_b1, for instance, is set to 'lower'. But where is the value of 'lower'?
Unless it is set in 'space_data', which is not shown.
One variable that is sure local and not initialized is 'delta_x'.
It is pretty hard to do a diagnostic if you do not show all of the code; please include the content of the 'space_data' module.