Skip to content

Instantly share code, notes, and snippets.

@AndreasKuhn-ak
Created April 12, 2022 14:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AndreasKuhn-ak/0fbba22c28e57ccef5eadded382a48ba to your computer and use it in GitHub Desktop.
Save AndreasKuhn-ak/0fbba22c28e57ccef5eadded382a48ba to your computer and use it in GitHub Desktop.
Fortran logistic
subroutine f(u, r)
implicit none
real(kind(0.d0)), intent(out) :: r
real(kind(0.d0)), intent(in) :: u
r = u*(1.d0-u)
end subroutine f
subroutine fortran_logistic(N, dt, u)
integer, intent(in) :: N
real(kind(0.d0)), intent(in) :: dt
real(kind(0.d0)), intent(out) :: u(N)
integer :: i
real(kind(0.d0)) :: temp
u(1) = 1.d-5
do i=1, N-1
call f(u(i), temp)
u(i+1) = u(i) + dt*temp
end do
end subroutine fortran_logistic
@MarDiehl
Copy link

I would recommend to use functions instead of subroutines:

function fortran_logistic(N, dt) result(u)
  implicit none

  integer, intent(in) :: N
  real(kind(0.d0)), intent(in) :: dt
  real(kind(0.d0)) :: u(N)

  integer :: i

  u(1) = 1.d-5
  do i=1, N-1
    u(i+1) = u(i) + dt*f(u(i))
  end do

  contains
  function f(u)
    real(kind(0.d0)), intent(in) :: u
    real(kind(0.d0)) :: f

    f = u*(1.d0-u)

  end function f

end function fortran_logistic

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment