Tutorial 1
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?
Declare the symbol to be used in the function. For example, if you wanted to use the symbol , just use x as usual because we have import the variable x earlier. Alternatively, you can declare such that
x = sy.symbols('x')
. This will make the variablex
a representation of in the function to be solved. Note thatsy
is used because weimport sympy as sy
.Type the function. For example, if the function is , type it as
1/x
. Note, for power, python uses**
instead of^
.Solve the limit with
sympy
library by usingsy.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:
The representation of mathematics function in Python:
Mathematics Function | Python representation |
---|---|
x**2 | |
sqrt(x) | |
root(x,3) | |
tan(x) | |
sec(x) | |
pi |
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.
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)diff(log(4+cos(x),10))
eqn=cos(x**2)-x*exp(y)
idiff(eqn,y,x)eqn=x**3*y**3-2*y-x
idiff(eqn,y,x)dydt = sqrt(1-4*t).diff(t)
dxdt = 7*ln(t).diff(t)
dydt/dxdtdydt = 2*exp(t).diff(t)
dxdt = (t**2-1).diff(t)
dydt/dxdta.
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)diff(ln(cos(x**2)))
eqn=10*exp(2*x*y)-exp(15*y)-exp(13*x)
idiff(eqn,y,x)diff(2*x*(atan(5*x))**2+6*tan(cos(6*x)))
diff(4*x*asinh(x/6)+atanh(cos(10*x)))