Skip to content
Snippets Groups Projects
Commit 63a317d8 authored by Tom van Woudenberg's avatar Tom van Woudenberg
Browse files

test2

parent 6bd1a1ee
No related branches found
No related tags found
1 merge request!382Python + mathematical notation
%% 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.
Click `rocket` -->`Live Code` to start practising.
%% 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 display, Latex, display_jpeg, Math, Markdown
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: {sp.latex(expected)}")
display(expected)
print(f"The correct answer is in python notation: {str(expected)}")
print(f"Or in mathematical notation: {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 = 1
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:
## 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