I wanted to make a function that returns the derivative of a function at a point for part of my app. Obviously, this is the formal definition of a limit.
But what kind of a function would be able return the derivative of a function at a point in swift? Any ideas for automatic differentiation in swift?
Here is a simple numerical approach based upon your formula above. You could improve upon this:
derivativeOf
takes a functionfn
and an x-coordinatex
and returns a numerical approximation of derivative offn
atx
:If you are planning on getting the function from the user, that is the tougher part. You could give them some templates to choose from:
y = Ax^3 + Bx^2 + Cx + D
y = A * sin(B*x + C)
y = A * cos(B*x + C)
y = x ^ (1/N)
etc. And then you could have them give you A, B, C, D, or N
Let's look at how that would work for a 3rd order polynomial: