Instantly share code, notes, and snippets.

# jgphilpott/calculus.coffee

Last active November 19, 2022 08:24
Show Gist options
• Save jgphilpott/4276345a5b7c96fc010afa28cc5d38b6 to your computer and use it in GitHub Desktop.
A collection of functions for performing differential and integral calculus in JavaScript.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
 # Find the tangent of any polynomial function at x. findTangent = (x, coefficients) -> slope = 0 intercept = coefficients[0] for index in [1 ... coefficients.length] slope += coefficients[index] * index * Math.pow x, index - 1 intercept += coefficients[index] * Math.pow x, index return [intercept - slope * x, slope] # Find the area under any polynomial function within bounds. # This function works assuming your curve is either entirely above or entirely below the x axis. # If your curve crosses the x axis within the given bounds you will need to first find the roots (x intercepts) and then calculate the segments above and below the axis separately. # For finding the roots of polynomials look here: https://gist.github.com/jgphilpott/e483b5fbe52a7233c292f35737e5a682 findArea = (lowerLimit, upperLimit, coefficients) -> lower = 0 upper = 0 for coefficient, index in coefficients lower += (coefficient * Math.pow lowerLimit, index + 1) / (index + 1) upper += (coefficient * Math.pow upperLimit, index + 1) / (index + 1) return Math.abs upper - lower
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
 // Generated by CoffeeScript 2.7.0 var findArea, findTangent; // Find the tangent of any polynomial function at x. findTangent = function(x, coefficients) { var i, index, intercept, ref, slope; slope = 0; intercept = coefficients[0]; for (index = i = 1, ref = coefficients.length; (1 <= ref ? i < ref : i > ref); index = 1 <= ref ? ++i : --i) { slope += coefficients[index] * index * Math.pow(x, index - 1); intercept += coefficients[index] * Math.pow(x, index); } return [intercept - slope * x, slope]; }; // Find the area under any polynomial function within bounds. // This function works assuming your curve is either entirely above or entirely below the x axis. // If your curve crosses the x axis within the given bounds you will need to first find the roots (x intercepts) and then calculate the segments above and below the axis separately. // For finding the roots of polynomials look here: https://gist.github.com/jgphilpott/e483b5fbe52a7233c292f35737e5a682 findArea = function(lowerLimit, upperLimit, coefficients) { var coefficient, i, index, len, lower, upper; lower = 0; upper = 0; for (index = i = 0, len = coefficients.length; i < len; index = ++i) { coefficient = coefficients[index]; lower += (coefficient * Math.pow(lowerLimit, index + 1)) / (index + 1); upper += (coefficient * Math.pow(upperLimit, index + 1)) / (index + 1); } return Math.abs(upper - lower); };
to join this conversation on GitHub. Already have an account? Sign in to comment