Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
symbolic derivatives of arctan(x)
#2
Code: (Select All)

_Title "numerical derivatives of arctan"

$Console:Only
_Dest _Console
Option _Explicit

Const nderivs = 20
Dim As Long n, k, m
Dim As _Integer64 sign
Dim As Double x, dx, denom

x = 1
Print "the first "; nderivs; " derivatives of arctan(x) at x="; x
For n = 0 To nderivs - 1
    denom = (1 + x ^ 2)
    If n > 0 Then
        denom = denom ^ (n + 1)
    End If
    If n And 1 Then
        sign = -1
    Else
        sign = 1
    End If
    m = n \ 2
    dx = 0
    For k = 0 To m
        dx = dx + sign * t(n, k) / denom * x ^ (n - 2 * k)
        sign = -sign
    Next
    Print "derivative("; n + 1; ") = "; dx
Next

Function binomial# (n As Long, k As Long)
    Dim As Double c
    Dim As Long i
    If k < 0 Or k > n Then
        binomial = 0
        Exit Function
    End If
    If k = 0 Or k = n Then
        binomial = 1
        Exit Function
    End If
    k = _IIf(k <= (n - k), k, (n - k)) ' Take advantage of symmetry
    c = 1
    For i = 0 To k - 1
        c = c * (n - i) / (i + 1)
    Next
    binomial = c
End Function

Function fact# (n As Long)
    If n = 0 Or n = 1 Then
        fact = 1
        Exit Function
    End If
    If n = 2 Then
        fact = 2
        Exit Function
    End If
    Dim As _Integer64 f
    Dim As Long i
    f = 2
    For i = 3 To n
        f = f * i
    Next
    fact = f
End Function

' from the pari implementation, see https://oeis.org/A131980
Function t# (n As Long, k As Long)
    Dim As Double b
    b = binomial(n + 1, 2 * k + 1)
    t = fact(n) * b
End Function

output
the first  10  derivatives of arctan(x) at x= 1
derivative( 1 ) =  .5
derivative( 2 ) = -.5
derivative( 3 ) =  .5
derivative( 4 ) =  0
derivative( 5 ) = -3
derivative( 6 ) =  15
derivative( 7 ) = -45
derivative( 8 ) =  0
derivative( 9 ) =  1260
derivative( 10 ) = -11340
Reply


Messages In This Thread
symbolic derivatives of arctan(x) - by Jack - 05-14-2025, 01:52 PM
RE: symbolic derivatives of arctan(x) - by Jack - 05-14-2025, 02:40 PM



Users browsing this thread: 2 Guest(s)