A Taylor series expands any smooth function as an infinite sum of polynomial terms centred at a point. The Maclaurin series (centred at zero) for sin(x) is x−x³/3!+x&sup5;/5!−… Each additional term widens the region where the polynomial faithfully tracks the true curve — the orange line chases the blue one outward as N grows.
Choose a function (sin, cos or e⊃x), then drag the Terms (N) slider to add polynomial terms one by one. Press Animate to watch the approximation step through N=0 to your chosen maximum, revealing how each term improves accuracy. The Max error stat shows the largest deviation across the visible window.
Brook Taylor published the series in 1715, but James Gregory had used special cases decades earlier. The exponential e⊃x has the simplest Taylor series — every coefficient is 1/k! — because e⊃x is its own derivative. Both sin and cos converge globally but alternate in sign, causing the oscillatory overshoot visible at small N far from the origin.
This simulation computes each Maclaurin coefficient directly from the closed-form pattern for sin, cos, and ex rather than differentiating symbolically: taylorCoeff() returns (−1)(k−1)/2/k! on odd k for sin (zero on even k), (−1)k/2/k! on even k for cos (zero on odd k), and 1/k! for every k for ex, with factorials cached recursively in a lookup array. evalTaylor() sums these coefficients times xk from k=0 up to the Terms (N) slider's value. Every animation frame samples 700 points across the X window slider's range, plots the exact function in blue and the truncated polynomial in orange, and reports whichever pointwise gap between the two curves is largest as the Max error stat.
What it shows
The blue curve is the true function evaluated with Math.sin/Math.cos/Math.exp; the orange curve is the truncated Taylor polynomial TN(x) = Σ ckxk for k = 0…N, built from the same closed-form coefficients used by taylorCoeff(). Wherever orange visibly separates from blue, the polynomial has left its region of good approximation for the current N and X window.
How to use it
Pick sin, cos, or ex, then drag Terms (N) from 1 to 20 to add polynomial terms one at a time, and widen X window to make divergence appear sooner. Animate steps animN back and forth between 0 and N every 0.7 seconds so you can watch each term's contribution build and recede; Reset stops the animation and returns animN to 0.
Did you know?
Because the factorial in the denominator grows faster than any fixed power of x, all three series converge for every real x — there is no finite radius of convergence to worry about, unlike a series such as 1/(1−x). That is why ex's series, with every coefficient exactly 1/k!, is the simplest possible: it needs no sign alternation or zero-term skipping at all.
taylorCoeff() returns 0 for even k when the function is sin, and 0 for odd k when the function is cos. That mirrors the real Maclaurin expansions: sin's derivatives at 0 cycle 0, 1, 0, −1 so only odd powers survive, while cos's cycle 1, 0, −1, 0 so only even powers survive.
The draw loop checks isFinite(ppyj) and whether the plotted y-value falls outside twice the canvas height. When the polynomial's value overflows at high powers of x, the code breaks the line segment (resets firstPt) instead of drawing a wild spike across the chart.
Every frame, evalTaylor and evalTrue are compared at 700 sample points spanning the current X window, and the largest absolute difference is kept. It is a numerical estimate of the worst-case gap over the visible range, so it depends on both N and how wide the X window slider is set.
displayN equals animN while animating, and animN increases or decreases by 1 every 0.7 seconds between 0 and the Terms (N) slider value, reversing direction whenever it hits either bound. It is a back-and-forth ping-pong sweep, not a one-shot count-up.
The derivative of ex is ex at every order, so every Maclaurin coefficient is simply 1/k! with no sign changes. sin and cos cycle through four repeating derivatives (sin, cos, −sin, −cos), which forces the alternating (−1)k sign seen in taylorCoeff().