get_formula Subroutine

private subroutine get_formula(me, formula)

Return a string with the finite difference formula.

Example

  • For 3-point backward: dfdx = (f(x-2h)-4f(x-h)+3f(x)) / (2h)

Type Bound

finite_diff_method

Arguments

Type IntentOptional Attributes Name
class(finite_diff_method), intent(in) :: me
character(len=:), intent(out), allocatable :: formula

Calls

proc~~get_formula~~CallsGraph proc~get_formula numerical_differentiation_module::finite_diff_method%get_formula proc~integer_to_string numerical_differentiation_module::integer_to_string proc~get_formula->proc~integer_to_string

Called by

proc~~get_formula~~CalledByGraph proc~get_formula numerical_differentiation_module::finite_diff_method%get_formula proc~get_finite_diff_formula numerical_differentiation_module::get_finite_diff_formula proc~get_finite_diff_formula->proc~get_formula

Source Code

    subroutine get_formula(me,formula)

    class(finite_diff_method),intent(in) :: me
    character(len=:),allocatable,intent(out) :: formula

    integer :: i !! counter
    character(len=:),allocatable :: x !! temp variable for integer to string conversion
    character(len=:),allocatable :: f !! temp variable for integer to string conversion

    if (allocated(me%dx_factors) .and. allocated(me%df_factors)) then

        formula = 'dfdx = ('

        do i = 1, size(me%dx_factors)

            if (int(me%df_factors(i))==1) then
                if (i==1) then
                    formula = formula//'f('
                else
                    formula = formula//'+f('
                end if
            elseif (int(me%df_factors(i))==-1) then
                formula = formula//'-f('
            else
                if (i==1) then
                    f = integer_to_string(int(me%df_factors(i)))
                else
                    f = integer_to_string(int(me%df_factors(i)), with_sign = .true.)
                end if
                formula = formula//trim(adjustl(f))//'f('
            end if

            if (int(me%dx_factors(i))==0) then
                formula = formula//'x'
            elseif (int(me%dx_factors(i))==1) then
                formula = formula//'x+h'
            elseif (int(me%dx_factors(i))==-1) then
                formula = formula//'x-h'
            else
                x = integer_to_string(int(me%dx_factors(i)), with_sign = .true.)
                formula = formula//'x'//trim(adjustl(x))//'h'
            end if

            formula = formula//')'

        end do

        f = integer_to_string(int(me%df_den_factor))
        if (int(me%df_den_factor)==1) then
            formula = formula//') / h'
        else
            formula = formula//') / ('//trim(adjustl(f))//'h)'
        end if

    else
        formula = ''
    end if

    end subroutine get_formula