manual_teachbooks/book/examples/parametric_questions.ipynb
2024-08-07 14:52:51 +02:00

318 lines
6.7 KiB
Text

{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Parametric questions\n",
"\n",
"On this page an example is given for parametric questions, in which an equation is checked on equivalence with the correct answer."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"tags": [
"thebe-remove-input-init"
]
},
"outputs": [],
"source": [
"import sympy as sp\n",
"import numpy as np\n",
"from sympy import pi, latex\n",
"from sympy.printing.mathml import mathml\n",
"\n",
"import operator\n",
"import ipywidgets as widgets\n",
"from IPython.display import display, Latex, display_jpeg, Math, Markdown\n",
"sp.init_printing(use_latex=True) \n",
"\n",
"check_equation = lambda eq1, eq2: sp.simplify(eq1-eq2) == 0\n",
"\n",
"def check_answer(variable_name, expected, comparison = operator.eq):\n",
" output = widgets.Output()\n",
" button = widgets.Button(description=\"Check answer\")\n",
" def _inner_check(button):\n",
" with output:\n",
" if comparison(globals()[variable_name], expected):\n",
" output.outputs = [{'name': 'stdout', 'text': 'Correct!',\n",
" 'output_type': 'stream'}]\n",
" else:\n",
" output.outputs = [{'name': 'stdout', 'text': 'Incorrect!',\n",
" 'output_type': 'stream'}]\n",
" button.on_click(_inner_check)\n",
" display(button, output)\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Exercises on Taylor expansion\n",
"\n",
"This page shows some exercises on calculation Taylor expansion. If you reload this page, you'll get new values.\n",
"\n",
"### Exercise 1\n",
"\n",
"Calculate the taylor series expension of:\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"scrolled": true,
"tags": [
"thebe-remove-input-init"
]
},
"outputs": [],
"source": [
"x, y = sp.symbols('x, y')\n",
"a_1 = sp.Integer(np.random.randint(2,6))\n",
"b_1 = sp.Integer(np.random.randint(-10,10))\n",
"c_1 = sp.Integer(np.random.randint(-5,5))\n",
"eq1_original = a_1 * x**2 + b_1*x\n",
"eq1_correct = sp.series(eq1_original,x,c_1)\n",
"eq1_answer = 0\n",
"display(eq1_original)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"around:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"tags": [
"thebe-remove-input-init"
]
},
"outputs": [],
"source": [
"display(sp.Eq(x,c_1))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Fill in your answer and run the cell before clicking 'Check answer'. "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"tags": [
"auto-execute-page",
"disable-download-page"
]
},
"outputs": [],
"source": [
"eq1_answer = "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"tags": [
"thebe-remove-input-init"
]
},
"outputs": [],
"source": [
"check_answer(\"eq1_answer\",eq1_correct, check_equation)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Exercise 2\n",
"\n",
"Calculate the taylor series expension of:\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"tags": [
"thebe-remove-input-init"
]
},
"outputs": [],
"source": [
"a_2 = sp.Integer(np.random.randint(1,7))\n",
"c_2 = sp.Integer(np.random.randint(-5,5))\n",
"eq2_original = a_2*sp.tan(x)\n",
"display(eq2_original)\n",
"eq2_correct = sp.series(eq2_original,x,c_2*sp.pi,3).removeO()\n",
"#display(eq2_correct)\n",
"eq2_answer = 0"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"around:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"tags": [
"thebe-remove-input-init"
]
},
"outputs": [],
"source": [
"display(sp.Eq(x,c_2*sp.pi))"
]
},
{
"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'. Furthermore, use `pi` for $\\pi$:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"eq2_answer = "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"tags": [
"thebe-remove-input-init"
]
},
"outputs": [],
"source": [
"check_answer(\"eq2_answer\",eq2_correct, check_equation)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Exercise 3\n",
"\n",
"Calculate the taylor series expension of:\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"tags": [
"thebe-remove-input-init"
]
},
"outputs": [],
"source": [
"a_3 = sp.Integer(np.random.randint(1,10))\n",
"c_3 = sp.Integer(np.random.randint(-1,1))\n",
"eq3_original = a_3 / (1 - x)\n",
"display(eq3_original)\n",
"eq3_correct = sp.series(eq3_original,x,c_3,3).removeO()\n",
"#display(eq3_correct)\n",
"eq3_answer = 0"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"around:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"tags": [
"thebe-remove-input-init"
]
},
"outputs": [],
"source": [
"display(sp.Eq(x,c_3))"
]
},
{
"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": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"eq3_answer ="
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"tags": [
"thebe-remove-input-init"
]
},
"outputs": [],
"source": [
"check_answer(\"eq3_answer\",eq3_correct, check_equation)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.5"
}
},
"nbformat": 4,
"nbformat_minor": 2
}