In this tutorial, we discuss how to solve the problems from the topics like limits, derivatives, and Taylor's series expansion using SymPy.
# Load required libraries
import sympy as smp
from sympy import *
x, y = smp.symbols('x y')
f = x**2+y
f
smp.sin(x)
smp.sec(x)
other basic math function
x**(smp.Rational(3,2))
SymPy can compute symbolic limits with the limit function. We use the following syntax
smp.limit(f(x),x,x0)smp.limit(f(x),x,x0,dir="+")smp.limit(f(x),x,x0,dir="-")Compute the limits of the following: $$\lim_{x\rightarrow \pi} \sin (x/2 +\sin(x))$$
smp.limit(smp.sin(x/2+smp.sin(x)),x,smp.pi)
smp.Limit(smp.sin(x/2+smp.sin(x)),x,smp.pi)
smp.limit(2*smp.exp(1/x) / (smp.exp(1/x) + 1),x,0,dir='+') # one sided limit
smp.limit((smp.cos(x)-1)/x,x,smp.oo)
smp.diff(((1+smp.sin(x))/(1-smp.sin(x)))**2,x)
smp.diff(((1+smp.sin(x))/(1-smp.cos(x)))**2,x)
smp.diff(smp.log(x,5)**(x/2),x)
f, g = smp.symbols('f g', cls=smp.Function)
g = g(x)
f = f(x+g)
smp.diff(f,x)
# First derivatives
smp.diff(smp.exp(x**4),x)
# Second derivative
smp.diff(smp.exp(x**4),x,x)
z = smp.symbols('z')
expr = smp.exp(x*y*z)
diff(expr, x,y,y,z,z,z,z)
## Equivalently
diff(expr,x,y,2,z,4)
## Alternative
deriv = smp.Derivative(expr,x,y,2,z,4)
deriv
deriv.doit()
Any smooth function $f(x)$ can be approximated by using series expansion. The Taylor's series of the function $f(x)$ at $x=x_0$ is $$ f(x)=f(x_0) + {(x-x_0)f'(x_0)\over 1!}+ {(x-x_0)^2f"(x_0)\over 2!}+\cdots+\infty $$ The series expansion at $x_0=0$ is known as Maclaurin's series.
SymPy can compute asymptotic series expansions of functions around a point. To compute the expansion of $f(x)$ around the point $x=x_0$ terms of order $x^n.$ The following sytax will give the series expansion.
f(x).series(x,x0,n)
By default x0=0 and n=6
fun = smp.sin(x)
fun.series(x,0,8)
All $x$ terms with power greater than or equal to $x^8$ are omitted.
Taylor's series of the function $e^x$ about $x=1.$
smp.exp(x).series(x,x0=6,n=4)