Topics About Contact Resources Quotes

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