1.方程求导
代码语言:javascript复制from __future__ import print_function
from __future__ import division
import numpy as np
import scipy as sp
import scipy.misc
def f(x): return 2*x*x 3*x 1
print(sp.misc.derivative(f, 2))
2.求不定积分
代码语言:javascript复制from __future__ import print_function
from __future__ import division
import numpy as np
import scipy as sp
import scipy.integrate
f = lambda x : x**2
print(sp.integrate.quad(f, 0, 2))
print(sp.integrate.fixed_quad(f, 0, 2))
3.求解非线性方程组
代码语言:javascript复制from __future__ import print_function
from __future__ import division
import numpy as np
import scipy as sp
import scipy.optimize
def f(x):
return [5*x[1] 3, 4*x[0]*x[0], x[1]*x[2] - 1.5]
ans = sp.optimize.fsolve(f, [0, 0, 0])
print(ans)
print(f(ans))
4.求解线性方程组
代码语言:javascript复制from __future__ import print_function
from __future__ import division
import numpy as np
import scipy as sp
import matplotlib.pylab as plt
import scipy.linalg
a = np.array([[1, 3, 5], [2, 5, 1], [2, 3, 8]])
b = np.array([10, 8, 3])
print(sp.linalg.solve(a, b))
# print(sp.linalg.inv(a).dot(b))
5.画3D马鞍面
代码语言:javascript复制from __future__ import print_function
from __future__ import division
import numpy as np
import scipy as sp
import matplotlib.pylab as plt
from pylab import *
from mpl_toolkits.mplot3d import Axes3D
fig = figure()
ax = Axes3D(fig)
X = np.linspace(-10, 10, 30)
Y = np.linspace(-10, 10, 30)
X, Y = np.meshgrid(X, Y)
Z = (X*X)/3 - (Y*Y)/3
ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap='hot')
show()
6.画三叶玫瑰线
代码语言:javascript复制from __future__ import print_function
from __future__ import division
import numpy as np
import scipy as sp
import matplotlib.pylab as plt
t = np.linspace(0, 2*np.pi, 1000, endpoint=True)
p = 3 * np.sin(3 * t)
plt.subplot(111, polar=True)
plt.plot(t, p, lw=3, color='g')
plt.grid(True)
plt.show()
7.画星形线
代码语言:javascript复制from __future__ import print_function
from __future__ import division
import numpy as np
import scipy as sp
import matplotlib.pylab as plt
t = np.linspace(-10, 10, 1024)
X = np.cos(t) ** 3
Y = np.sin(t) ** 3
plt.plot(X, Y, label='My', lw=3, color='g')
plt.xlim(X.min()*1.2, X.max()*1.2)
plt.ylim(Y.min()*1.2, Y.max()*1.2)
plt.grid(True)
plt.legend()
plt.show()
8.画随机散点图
代码语言:javascript复制from __future__ import print_function
from __future__ import division
import numpy as np
import scipy as sp
import numpy.random
import matplotlib.pylab as plt
X = np.random.rand(50)
Y = np.random.rand(50)
colors = np.random.rand(50)
area = np.pi * (15 * np.random.rand(50)) ** 2
plt.scatter(X, Y, s=area, c=colors)
plt.show()
解释器提示如:SyntaxError: invalid character in identifier, 但又一直找不到问题点的话,请确保代码行内没有夹杂中文的空格,tab等,非文字字符. 例如
代码语言:javascript复制for x in range(10): #这是个注释
print(x) #下一个注释这样的代码就会出现
以上错误, 因为在注释 # 之前夹杂了中文输入法的空格,print缩进里也是如此错误.