Parametric questions#

On this page an example is given for parametric questions, in which an equation is checked on equivalence with the correct answer.

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()
    button = widgets.Button(description="Check answer")
    def _inner_check(button):
        with output:
            if comparison(globals()[variable_name], expected):
                output.outputs = [{'name': 'stdout', 'text': 'Correct!',
                                   'output_type': 'stream'}]
            else:
                output.outputs = [{'name': 'stdout', 'text': 'Incorrect!',
                                   'output_type': 'stream'}]
    button.on_click(_inner_check)
    display(button, output)

Exercises on Taylor expansion#

This page shows some exercises on calculation Taylor expansion. If you reload this page, you’ll get new values.

Exercise 1#

Calculate the taylor series expension 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)

around:

display(sp.Eq(x,c_1))

Fill in your answer and run the cell before clicking ‘Check answer’.

eq1_answer = 
check_answer("eq1_answer",eq1_correct, check_equation)

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)