Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Looking for derivative script

I am desperately looking for a JavaScript that can calculate the first derivative of a function. The function always includes only one variable, x.

e.g. f(x) = x²
  f'(3) = 2x

Consequently, the script should deliver the result 6, since 2*3 = 6.

I hope you know what I mean.

like image 403
enne87 Avatar asked Jun 19 '11 00:06

enne87


People also ask

How do you find derivatives?

Computing DerivativesFind f(x + h). Plug f(x + h), f(x), and h into the limit definition of a derivative. Simplify the difference quotient. Take the limit, as h approaches 0, of the simplified difference quotient.

How do you find the derivative in R?

In R programming, derivative of a function can be computed using deriv() and D() function. It is used to compute derivatives of simple expressions.


2 Answers

function slope (f, x, dx) {
    dx = dx || .0000001;
    return (f(x+dx) - f(x)) / dx;
}

var f = function (x) { return Math.pow(x, 2); }

slope(f, 3)
like image 73
Cristian Sanchez Avatar answered Oct 20 '22 00:10

Cristian Sanchez


Here's a Java library to help do such a thing:

http://jscl-meditor.sourceforge.net/

And another:

http://www.mathtools.net/Java/Mathematics/index.html

You can always use Rhino and import Java classes to use in your JavaScript.

Here's one for JavaScript:

http://code.google.com/p/smath/wiki/smathJavaScriptAPI

like image 24
duffymo Avatar answered Oct 20 '22 00:10

duffymo