# Matplotlib compatibility patch for Pyodide
import matplotlib
if not hasattr(matplotlib.RcParams, "_get"):
matplotlib.RcParams._get = dict.get
Exercises on Taylor expansion#
Note
This page might not work well in dark mode. If you cannot see the equations in the exercises, please toggle light mode.
Use this page to practice calculating Taylor expansions. Each exercise is based on a different family of functions. You can choose from a set of coefficients and expansion points for each exercise (use the dropdown menu).
Tip
To provide an automatic check of your answer, this page uses Python in the background. It should load automatically, but it can take a few seconds until your see the equations. If it does not load automatically, you can click rocket –>Live Code.
Work on the exercises and check your answers as follows:
You should work out the exercises “by hand” first (using paper or a digital notebook)
You can type your answer in the code cell and then click “Check Answer” to see if it is correct. Use basic Python syntax for representing the equation, for example,
x**2 + 1for \(x^2+1\); usepiorsin()andcos()for the trigonometric functions (not numpy!)You can click the “Show correct answer” button to see the solution; however, note that the form of the equation will be different than that which you are expected to derive, so you will need to do some arithmetic to confirm you are correct.
The reason the Python syntax is different is because it uses Sympy, a library for symbolic mathematics.
import sympy as sp
import numpy as np
import ipywidgets as widgets
from IPython.display import display, clear_output, Markdown, HTML
sp.init_printing()
# Checker function
def check_equation(eq1, eq2):
return sp.simplify(eq1 - eq2.removeO()) == 0
def check_answer_from_widget(answer_widget, expected, comparison=check_equation):
output = widgets.Output()
correct_output = widgets.Output()
button = widgets.Button(description="Check answer")
show_correct_button = widgets.Button(description="Show correct answer")
def _inner_check(button):
with output:
output.clear_output()
try:
user_expr = sp.sympify(answer_widget.value)
if comparison(user_expr, expected):
display(Markdown("✅ Correct!"))
correct_output.clear_output()
else:
display(Markdown("❌ Incorrect!"))
except Exception as e:
display(
Markdown(
"⚠️ Error parsing your input. Are you using correct syntax? "
f"You entered the following code: `{answer_widget.value}`"
)
)
def _show_correct_answer(button):
with correct_output:
correct_output.clear_output()
display(Markdown("<br>Correct answer (Python notation):<br><br>"))
print(expected)
display(Markdown("<br>Mathematical notation:<br><br>"))
display(expected)
button.on_click(_inner_check)
show_correct_button.on_click(_show_correct_answer)
display(button, output, show_correct_button, correct_output)
# Generalized exercise widget creator
def create_taylor_exercise(expr_generator, n_exercises, rng, description="Exercise"):
x = sp.symbols("x")
# Precompute exercises and expansions
solutions = {}
for idx in range(n_exercises):
eq_original, eq_correct, expansion_point = expr_generator(idx, rng, x)
solutions[idx] = (eq_original, eq_correct, expansion_point)
# Widgets
selector = widgets.Dropdown(
options=np.arange(n_exercises), value=0, description=description
)
out = widgets.Output()
answer_box = widgets.Textarea(
placeholder="Enter your answer here, e.g. 2*x**2 + 3*x",
description="Your answer:",
layout=widgets.Layout(width="80%", height="80px"),
)
def show_exercise(change=None):
with out:
clear_output(wait=True)
idx = selector.value
eq_original, eq_correct, expansion_point = solutions[idx]
display(HTML("<br>Calculate the Taylor series expansion of <br>"))
display(eq_original)
display(HTML("<br>around<br>"))
display(sp.Eq(x, expansion_point))
display(HTML(r"<br>Discard any $\mathcal{O}(\Delta x^3)$ terms.<br><br>"))
# Reset answer box and display
answer_box.value = ""
display(answer_box)
check_answer_from_widget(answer_box, eq_correct)
selector.observe(show_exercise, names="value")
display(selector, out)
show_exercise()
Exercise 1#
# Exercise set 1: quadratic functions
n_exercises = 4
rng = np.random.default_rng(12345)
def expr_gen_quadratic(idx, rng, x):
random_a1 = rng.integers(2, 6, size=(n_exercises,))
random_b1 = rng.integers(-10, 10, size=(n_exercises,))
random_c1 = rng.integers(-5, 5, size=(n_exercises,))
a_1 = sp.Integer(random_a1[idx])
b_1 = sp.Integer(random_b1[idx])
c_1 = random_c1[idx]
eq_original = a_1 * x**2 + b_1 * x
eq_correct = sp.series(eq_original, x, c_1, 3).removeO()
return eq_original, eq_correct, c_1
create_taylor_exercise(expr_gen_quadratic, n_exercises, rng, description="Exercise Set")
Tip
Note that for Exercise 1, the equation is an even-order polynomial, which means that the Taylor Series should not introduce any error!
Exercise 2#
Tip
In your answer, you can write pi for \(\pi\).
# Exercise set 2: tangent functions
def expr_gen_tangent(idx, rng, x):
random_a2 = rng.integers(1, 7, size=(n_exercises,))
random_c2 = rng.integers(-5, 5, size=(n_exercises,))
a_2 = sp.Integer(random_a2[idx])
expansion_point = sp.Integer(random_c2[idx]) * sp.pi
eq_original = a_2 * sp.tan(x)
eq_correct = sp.series(eq_original, x, expansion_point, 3).removeO()
return eq_original, eq_correct, expansion_point
create_taylor_exercise(expr_gen_tangent, n_exercises, rng, description="Exercise Set")
Exercise 3#
# Exercise set 3: rational functions
def expr_gen_rational(idx, rng, x):
random_a3 = rng.integers(1, 10, size=(n_exercises,))
random_c3 = rng.integers(-1, 1, size=(n_exercises,))
a_3 = sp.Integer(random_a3[idx])
c_3 = random_c3[idx]
eq_original = a_3 / (1 - x)
eq_correct = sp.series(eq_original, x, c_3, 3).removeO()
return eq_original, eq_correct, c_3
create_taylor_exercise(expr_gen_rational, n_exercises, rng, description="Exercise Set")
Attribution
This chapter is written by Jaime Arriaga Garcia, Justin Pittman and Robert Lanzafame. Find out more here.