Exercises on Taylor expansion#
Use this page to practice calculating Taylor expansions. This requires
Tip
This page presents several functions of \(x\) that you should approximate with Taylor Series. You can click rocket
–>Live Code
to check your answers, however, note the following:
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 + 1
for \(x^2+1\); usepi
orsin()
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
from sympy import pi, latex
#from sympy.printing.mathml import mathml
import operator
import ipywidgets as widgets
from IPython.display import Latex, Math
sp.init_printing()
#sp.init_printing(use_latex=True)
check_equation = lambda eq1, eq2: sp.simplify(eq1 - eq2) == 0
def check_answer(variable_name, expected, comparison=operator.eq):
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:
if comparison(globals()[variable_name], expected):
output.outputs = [{'name': 'stdout', 'text': 'Correct!',
'output_type': 'stream'}]
correct_output.clear_output() # Clear the correct answer display if they got it right
else:
output.outputs = [{'name': 'stdout', 'text': 'Incorrect!',
'output_type': 'stream'}]
def _show_correct_answer(button):
with correct_output:
correct_output.clear_output() # Clear previous outputs
print(f"The correct answer is in python notation: {expected}")
print(f"Or in mathematical notation:")
display(expected)
button.on_click(_inner_check)
show_correct_button.on_click(_show_correct_answer)
display(button, output, show_correct_button, correct_output)
Exercise 1#
Calculate the taylor series expansion of:
x, y = sp.symbols('x, y')
a_1 = sp.Integer(np.random.randint(2,6))
b_1 = sp.Integer(np.random.randint(-10,10))
c_1 = sp.Integer(np.random.randint(-5,5))
eq1_original = a_1 * x**2 + b_1*x
eq1_correct = sp.series(eq1_original,x,c_1)
eq1_answer = 0
display(eq1_original)
#display(eq1_correct)
around:
display(sp.Eq(x,c_1))
Discard any \(O(x^3)\) terms.
Fill in your answer and run the cell before clicking ‘Check answer’.
eq1_answer =
check_answer("eq1_answer",eq1_correct, check_equation)
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#
Calculate the taylor series expension of:
a_2 = sp.Integer(np.random.randint(1,7))
c_2 = sp.Integer(np.random.randint(-5,5))
eq2_original = a_2*sp.tan(x)
display(eq2_original)
eq2_correct = sp.series(eq2_original,x,c_2*sp.pi,3).removeO()
#display(eq2_correct)
eq2_answer = 0
around:
display(sp.Eq(x,c_2*sp.pi))
discard any \(O(x^3)\) terms.
Fill in your answer and run the cell before clicking ‘Check answer’. Furthermore, use pi
for \(\pi\):
eq2_answer =
check_answer("eq2_answer",eq2_correct, check_equation)
Exercise 3#
Calculate the taylor series expension of:
a_3 = sp.Integer(np.random.randint(1,10))
c_3 = sp.Integer(np.random.randint(-1,1))
eq3_original = a_3 / (1 - x)
display(eq3_original)
eq3_correct = sp.series(eq3_original,x,c_3,3).removeO()
#display(eq3_correct)
eq3_answer = 0
around:
display(sp.Eq(x,c_3))
discard any \(O(x^3)\) terms.
Fill in your answer and run the cell before clicking ‘Check answer’:
eq3_answer =
check_answer("eq3_answer",eq3_correct, check_equation)