鸣谢:陈湛文、陈俊羽、徐泽瑞、魏彤等同学对此文做出了极大的贡献
Problem Scope:
(1)simplify a given function;
(2)solve the equation or equation set,determine the limit,integration of an expression;
(3)find the determinant and inverse of a matrix;matrix multiplication;
(4)plot a function in 2-D or 3-D.
值得注意的是,下文并不是对答案的罗列,而是解题的格式
Related knowledge points:
0 define a variable(Problem solved)
import sympy as sym
x=sym.Symbol('x')
1.1 simplify a given function(Problem solved)
import sympy as sym
x=sym.Symbol('x')
sym.simplify((x**3 + x**2 - x - 1)/(x**2 + 2*x + 1))
import sympy as sym
x=sympy.Symbol('x')
sympy.simplify((x**3 + x**2 - x - 1)/(x**2 + 2*x + 1))
1.2 expand a given function(Problem solved)
import sympy as sym
x=sympy.Symbol('x')
sym.expand((x-1)*(x+1)*(x+1))
2.1 solve the equation or equation set(Problem solved)
import sympy as sym
x=sympy.Symbol('x')
sym.solve('x**5+1',x)#此处列出了0=x^5+1的所有解
2.2 determine the limit(Problem solved)
import sympy as sym
x=sym.Symbol('x')
sym.limit(sym.sin(x) / x, x, 0)#当x趋于0时,求sinx/x的值
import sympy as sym
x=sym.Symbol('x')
sym.limit(x, x, sym.oo)
import sympy as sym
x=sym.Symbol('x')
sym.limit(1 / x, x, sym.oo)
2.3 determine the integration(Problem solved)
import sympy as sym
x=sym.Symbol('x')
sym.integrate(6 * x ** 5, x)
import sympy as sym
x=sym.Symbol('x')
sym.integrate(sym.exp(-x ** 2), (x, -sym.oo, sym.oo))
2.4 determine the differential(Problem solved)
import sympy as sym
sym.diff(sym.exp(x)*sym.cos(x),x)
3.1 determinant(Problem solved)
import numpy as np
A = np.array([[3,4],[5,-2]])
np.linalg.det(A)
3.2 inverse of a matrix(Problem solved)
import numpy as np
A = np.array([[3,4],[5,-2]])
np.linalg.inv(A)
3.3 matrix multiplication(Problem solved)
import numpy as np
A=np.array([[3,4],[5,-2]])
B=np.array([[1,2],[3,4]])
A@B
4.1 plot a function in 2-D(Problem solved)
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
x=np.linspace(0,10,200)
y=np.exp(-x)
fig, ax = plt.subplots()
ax.plot(x,y,'b-',label='exponential function')
ax.legend()
plt.show()
4.2 plot a function in 3-D(Problem solved)
import sympy as sym
import numpy as np
import matplotlib.pyplot as plt
z=sym.Symbol('z')
from mpl_toolkits.mplot3d.axes3d import Axes3D
from matplotlib import cm
def f(x,y):
return 1-x**2-(y**2)/4
xgrid = np.linspace(-3, 3, 50)
ygrid = xgrid
x, y = np.meshgrid(xgrid, ygrid)
fig = plt.figure(figsize=(8, 6))
ax = fig.add_subplot(111, projection='3d')
ax.plot_surface(x,
y,
f(x, y),
rstride=2, cstride=2,
cmap=cm.jet,
alpha=0.7,
linewidth=0.25)
ax.set_zlim(-0.5, 1.0)
plt.show()