Looking for derivative script

后端 未结 6 647
情话喂你
情话喂你 2021-01-05 20:20

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



        
6条回答
  •  甜味超标
    2021-01-05 20:56

    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)
    

提交回复
热议问题