Find the Area Under a Curve Using Javascript
Why Is Finding the Area Under a Curve Difficult
Finding the area under a curve is a difficult problem. Fortunately, with a little code we can see how it works.
function areaUnderCurve(func, start, end, delta){
let area = 0;
do
{
area += Math.abs(func(start)) * delta;
//func finds the height of the rect & delta is the width
// we use abs because a negative area doesn't make sense
start += delta; //move forward by the width of the rect
}
while (start <= end); //go until we reach the end
return area;
}
console.log(areaUnderCurve(Math.cos, 0, 3, .001)) //1.85;
console.log(areaUnderCurve(Math.sin, 0, 3, .001)); //1.99;
Resources
- Elements of Programming
- Beginning Math and Physics for Game Programmers
- From Mathematics to Generic Programming
- Data-oriented design: software engineering for limited resources and short schedules
- Design of Design, The: Essays from a Computer Scientist
- Mythical Man-Month, The: Essays on Software Engineering, Anniversary Edition
- More Effective C++: 35 New Ways to Improve Your Programs and Designs
- Mathematics for Machine Learning
- The Pragmatic Programmer: Your Journey To Mastery, 20th Anniversary Edition (2nd Edition)
- Schaum's Outline of Essential Computer Mathematics 1st Edition
- All recommended resources