scipy なぞるならこれ

  • 読み込んだデータでプロットしてみる
os.chdir('C:\\Users\\ryamada\\Desktop\\python')
data = np.loadtxt('populations.txt')
year, hares, lynxes, carrots = data.T
plt.axes([0.2, 0.1, 0.5, 0.8]) 
plt.plot(year, hares, year, lynxes, year, carrots)
plt.legend(('Hare', 'Lynx', 'Carrot'), loc=(1.05, 0.5)) 
  • 3次元数値積分をしてみる。f(a,b,c)=a^b-cなる関数を0 \le a, b, c \le 1の範囲で数値積分してみる。理論値は\ln(2)-1/2=0.19314....という
def f1 (a,b,c):
    return a**b -c

x = np.ogrid[0:1:100j] # ここで100jと複素数を使うのは、0-1の1も含んで、100個のグリッドを作る、というやり方(どうしてそうなのかは未確認)
y = np.ogrid[0:1:100j]
z = np.ogrid[0:1:100j]

X, Y, Z = np.meshgrid(x, y, z)

sum(f1(X,Y,Z)*(x[1]-x[0])*(y[1]-y[0])*(z[1]-z[0]))

Out[52]: 0.19693802124662518

log(2)-0.5
Out[54]: 0.19314718055994529
  • グリッドはよく使うはず。始点ー終点(を含めて)作りたいだろう

x = np.linspace(-5, 5, 1000) # [-5,5]で1000点
y = np.linspace(-5, 5, 1000)
xx, yy = meshgrid(x, y, sparse=True)
z = np.sin(xx**2 + yy**2) / (xx**2 + yy**2)
h = plt.contourf(x,y,z)
N_max = 50
x = np.linspace(-2, 1, 1000)
y = np.linspace(-1.5, 1.5, 1000)
xx, yy = meshgrid(x, y, sparse=True)

c = xx + 1j * yy
z = np.zeros_like(xx) + 0.1*1j
for j in xrange(N_max):
	z = z**2 + c
plt.contourf(x,y,(real(z)**2 + imag(z)**2).reshape(1000,1000)<50^2)