参考书籍:毕文斌, 毛悦悦. Python漫游数学王国[M]. 北京:清华大学出版社,2022.
无穷大在sympy中用两个字母o表示无穷大,正无穷大为sy.oo,负无穷大为-sy.oo
import sympy as sy
x=sy.oo
print(1/x)
>>>0
lim x → 0 − 1 x \lim_{x \to 0^-} \frac{1}{x} limx→0−x1
x=sy.symbols('x')
print(sy.limit(1/x,x,0,dir='-'))
>>>-oo
lim x → 3 x − 3 x 2 − 9 \lim_{x \to 3} \frac{x-3}{x^2-9} limx→3x2−9x−3
import sympy as sy
x=sy.symbols('x')
print(sy.limit((x-3)/(x**2-9),x,3,dir='+-'))
lim x → 1 2 x − 3 x 2 − 5 x + 4 \lim_{x \to 1} \frac{2x-3}{x^2-5x+4} limx→1x2−5x+42x−3
x=sy.symbols('x')
print(sy.limit((2*x-3)/(x**2-5*x+4),x,1,dir='-'))
print(sy.limit((2*x-3)/(x**2-5*x+4),x,1))
>>>-oo, oo 故趋于无穷时极限为无穷oo
lim x → ∞ 3 x 3 + 4 X 2 + 2 7 x 3 + 5 x 2 − 3 \lim_{x \to \infty} \frac{3x^3+4X^2+2}{7x^3+5x^2-3} limx→∞7x3+5x2−33x3+4X2+2
x=sy.symbols('x')
print(sy.limit((3*x**3+4*x**2+2)/(7*x**3+5*x**2-3),x,sy.oo,dir='-'))
print(sy.limit((3*x**3+4*x**2+2)/(7*x**3+5*x**2-3),x,-sy.oo,dir='+'))
>>>3/7,3/7 故趋于无穷时极限为3/7
当分子分母极限都不存在时, lim x → ∞ sin x x \lim_{x \to \infty} \frac{\sin x}{x} limx→∞xsinx
x=sy.symbols('x')
y=sy.sin(x)/x
print(sy.limit(y,x,sy.oo,dir='+'))
print(sy.limit(y,x,-sy.oo,dir='+'))
>>>0 , 0 故趋于无穷时极限为0
eg1: lim x → 0 sin x x \lim_{x \to 0} \frac{\sin x}{x} limx→0xsinx
import sympy as sy
x=sy.symbols('x')
lim=sy.limit(sy.sin(x)/x,x,0,dir='+-')
print(lim)
>>>1
eg2: lim x → 0 arcsin x tan x \lim_{x \to 0} \frac{\arcsin x}{\tan x} limx→0tanxarcsinx
x=sy.symbols('x')
print(sy.limit(sy.asin(x)/sy.tan(x),x,0,dir='+-')) #sy.asin()指arcsin函数
>>>1
eg3: lim x → 0 1 − cos x x 2 \lim_{x \to 0} \frac{1- \cos x}{x^2} limx→0x21−cosx
x=sy.symbols('x')
print(sy.limit((1-sy.cos(x))/(x**2),x,0,dir='+-'))
>>>1/2
eg4: lim x → 0 ( 1 + x ) 1 x \lim_{x \to 0} (1+x)^{\frac{1}{x}} limx→0(1+x)x1
x=sy.symbols('x')
lim=sy.limit((1+x)**(1/x),x,0,dir='+-')
print(lim)
>>>E
eg5: lim x → ∞ ( 1 + 1 x ) x \lim_{x \to \infty} (1+\frac{1}{x})^x limx→∞(1+x1)x
x=sy.symbols('x')
lim=sy.limit((1+1/x)**x,x,sy.oo,dir='-')
print(lim)
print(lim.round(3))
print(sy.limit((1+1/x)**x,x,-sy.oo))
>>>E, 2.718, E
eg6: 说明数列 2 , 2 + 2 , 2 + 2 + 2 \sqrt{2} , \sqrt{2+\sqrt{2}},\sqrt{2+\sqrt{2+\sqrt{2}}} 2,2+2,2+2+2,···的极限存在
#用函数的递归机制定义数列
def a_complex_series(n):
#退出条件
if n<=0:return 2**0.5
#一个函数如果调用自身,则这个函数就是一个递归函数
return (2.0+a_complex_series(n-1))**0.5
#绘制前20个数的散点图
import matplotlib.pyplot as plt
import numpy as np
x=[]
y=[]
for i in range(20):
x.append(i)
y.append(a_complex_series(i))
print(np.array(y))
plt.scatter(x,y)
plt.show()
>>>[1.41421356 1.84775907 1.96157056 1.99036945 1.99759091 1.99939764
1.9998494 1.99996235 1.99999059 1.99999765 1.99999941 1.99999985
1.99999996 1.99999999 2. 2. 2. 2.
2. 2. ]
eg1: lim x → 0 tan 2 x sin 5 x \lim_{x \to 0} \frac{\tan 2x}{\sin 5x} limx→0sin5xtan2x
from sympy import limit,sin,cos,tan,symbols #从sympy中仅导入这几个函数
x=symbols('x')
example_1=tan(2*x)/sin(5*x)
result=limit(example_1,x,0,dir='+-')
print(result)
>>>2/5
eg2: lim x → 0 sin x x 3 + 3 x \lim_{x \to 0} \frac{\sin x}{x^3+3x} limx→0x3+3xsinx
x=symbols('x')
example_2=sin(x)/(x**3+3*x)
result=limit(example_2,x,0,dir='+-')
print(result)
>>>1/3
eg3: lim x → 0 ( 1 + x 2 ) 1 / 3 − 1 cos x − 1 \lim_{x \to 0} \frac{(1+x^2)^{1/3}-1}{\cos x-1} limx→0cosx−1(1+x2)1/3−1
x=symbols('x')
example_3=((1+x**2)**(1/3)-1)/(cos(x)-1)
result=limit(example_3,x,0,dir='+-')
print(result)
>>>-2/3