Skip to main content

Tutorial 1

Binder Open In Colab

Sympy: SymPy is a Python library for symbolic mathematics. We will use this library to solve all the tutorials.

import sympy as sy
from sympy.abc import x, y, t # variable x
from sympy.solvers import solve # Solving equations
from sympy import sqrt,tan,sin,cos,sec,pi,root,ln
from sympy import log,exp,atan,asinh,atanh,asin # Import all required math function
from sympy import diff, idiff # Solving differentiation
sy.init_printing(use_latex=True) # Show it in natural display

How to use sympy to solve limit questions?

  1. Declare the symbol to be used in the function. For example, if you wanted to use the symbol xx, just use x as usual because we have import the variable x earlier. Alternatively, you can declare xx such that x = sy.symbols('x'). This will make the variable x a representation of xx in the function to be solved. Note that sy is used because we import sympy as sy.

  2. Type the function. For example, if the function is 1x\frac{1}{x}, type it as 1/x. Note, for power, python uses ** instead of ^.

  3. Solve the limit with sympy library by using sy.limit(function,symbol,limit). sy.limit takes three parameters, which are the function, the symbols used in the function and the limit. The return of this fuction will be the answer.

How to use sympy to solve derivative questions?

Just diff(function,symbol)!

Some cheat sheet!

To make sure if you input the equation correctly, you can use the display(function) function. For example,

x = sqrt(x)
display(x)

will return:

x\sqrt{x}

The representation of mathematics function in Python:

Mathematics FunctionPython representation
x2x^2x**2
x\sqrt{x}sqrt(x)
x3\sqrt[3]{x}root(x,3)
tanx\tan{x}tan(x)
secx\sec{x}sec(x)
π\pipi

Note that the sqrt, tan, sec, pi used in this notebook are from sympy library, not from math library.

That's it! Let's use this knowledge and solve them.

  1. a.

    lim = 5
    func = (x**2-25)/(x**2+x-30)
    sy.limit(func,x,lim)

    b.

    lim = 9
    func = (sqrt(x)-3)/(x-9)
    sy.limit(func,x,lim)

    c.

    lim = 0
    func = x/(3-sqrt(x+9))
    sy.limit(func,x,lim)
  2. diff(log(4+cos(x),10))
  3. eqn=cos(x**2)-x*exp(y)
    idiff(eqn,y,x)
  4. eqn=x**3*y**3-2*y-x
    idiff(eqn,y,x)
  5. dydt = sqrt(1-4*t).diff(t)
    dxdt = 7*ln(t).diff(t)
    dydt/dxdt
  6. dydt = 2*exp(t).diff(t)
    dxdt = (t**2-1).diff(t)
    dydt/dxdt
  7. a.

    eqn=x**2*tan(y)+y**10*sec(x)-2*x
    idiff(eqn,y,x)

    b.

    eqn=x**3*y**5+3*x-8*y**3-1
    idiff(eqn,y,x)

    c.

    eqn=exp(2*x+3*y)-x**2+ln(x*y**3)
    idiff(eqn,y,x)
  8. diff(ln(cos(x**2)))
  9. eqn=10*exp(2*x*y)-exp(15*y)-exp(13*x)
    idiff(eqn,y,x)
  10. diff(2*x*(atan(5*x))**2+6*tan(cos(6*x)))
  11. diff(4*x*asinh(x/6)+atanh(cos(10*x)))