1.計算積分(1)計算定積分 from scipy import integrate #定義函數(shù) return (1-x**2)**0.5 pi_half, err = integrate.quad(half_circle, -1, 1) print(pi_half*2) #err為誤差精度 (2)計算二重積分 def half_sphere(x, y):
return (1-x**2-y**2)**0.5 print(integrate.dblquad(half_sphere, -1, 1,lambda x:-half_circle(x),lambda x:half_circle(x))[0]) 2.計算常微分方程(1)案例一,,計算洛侖茲吸引子的軌跡 # -*- coding: utf-8 -*-
from scipy.integrate import odeint
import numpy as np
def lorenz(w, t, p, r, b):
# 給出位置矢量w,,和三個參數(shù)p, r, b計算出
# dx/dt, dy/dt, dz/dt的值
x, y, z = w
# 直接與lorenz的計算公式對應
return np.array([p*(y-x), x*(r-z)-y, x*y-b*z])
t = np.arange(0, 30, 0.01) # 創(chuàng)建時間點
# 調(diào)用ode對lorenz進行求解, 用兩個不同的初始值
track1 = odeint(lorenz, (0.0, 1.00, 0.0), t, args=(10.0, 28.0, 3.0))
track2 = odeint(lorenz, (0.0, 1.01, 0.0), t, args=(10.0, 28.0, 3.0))
# 繪圖
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
fig = plt.figure()
ax = Axes3D(fig)
ax.plot(track1[:,0], track1[:,1], track1[:,2])
ax.plot(track2[:,0], track2[:,1], track2[:,2])
plt.show()
(2)案例二 #y"+a*y'+b*y=0 figure()
|
|
來自: 星光閃亮圖書館 > 《Python學習》