"""The derivative of x squared in under a minute: the slope question on a plot, a derivation whose changed terms flash, a tangent that rides the curve, and a table that checks slope = 2x. Needs node + mathjax-full (MATHJAX_PATH).""" import os import sys sys.path.insert(0, "plugins/opencanviz/skills/opencanviz-json/scripts") from opencanviz import Project # noqa: E402 BLUE, AMBER, GREEN, PINK = "#38bdf8", "#f59e0b", "#22c55e", "#f472b6" p = Project("Derivative of x squared", aspect="16:9", background="#0f172a", sfx="soft", stage="steady") area = p.area() def sq(x): return x * x # 1. The question, on the curve. s1 = p.scene("Question") t1, body1 = s1.title("How steep is x squared?") plot_r, side = body1.inset(40, 10).split(0.6) pl = s1.plot(plot_r, (-3, 3), (-1, 9), x_ticks=[-3, -2, -1, 0, 1, 2, 3], y_ticks=[0, 2, 4, 6, 8], grid=True, x_label="x", y_label="y") curve = pl.curve(sq, color=BLUE) dot = pl.point(1, 1, size=30, color=AMBER) tan = pl.tangent(sq, 1, length=340, color=AMBER) q_r, ask_r = side.inset(20, 40).cut_top(side.h * 0.45) q = s1.math(r"y = x^2", at=q_r, size=120) ask = s1.text("slope = ?", at=ask_r.cut_top(120)[0], fontSize=72, fontWeight=700, color=AMBER) s1.step("Here is y equals x squared.").reveal(t1).reveal(pl.node).reveal(curve, style="draw") \ .reveal(q, style="wipe", offset="with") s1.step("Pick a point on the curve.").pop([dot]) s1.step("How steep is the curve right here?").reveal(ask, style="rise").reveal(tan, style="zoom", offset=400) # 2. The derivation: each changed term flashes when it changes. s2 = p.scene("Derivation") title, body = s2.title("Slope as a limit") lines = s2.derivation([[r"f'(x) =", r"\lim_{h \to 0} \frac{(x+h)^2 - x^2}{h}"], [r"=", r"\lim_{h \to 0} \frac{\part{a}{x^2} + 2xh + h^2 - \part{b}{x^2}}{h}"], [r"=", r"\lim_{h \to 0} (2x + h)"], [r"=", r"2x"]], at=body.inset(60, 0), size=76, gap=30, align="eq") s2.step("The slope is rise over run, as the run h shrinks to zero.").reveal(title) \ .stagger(lines[0], style="fade", gap=400) s2.step("Expand the square. The x squared terms cancel.").stagger(lines[1], style="fade", gap=300) \ .emphasize(lines[1][1], style="flash", part=["a", "b"], color=AMBER, offset=1000) s2.step("What is left divides by h.").stagger(lines[2], style="fade", gap=300) \ .emphasize(lines[2][1], style="flash", color=AMBER, offset=900) s2.step("Let h go to zero. Two x is left.").stagger(lines[3], style="fade", gap=300) \ .emphasize(lines[3][1], style="flash", color=GREEN, offset=900) # 3. Back to the curve: the tangent rides it and its slope tracks 2x. s3 = p.scene("Tangent") plot3, side3 = area.inset(40, 20).split(0.6) pl3 = s3.plot(plot3, (-3, 3), (-1, 9), x_ticks=[-3, -2, -1, 0, 1, 2, 3], y_ticks=[0, 2, 4, 6, 8], grid=True, x_label="x", y_label="y") curve3 = pl3.curve(sq, color=BLUE) dot3 = pl3.point(-2, 4, size=30, color=AMBER) tan3 = pl3.tangent(sq, -2, length=340, color=AMBER) fx_r, rest3 = side3.inset(30, 40).cut_top(side3.h * 0.34) key_r, rule_r = rest3.cut_top(rest3.h * 0.4) fx3 = s3.math(r"f(x) = x^2", at=fx_r, size=110) key = pl3.legend([("the curve", BLUE), ("its tangent", AMBER)], at=key_r.inset(60, 20), font=44) rule = s3.math(r"f'(x) = 2x", at=rule_r, size=110, color=GREEN) s3.step("Now watch the tangent as the point slides.").reveal(pl3.node).reveal(curve3, offset="with") \ .reveal(key, offset="with").reveal(fx3, style="wipe", offset="with").pop([dot3]).reveal(tan3, style="zoom") s3.step("On the left it falls. At zero it is flat. On the right it climbs.") \ .ride(dot3, pl3, sq, -2, 2, duration=3600, tangent=tan3) s3.step("Its slope is always two x.").reveal(rule, style="wipe").emphasize(rule, style="pulse", offset=900) # 4. Check a few points. s4 = p.scene("Check") title4, body4 = s4.title("Check a few points") tbl = s4.table([["x", "-1", "0", "1", "2", "3"], ["slope", "-2", "0", "2", "4", "6"]], at=body4.inset(160, 120)) s4.step("Check a few points.").reveal(title4).stagger(tbl.column(0), style="fade") st = s4.step("Every slope is twice its x.") for c in range(1, 6): st.stagger(tbl.column(c), style="pop", gap=120, offset=c * 350 - 350) st.tint([tbl.cells[1][3]], GREEN, offset=2200) s4.step("The derivative of x squared is two x.").emphasize(tbl.cells[1][1:], style="pulse") p.save(os.path.join(os.path.dirname(os.path.abspath(__file__)), "derivative_x_squared.json"))