Skip to content

Derivative free 1D function minimizer in modern Fortran

License

Notifications You must be signed in to change notification settings

jacobwilliams/fmin

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

25 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

stl-fortran

Derivative free 1D function minimizer in modern Fortran

Status

Language GitHub release Build Status codecov last-commit

Compiling

A Fortran Package Manager manifest file is included, so that the library and test cases can be compiled with FPM. For example:

fpm build --profile release
fpm test --profile release

By default, the library is built with double precision (real64) real values. Explicitly specifying the real kind can be done using the following preprocessor flags:

Preprocessor flag Kind Number of bytes
REAL32 real(kind=real32) 4
REAL64 real(kind=real64) 8
REAL128 real(kind=real128) 16

For example, to build a single precision version of the library, use:

fpm build --profile release --flag "-DREAL32"

To use fmin within your fpm project, add the following to your fpm.toml file:

[dependencies]
fmin = { git="https://github.com/jacobwilliams/fmin.git" }

Example

program test

use fmin_module
use iso_fortran_env, only: wp => real64 ! double precision

real(wp) :: xmin, xerr

real(wp),parameter :: ax  = -4.0_wp       ! lower bound
real(wp),parameter :: bx  = 0.0_wp        ! upper bound
real(wp),parameter :: tol = 1.0e-8_wp     ! tolerance
real(wp),parameter :: pi  = acos(-1.0_wp) ! pi
real(wp),parameter :: x  = -pi/2.0_wp     ! true answer

xmin = fmin(func,ax,bx,tol) ! compute the minimum

xerr = xmin - x  ! difference from true value

write(*,*) 'xmin       = ', xmin
write(*,*) 'xmin exact = ', x
write(*,*) 'xmin error = ', xerr

contains

    function func(x) result(f)

    implicit none

    real(wp),intent(in) :: x  !! indep. variable
    real(wp)            :: f  !! function value `f(x)`

    f = sin(x)

    end function func

end program test

The output is:

 xmin       =   -1.5707963254967554
 xmin exact =   -1.5707963267948966
 xmin error =    1.2981411501300499E-009

Documentation

  • The API documentation for the current master branch can be found here. This is generated by processing the source files with FORD.

License

  • The Fmin source code and related files and documentation are distributed under a permissive free software license (BSD-3).

See also