Skip to content
Snippets Groups Projects
Commit fb216d4b authored by Robert Lanzafame's avatar Robert Lanzafame
Browse files

update taylor exercise

parent 1452892e
No related branches found
No related tags found
No related merge requests found
%% Cell type:markdown id: tags:
# Exercises on Taylor expansion
This page shows some exercises on calculating Taylor expansions. If you reload this page, you'll get new values.
Use this page to practice calculating Taylor expansions. This requires
Click `rocket` -->`Live Code` to start practising.
```{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:
1. You should work out the exercises "by hand" first (using paper or a digital notebook)
2. 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$; use `pi` or `sin()` and `cos()` for the trigonometric functions (not numpy!)
3. 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._
```
%% Cell type:code id: tags:thebe-remove-input-init
``` python
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)
```
%% Cell type:markdown id: tags:
## Exercise 1
Calculate the taylor series expansion of:
%% Cell type:code id: tags:thebe-remove-input-init
``` python
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)
```
%% Cell type:markdown id: tags:
around:
%% Cell type:code id: tags:thebe-remove-input-init
``` python
display(sp.Eq(x,c_1))
```
%% Cell type:markdown id: tags:
Discard any $O(x^3)$ terms.
Fill in your answer and run the cell before clicking 'Check answer'.
%% Cell type:code id: tags:auto-execute-page,disable-download-page
``` python
eq1_answer =
```
%% Cell type:code id: tags:thebe-remove-input-init
``` python
check_answer("eq1_answer",eq1_correct, check_equation)
```
%% Output
%% Cell type:markdown id: tags:
```{tip}
Note that for Exercise 1, the equation is an even-order polynomial, which means that the Taylor Series should not introduce any error!
```
%% Cell type:markdown id: tags:
## Exercise 2
Calculate the taylor series expension of:
%% Cell type:code id: tags:thebe-remove-input-init
``` python
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
```
%% Cell type:markdown id: tags:
around:
%% Cell type:code id: tags:thebe-remove-input-init
``` python
display(sp.Eq(x,c_2*sp.pi))
```
%% Cell type:markdown id: tags:
discard any $O(x^3)$ terms.
Fill in your answer and run the cell before clicking 'Check answer'. Furthermore, use `pi` for $\pi$:
%% Cell type:code id: tags:
``` python
eq2_answer =
```
%% Cell type:code id: tags:thebe-remove-input-init
``` python
check_answer("eq2_answer",eq2_correct, check_equation)
```
%% Cell type:markdown id: tags:
## Exercise 3
Calculate the taylor series expension of:
%% Cell type:code id: tags:thebe-remove-input-init
``` python
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
```
%% Cell type:markdown id: tags:
around:
%% Cell type:code id: tags:thebe-remove-input-init
``` python
display(sp.Eq(x,c_3))
```
%% Cell type:markdown id: tags:
discard any $O(x^3)$ terms.
Fill in your answer and run the cell before clicking 'Check answer':
%% Cell type:code id: tags:
``` python
eq3_answer =
```
%% Cell type:code id: tags:thebe-remove-input-init
``` python
check_answer("eq3_answer",eq3_correct, check_equation)
```
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment