Skip to content

Commit 8194078

Browse files
Implemented :meth:.CoordinateSystem.plot_antiderivative_graph (#2267)
* implement antiderivative * docs * tests * typo * more documentation * Revert "more documentation" This reverts commit 327e4ad. * more documentation * optimized antideriv function * added parameters * tests Co-authored-by: Benjamin Hackl <[email protected]>
1 parent f890d5a commit 8194078

File tree

3 files changed

+56
-1
lines changed

3 files changed

+56
-1
lines changed

manim/mobject/coordinate_systems.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1447,6 +1447,60 @@ def get_derivative_graph(
14471447
) -> ParametricFunction:
14481448
return self.plot_derivative_graph(graph, color, **kwargs)
14491449

1450+
def plot_antiderivative_graph(
1451+
self,
1452+
graph: ParametricFunction,
1453+
y_intercept: float = 0,
1454+
samples: int = 50,
1455+
**kwargs,
1456+
):
1457+
"""Plots an antiderivative graph.
1458+
1459+
Examples
1460+
--------
1461+
.. manim:: AntiderivativeExample
1462+
:save_last_frame:
1463+
1464+
class AntiderivativeExample(Scene):
1465+
def construct(self):
1466+
ax = Axes()
1467+
graph1 = ax.plot(
1468+
lambda x: (x ** 2 - 2) / 3,
1469+
color=RED,
1470+
)
1471+
graph2 = ax.plot_antiderivative_graph(graph1, color=BLUE)
1472+
self.add(ax, graph1, graph2)
1473+
1474+
.. note::
1475+
This graph is plotted from the values of area under the reference graph.
1476+
The result might not be ideal if the reference graph contains uncalculatable
1477+
areas from x=0.
1478+
1479+
Parameters
1480+
----------
1481+
graph
1482+
The graph for which the antiderivative will be found.
1483+
y_intercept
1484+
The y-value at which the graph intercepts the y-axis.
1485+
samples
1486+
The number of points to take the area under the graph.
1487+
**kwargs
1488+
Any valid keyword argument of :class:`~.ParametricFunction`
1489+
1490+
Returns
1491+
-------
1492+
:class:`~.ParametricFunction`
1493+
The curve of the antiderivative.
1494+
"""
1495+
1496+
def antideriv(x):
1497+
x_vals = np.linspace(0, x, samples)
1498+
f_vec = np.vectorize(graph.underlying_function)
1499+
y_vals = f_vec(x_vals)
1500+
return np.trapz(y_vals, x_vals) + y_intercept
1501+
1502+
return self.plot(antideriv, **kwargs)
1503+
14501504
def get_secant_slope_group(
14511505
self,
14521506
x: float,
Binary file not shown.

tests/test_graphical_units/test_axes.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,8 @@ def test_plot_derivative_graph(scene):
7171

7272
curve_1 = ax.plot(lambda x: x ** 2, color=PURPLE_B)
7373
curve_2 = ax.plot_derivative_graph(curve_1)
74-
curves = VGroup(curve_1, curve_2)
74+
curve_3 = ax.plot_antiderivative_graph(curve_1)
75+
curves = VGroup(curve_1, curve_2, curve_3)
7576
scene.add(ax, curves)
7677

7778

0 commit comments

Comments
 (0)