Topic

Calculus pictures

Tangent lines, shaded areas, Riemann sums, Taylor polynomials, gradient fields and level curves. Each function returns layers to draw together with the numbers behind the picture: exact where they are rational, certified enclosures otherwise.

Tangents and secants

tangent(f, x0) gives the slope, the point of contact, the line and its equation. Where ff and f′f' are rational at x0x_0 the answer is exact; otherwise slope and value are RationalIntervals from @ratmath/analytic that provably hold the true numbers.

const t = tangent("x^3 - x", 1);
t.equation;            // "y = 2x - 2", exact
tangent("sin(x)", 1).slope;   // an interval around cos(1), 12 digits wide
secant("x^2", 0, 2).slope;    // 2
drawScene(ctx, vp, [graph("x^3 - x", {}, vp).exact(), ...t.layers], { axes: true });

Areas and integrals

areaUnder(f, [a, b], params, vp) and areaBetween(f, g, [a, b], params, vp) are Refinements. The quick stage shades the region and integrates in floats. The exact stage returns the same shading with integral exact (for polynomials and other rational cases) or a certified interval. Parts where ff is below gg are shaded in a second colour and count negative.

An end may be a constant such as "pi/2", or "inf" and "-inf". The integral is then improper: improper is true, and integral is Infinity when it is proven to diverge.

areaUnder("x^2", [0, 3], {}, vp).exact().integral;         // 9, a Rational
areaUnder("sin(x)", [0, "pi"], {}, vp).exact().integral;   // an interval around 2
areaUnder("1/x^2", [1, "inf"], {}, vp).exact().integral;   // an interval around 1
areaUnder("1/x", [1, "inf"], {}, vp).exact().integral;     // Infinity

riemannSum(f, [a, b], n, params, { rule }) draws the rectangles (or trapezoids) and adds them up. The sum is an exact Rational when ff is rational at the sample points. Rules: left, right, midpoint, trapezoid, lower, upper.

Taylor polynomials

taylorPolynomial(f, c, n) returns the coefficients of (x−c)k(x - c)^k, exact or enclosed, and an expression that graph() draws next to the function.

Gradients, fields and level curves

vectorField([P, Q], params, vp) and gradientField(F, params, vp) return a frame of arrows. gradientAt(F, [x, y]) gives the certified gradient at a point with an arrow and the line tangent to the level curve there. contours(F, levels, params, vp) returns one implicit curve per level.

const g = gradientAt("x^2 + y^2", [1, 2]);
g.gradient.map(String);     // ["2", "4"], exact
drawScene(ctx, vp, [gradientField("x^2 + y^2", {}, vp), ...contours("x^2 + y^2", [1, 4, 9], {}, vp).map((c) => c.curve.exact()), ...g.layers]);

How this connects to analytic

The numbers come from @ratmath/analytic: derivatives by automatic differentiation, integrals by Taylor models, improper integrals by its tail bounds. Anything that library returns with an .ast (a function made with fn(), a derivative, a Taylor polynomial) can be passed straight to graph(), tangent() and the rest. Its special functions draw too: gamma(x), lgamma(x), digamma(x), besselJ(n, x), Si(x), fresnelS(x) and fresnelC(x) work in graph() with certified pixels.

Try itEdit the code and run it with @ratmath/graphics and Core's Rational in scope. The last line's value is shown; PRINT(…) adds lines, TEX(…) typesets math, and SHOW(svg) draws a picture.
Ctrl/⌘ + Enter runs