33 lines
807 B
Python
33 lines
807 B
Python
# %%
|
|
# Import SymPy for symbolic mathematics
|
|
import sympy as sp
|
|
|
|
# Import display helpers to render LaTeX output in Jupyter/IPython
|
|
from IPython.display import display, Math
|
|
|
|
# Define the symbolic variable x
|
|
x = sp.symbols("x")
|
|
|
|
# List of functions to differentiate
|
|
functions = [
|
|
sp.exp(-x),
|
|
x * sp.exp(x),
|
|
(x + 2) * sp.exp(x),
|
|
sp.exp(-(x**2)),
|
|
(x + 1) * 2**x,
|
|
(x + 1) * 3 ** -(x**2),
|
|
]
|
|
|
|
|
|
# Loop over each function, compute its derivative, and display both
|
|
for func in functions:
|
|
|
|
# Differentiate with respect to x, then simplify the result
|
|
derivativ = sp.simplify(sp.diff(func))
|
|
|
|
# Render f(x) and f'(x) as LaTeX; the double backslash (\\) creates a line break
|
|
display(
|
|
Math(r"f(x) = {} \\ f'(x) = {}".format(sp.latex(func), sp.latex(derivativ)))
|
|
)
|
|
# %%
|