JavaScript calculus

Archimedes
Calculus, the old-fashioned way

Zeno and Archimedes are having a drink when they see an attractive lady across the bar. Archimedes gets up to go talk to her. Zeno says, “Why bother? You will first need to cover half the distance, and then half the remaining distance, and so on, so you will never get there.” Archimedes says, “Maybe not, but I’ll get close enough.”

As a student, it is easy to get the impression that calculus is a magnificent system of integrals and derivatives that someone somewhere has validated with rigorous limit proofs. In the real world, not all datasets are able to be represented by elementary functions, and some seemingly-straightforward problems (such as the circumference of an ellipse !) have no elementary solution. So, mathematical purists notwithstanding, calculus must sometimes be performed with infinitesimals and brute-force numeric methods. (Plus, infinitesimals are a lot easier to understand! Plus, rigorous or not, they work! Must side with Leibniz here.)

x-1y-1x0y0x1y1

To approximate the tangent to a curve at a point (the derivative), find the slope of the line that connects closely-spaced points on either side (the symmetric difference quotient):

Powered by MathJax

m=(y1y1)/(x1x1)

The tangent line:

y=m(xx0)+y0

x1-x0y0y1

To approximate the area under a curve (the integral), partition it into narrow trapezoids and find the sum of the areas (the method of exhaustion).

The area of each trapezoid is the area of the rectangle plus the area of the triangle on top.
It is equivalent to the width × the average of the heights:

A(x1x0)((y1+y0)/2)

The arc length is approximated by the diagonal:

s(x1x0)2+(y1y0)2

The narrower the trapezoids, the closer the approximation.

r0r1α

To approximate area using polar coordinates, partition the figure into narrow triangles.

If the incremental angle θ1θ0=α is very small, the triangles may be treated as if they were right triangles with both hypotenuse and long side of length r, and short side of length rsinα.

The area of each right triangle would be approximately (rrsinα)/2.

The area of the segment of the figure may be more closely approximated by averaging the areas of the r0 and r1 right triangles, as with the trapezoids above; for very small α,

A(r0r1sinα)/2

The arc length is approximated by the r0r1 diagonal:

s(r1r0)2+r0r1(sinα)2

or, in Cartesian form:

s(r1cosθ1r0cosθ0)2+(r1sinθ1r0sinθ0)2

The narrower the angle, the closer the approximation.

The above may be applied to three-dimensional figures as well, by summing the areas and volumes of infinitesimal strips and slices.


To test the accuracy of the above, we can apply it to figures whose math is well-known.

x0y0x1

Begin with a parabola, y=x2.

Let x0= 0.4.  The slope of the tangent at x0 is the derivative

ddxx2=2x0= 0.8

Using an x increment of 0.000001, the slope of the line connecting points on either side is 0.800000000003.

Let x1= 1.1.  The area under the curve from 0 to x1 is the integral

0x1x2dx=x133= 0.443667

The calculated area by trapezoids is 0.443668.

r0θ0

Now for an ellipse, where a=16, b=5, c=a2b2, and e=ca.
When the origin is at one focus,  r=a(1e2)1ecosθ

Let θ0 = 5°. The slope of the tangent at θ0 is

b2(xc)a2y= -0.5309683222

Using an angle increment of 0.000001 radians, the slope of the line connecting points on either side is -0.5309683217.

The area of an ellipse is πab= 251.327412
The calculated area by triangles is 251.327413.

The circumference of an ellipse has no elementary solution, but may be calculated to any degree of precision by the following formula:

C=π(a+b)[1+n=1((2n1)!!2nn!)2hn(2n1)2], where h=(ab)2/(a+b)2

Taken to greater than 15 significant figures, the circumference of the ellipse is 70.5824241.
The calculated circumference by r0r1 segments is 70.5824236.

Note that infinitesimal methods may be used to find the tangent to any ellipse, such as one rotated at an arbitrary angle; and they may be used to find any segment of area or circumference.

Now for something a lot more practical. What is the area of the shaded slice of pizza?

The pizza radius is 1 and the slice dimensions are 13 × 13.

The equation for the top half of the pizza is y=1x2.

Using an x increment of 0.000001, the calculated area by trapezoids is 0.06404, or 58 % of a full slice.

The length of the crust by r0r1 segments is 0.38989.

728292102112

Starting the year in which the owner turns 72, retirement accounts must make an annual required minimum distribution based on life expectancy tables published by the Internal Revenue Service. The graph at right shows what happens to a $100,000 account with the specified growth rate as RMD’s are withdrawn in monthly installments.

growth rate: 4.0 %

age: 84 |  month: February

The rate of change in February the year the owner turns 84 is $ -1,852 per year.
The area under the curve (total value × time) is $ 2,489,786.


Please visit russellcottrell.com | blog