Skip to content

Instantly share code, notes, and snippets.

@wulab
Last active September 27, 2020 15:13
Show Gist options
  • Save wulab/b93869589a876419d0a3b67c9e641cf6 to your computer and use it in GitHub Desktop.
Save wulab/b93869589a876419d0a3b67c9e641cf6 to your computer and use it in GitHub Desktop.
Derivative of a Function
def deriv(f)
dx = 0.000001
->(x) { (f.(x+dx) - f.(x)) / dx }
end
f = ->(x) { x*x + 2*x + 1 }
g = deriv(f)
g.(2)
# => 6.000001000927568
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment