Skip to content

Instantly share code, notes, and snippets.

@lutzky
Created July 23, 2008 05:02
Show Gist options
  • Save lutzky/1534 to your computer and use it in GitHub Desktop.
Save lutzky/1534 to your computer and use it in GitHub Desktop.
Epsilon = 1e-10
def d_dx(&f) # :yields: x
Proc.new do |x|
(f[x + Epsilon] - f[x])/Epsilon
end
end
require 'derivative'
if __FILE__ == $0
puts "Derivation example: g is the derivative of x**2"
g = d_dx { |x| x**2 }
puts "g[8] is #{g[8]}"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment