Calculates the derivative of a scalar-valued function w.r.t. a scalar-valued variable using finite difference methods (forward, backward, central).
Type | Intent | Optional | Attributes | Name | ||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
private impure function f(z) result(fz)Arguments
Return Value real(kind=rk)scalar function |
||||||||||||||||||||
real(kind=rk), | intent(in) | :: | x |
scalar variable |
||||||||||||||||
real(kind=rk), | intent(in) | :: | h |
perturbation for finite difference methods |
||||||||||||||||
character(len=*), | intent(in) | :: | method |
finite difference method (forward, backward, central) |
derivative of w.r.t.
impure function finite_difference_T0_T0(f,x,h,method) result(dfdx) real(rk), intent(in) :: x !! scalar variable real(rk), intent(in) :: h !! perturbation for finite difference methods character(*), intent(in) :: method !! finite difference method (forward, backward, central) real(rk) :: dfdx !! derivative of \(f\) w.r.t. \(x\) interface !! scalar-valued function to differentiate impure function f(z) result(fz) use kinds real(rk), intent(in) :: z !! scalar variable real(rk) :: fz !! scalar function end function f end interface if (abs(h)<tiny(0.0_rk)) error stop 'Division by zero. Please provide a non-zero value for h.' select case (method) case('forward') dfdx = finite_difference_forward_T0_T0(f,x,h) case('backward') dfdx = finite_difference_backward_T0_T0(f,x,h) case('central') dfdx = finite_difference_central_T0_T0(f,x,h) end select end function finite_difference_T0_T0