Fortran passing statement function to function -



Fortran passing statement function to function -

i looking pass statement function evaluated in function, possible? trying have function can numerically integrate general function. there other values in real problem don't want have external function.

edit 2014/11/1 02:22 gmt

there couple of bugs in code @ianh set in. code works after take care of bugs.

program trapezoidz implicit none ! main programme global variables , constants here. phone call main contains subroutine main real :: u, integral1 integer :: integer, parameter :: n = 5000 ! delete declaration of trapezoid_integration , f. integral1 = trapezoid_integration(trigsin, n, 0., 3.14159 / 2.0) write (*,*) "i - trap = ",1 - integral1 end subroutine main function my_f(u) real :: my_f real, intent(in) :: u my_f = (u**4)*exp(u)/((exp(u)-1.0)**2) end function my_f function trigsin(u) result(my_f) real :: my_f real, intent(in) :: u my_f = sin(u) end function trigsin function trapezoid_integration (f, n, start_val, end_val) result (integral) ! delete declaration of integrand (assuming have it). procedure(my_f) :: f integer :: n real :: start_val, end_val real :: integral,u,h integer :: integral = 0.0 i=0,n u = start_val + ((end_val - start_val)*i)/n ! implement eqn g.4 if ((i.eq.0).or.(i.eq.n)) integral = integral+integrand(f, u) else integral = integral+(2.0*integrand(f, u)) end if end h=(end_val - start_val)/n integral = (h/2.0)*integral end function trapezoid_integration function integrand(f, x) result (value) implicit none real :: x real :: value real :: f if (x .lt. 0.00001) x = 0.00001 end if value = f(x) end function integrand end programme trapezoidz

below original code. not use

program trapezoidz implicit none integer, parameter :: n = 10 real :: u, integral integer :: real :: f, trapezoid_integration f(u) = (u**4)*exp(u)/((exp(u)-1.0)**2) integral = trapezoid_integration(f, n, 0.2, 3.0) write (*,*) '#trapezoidal integration = ',integral end programme trapezoidz function trapezoid_integration(f, n, start_val, end_val) implicit none integer :: n real :: start_val, end_val real :: f real :: integral,u,h integer :: integral = 0.0 i=0,n u = start_val + ((end_val - start_val)*i)/n ! implement eqn g.4 if ((i.eq.0).or.(i.eq.n)) integral = integral+integrand(f, u) else integral = integral+(2.0*integrand(f, u)) end if end h=(end_val - start_val)/n integral = (h/2.0)*integral end subroutine trapezoid_integration function integrand(f, x) result (value) implicit none real :: x real :: value real :: f if (x .lt. 0.00001) x = 0.00001 end if value = f(x) end function integrand

it not permitted in standard fortran - statement function not in list of permitted procedure types in constraint c1235 in fortran 2008.

however, constraint indicates, not restricted external functions. since fortran 90 actual argument can module procedure. of fortran 2008 can internal procedure.

that offers possibility of compact , much more robust source:

program trapezoidz implicit none ! main programme global variables , constants here. phone call main contains subroutine main real :: u, integral integer :: integer, parameter :: n = 10 ! delete declaration of trapezoid_integration , f. integral = trapezoid_integration(my_f, 0.2, 3.0) write (*,*) '#trapezoidal integration = ',integral end subroutine main function my_f(u) real :: my_f real, intent(in) :: u my_f = (u**4)*exp(u)/((exp(u)-1.0)**2) end function my_f function trapezoid_integration(f, n, start_val, end_val) ! delete declaration of integrand (assuming have it). procedure(my_f) :: f ... end function trapezoid_integration function integrand(f, x) result (value) ... end function integrand end programme trapezoidz

function fortran

Comments

Popular posts from this blog

xslt - DocBook 5 to PDF transform failing with error: "fo:flow" is missing child elements. Required content model: marker* -

mediawiki - How do I insert tables inside infoboxes on Wikia pages? -

Local Service User Logged into Windows -