A derivative from first principles

Derive the derivative of x² from the limit definition as a derivation that writes itself, line by line. One formula holds all five rows, Morph blocks grow each row out of the one above, and the result lights up at the end.

What you will make

A title, f(x) = x², and a derivation of five rows. The first row is the definition of the derivative, applied to x². The next three appear one after another, each copied out of the row above: the square is expanded, the terms that cancel are left behind, and the common h is divided out. The last row is the answer, 2x, and it swells in a flash of colour. Every h is gold, so the eye can follow the variable that goes to zero.

You will use a Morph block four times, an Appear block twice, two more blocks for the finish, and the pair editor to link glyphs by groups. It is a long tutorial: the steps are the same for each row, so the first block is the one to take slowly.

Before you start

The finished project is below. To compare as you go, download it, choose File ▸ Open…, press Other file… and pick the file (Projects explains the dialog).

Step by step

1. Write the derivation

Press + New in the Creator and give the formula the name derivation. In the Raw LaTeX field type the five rows, then press Apply. Manim sets every formula in an aligned environment, so the & before each = lines the equals signs up and \\ ends a row:

f'(x) &= \lim_{h \to 0} \frac{(x+h)^2 - x^2}{h} \\
  &= \lim_{h \to 0} \frac{x^2 + 2xh + h^2 - x^2}{h} \\
  &= \lim_{h \to 0} \frac{2xh + h^2}{h} \\
  &= \lim_{h \to 0} (2x + h) \\
  &= 2x
The formula editor with the five-row source in the Raw LaTeX field and the five rows drawn beside it.
The formula editor after Apply: the source on the left and the five rows rendered beside it.

Choose FadeIn in the Appears with menu: new clips of this formula start with that intro. Then add a second formula, title, with the source f(x) = x^2, and in its Size row type 72 in the second box, the one that shows Manim's font_size.

2. Put them on the timeline

Right-click each formula and choose Add to the timeline, derivation first. A new clip goes on the top track, Track 2, at the playhead, and a track holds one clip at a time: the second clip lands on the same track, right after the first. So open the Timing tab of the title clip and choose Track 1 in its Track list first: Start and End stop at the neighbour on the same track. Then set the times and the place of both clips:

ClipTrackStartEndInitial stateIntro · Outro
titleTrack 1021.4X −4.6 · Y 3.3Write 1 s · FadeOut 1 s
derivationTrack 2121.4X 0 · Y −0.2 · Scale 1.2FadeIn 1 s · FadeOut 1 s

A dropped clip is three seconds long, so the End field (or the right edge of the clip) is the first thing to change: the blocks below need room until about 18 seconds. The finished project calls its two tracks Derivation and Title; the names of a new project's tracks do not matter.

3. The first Morph block: row 1 into row 2

Put the playhead at 3.6 s, right-click the derivation clip and choose Morph within the formula. With the block selected, press Edit pairs… in the Animations panel. The editor draws the whole derivation twice, side by side: what can be a source at the left, what can arrive at the right. You link a glyph of row 1 at the left to a glyph of row 2 at the right. (The playhead does not have to be exactly on 3.6 s: the Start field in the block's panel sets the time.)

Suggest pairs by label is not the tool here. On this block it proposes 33 pairs at once, one for nearly every glyph that has a twin, and it picks the nearest twin in reading order. This derivation repeats its symbols (x, h, 2, brackets), so that twin is not always the glyph the algebra means. Link by groups instead. Drag a box over a run of glyphs at the left, another over the matching run at the right, and press Link: one pair for the whole run. For a glyph that goes to two places, click it, then Shift-click both targets and press Link.

The first row of the derivation in the pair editor, with a box being dragged over the glyphs = lim h→0.
A box dragged over = lim h→0 in the left copy selects those seven glyphs. Drag a box over the same glyphs of row 2 in the right copy, then press Link: one pair for the whole run.

Seven links make the block. The first is the box you just saw.

Sources (row 1)Targets (row 2)How
= and lim h→0, seven glyphsthe same sevena box on each side
the x of (x + h)the x of x² and the x of 2xhclick, then Shift-click both
the exponent of (x + h)²the exponents of x² and h²click, then Shift-click both
the + of (x + h)both + of x² + 2xh + h²click, then Shift-click both
the h of (x + h)the h of 2xh and the h of h²click, then Shift-click both
− x², three glyphsthe same threea box on each side
the h under the bar, and the barthe same twoa box on each side

The f′(x) and the brackets of (x + h) are not linked: they stay in row 1 and do not appear in row 2. Press Save pairs.

The pair editor of the first block in the finished project: the derivation drawn twice, one copy over the other, with seven coloured links from row 1 of the upper copy to row 2 of the lower one.
The pair editor of the first block, saved, as the finished project opens it: the two copies one over the other this time, since the drawing takes the layout that shows the glyphs that can take part larger. Each group is one link; the badge at its junction gives its number and the initial of its style (I: when the pair takes the block's).

4. The glyph no pair brings

Row 2 has one glyph that comes from nothing: the 2 of 2xh, the coefficient the expansion creates. Put the playhead at 4.9 s, right-click the clip and choose Appear ▸ Write. In the panel of the new block pick that glyph in the glyph view (it is the 2 in front of the xh of the second row). Set its duration to 0.6 s.

5. The other rows

The same two moves, three more times. Add each block where the table says; link with boxes and Shift-clicks as before. In the editor of a later block the glyphs of the rows already on screen are dimmed as targets and the later rows are free: that is how you know you are on the right row.

The pair editor of the second block, side by side: row 2 of the copy at the left linked to row 3 of the copy at the right with three coloured arcs; rows 1 and 2 are dimmed in the copy at the right.
The second block: three links from row 2 to row 3. The terms that cancel stay where they are, unlinked.
StartBlockLinks (sources → targets)
7.2 sMorph, row 2 into row 3= lim h→0 → the same (box on each side) · the numerator 2xh + h² (six glyphs) → the numerator of row 3 · the h under the bar and the bar → the same
10.2 sMorph, row 3 into row 4= lim h→0 → the same · 2x of the numerator → the 2x of row 4 · + h of the numerator → the + h of row 4
11.6 sAppear ▸ FadeIn, 0.6 sthe two brackets of row 4
14 sMorph, row 4 into row 5the = → the = · 2x in the brackets → the 2x of row 5

Look at what stays unlinked. In row 2 the terms x² and − x² cancel, so they are not carried into row 3. In row 3 the h of 2xh and the h under the bar cancel, so row 4 starts from 2x + h. The derivation shows the algebra by what it leaves behind.

6. Colour the h

In the formula editor, in the Glyphs view on the right, pick the thirteen h of the derivation (Ctrl-click each, or drag a box over several) and give them one colour; the project uses gold (#F4C867). A colour belongs to a glyph, and Morph copies keep it, so every row inherits it. Match colors does the same job in one step for every h at once: see Match colors.

7. The finish

The answer should arrive with some noise. At 16 s add a Transform ▸ Part of the formula block, pick the 2x of the last row, set its Scale to 1.3, tick Set colour and choose a teal. At 17.2 s add an Emphasis ▸ Indicate block on the same two glyphs. Indicate swells them and turns them yellow for a moment, then lets them go back.

The timeline of the derivation clip with eight blocks: Morph, Write, Morph, Morph, FadeIn, Morph, Transform and Indicate.
The clip with its eight blocks: four Morph blocks, a Write and a FadeIn for the glyphs no pair brings, a Transform and an Indicate for the answer.

8. Play it and export

Press Space and watch the derivation write itself. Fix timings by dragging blocks, as in the timeline page. When you are happy, Export ▸ Python — ManimCE… or Export ▸ Python — ManimGL… writes the scene (see Exporting).

The result

This is the scene as ManimCE renders it from the Python the editor exports, at 480p:

And the exported Python, for both libraries. Each Morph block is one self.play(…) with an animation for each pair; a pair of groups is one ReplacementTransform of copies of the glyphs. The first block starts like this:

        self.play(
            ReplacementTransform(VGroup(*derivation_g[5:12]).copy(), VGroup(*derivation_g[23:30])),
            ReplacementTransform(VGroup(derivation_g[13]).copy(), VGroup(derivation_g[30], derivation_g[34])),
            …
            run_time=1.2,
        )

The helper at the top of the file matches the editor's glyphs to Manim's by position.

Where to go next