Absurdism
|
  |
| Joined: 18 Jul 2013 |
| Total Posts: 2568 |
|
|
| 06 Jan 2014 08:16 PM |
Happy birthday.
local derivative = function(y) return (function(x, dx) dx = dx or 0.1e-5 return (f(x + dx) - f(x))/dx end) end
local tangentLine = function(y, x0) return (function(x) local dy = derivative(y) return dy(x) * x + y(x) - dy(x0) * x0 + y(x0) end) end
f = function(x) return x^2 end g = function(x) return math.cos(x) end
print(derivative(function(x) return math.exp(x) end)(0)) print(derivative(f)(2)) print(derivative(g)(math.pi)) -- quite the fault. print(tangentLine(f, 2)(2))
|
|
|
| Report Abuse |
|
|
maxomega3
|
  |
| Joined: 11 Jun 2010 |
| Total Posts: 10668 |
|
|
| 06 Jan 2014 08:17 PM |
| I get this feeling that I've lost all value as an SH'er as everyone with a problem is more knowledgeable in the field of Lua than I am. |
|
|
| Report Abuse |
|
|
Absurdism
|
  |
| Joined: 18 Jul 2013 |
| Total Posts: 2568 |
|
|
| 06 Jan 2014 08:19 PM |
| 'Tis not a problem. I am sharing my knowledge. It's a pretty efficient and accurate derivative calculator. |
|
|
| Report Abuse |
|
|
StBashkir
|
  |
| Joined: 29 Aug 2008 |
| Total Posts: 26171 |
|
|
| 06 Jan 2014 08:39 PM |
"I get this feeling that I've lost all value as an SH'er as everyone with a problem is more knowledgeable in the field of Lua than I am."
Mmmm... don't confuse knowing Lua with knowing a bit of math. |
|
|
| Report Abuse |
|
|
Absurdism
|
  |
| Joined: 18 Jul 2013 |
| Total Posts: 2568 |
|
|
| 06 Jan 2014 08:40 PM |
A slight variation is using:
local derivative = function(y) return (function(x, dx) dx = dx or 0.1e-5 return (f(x + dx/2) - f(x - dx/2))/dx end) end
Which may provide more accurate results depending on the context. |
|
|
| Report Abuse |
|
|