Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
Book
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
MUDE
Book
Commits
8baa8370
Commit
8baa8370
authored
6 months ago
by
Isabel Slingerland
Browse files
Options
Downloads
Patches
Plain Diff
added a sentence
parent
1e86aee1
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
book/numerical_methods/taylor-series-exercise.ipynb
+2
-0
2 additions, 0 deletions
book/numerical_methods/taylor-series-exercise.ipynb
with
2 additions
and
0 deletions
book/numerical_methods/taylor-series-exercise.ipynb
+
2
−
0
View file @
8baa8370
...
...
@@ -120,6 +120,8 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"Discard any $O(x^3)$ terms.\n",
"\n",
"Fill in your answer and run the cell before clicking 'Check answer'. "
]
},
...
...
%% Cell type:markdown id: tags:
# Exercises on Taylor expansion
This page shows some exercises on calculation Taylor expansion. 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
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
latex
(
Math
(
display
(
f
"
The correct answer is:
{
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 expension 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
)
```
%% 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 $
\p
i$:
%% 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
)
```
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment