當前位置:首頁 » 編程語言 » arangepython

arangepython

發布時間: 2023-09-06 16:09:29

1. 如何使用python計算常微分方程

常用形式
odeint(func, y0, t,args,Dfun)
一般這種形式就夠用了。
下面是官方的例子,求解的是
D(D(y1))-t*y1=0
為了方便,採取D=d/dt。如果我們令初值
y1(0) = 1.0/3**(2.0/3.0)/gamma(2.0/3.0)
D(y1)(0) = -1.0/3**(1.0/3.0)/gamma(1.0/3.0)
這個微分方程的解y1=airy(t)。

令D(y1)=y0,就有這個常微分方程組。
D(y0)=t*y1
D(y1)=y0

Python求解該微分方程。
>>> from scipy.integrate import odeint
>>> from scipy.special import gamma, airy
>>> y1_0 = 1.0/3**(2.0/3.0)/gamma(2.0/3.0)
>>> y0_0 = -1.0/3**(1.0/3.0)/gamma(1.0/3.0)
>>> y0 = [y0_0, y1_0]
>>> def func(y, t):
... return [t*y[1],y[0]]
>>> def gradient(y,t):
... return [[0,t],[1,0]]
>>> x = arange(0,4.0, 0.01)
>>> t = x
>>> ychk = airy(x)[0]
>>> y = odeint(func, y0, t)
>>> y2 = odeint(func, y0, t, Dfun=gradient)
>>> print ychk[:36:6]
[ 0.355028 0.339511 0.324068 0.308763 0.293658 0.278806]
>>> print y[:36:6,1]
[ 0.355028 0.339511 0.324067 0.308763 0.293658 0.278806]
>>> print y2[:36:6,1]
[ 0.355028 0.339511 0.324067 0.308763 0.293658 0.278806]

得到的解與精確值相比,誤差相當小。
=======================================================================================================

args是額外的參數。
用法請參看下面的例子。這是一個洛侖茲曲線的求解,並且用matplotlib繪出空間曲線圖。(來自《python科學計算》)
from scipy.integrate import odeint
import numpy as np
def lorenz(w, t, p, r, b):
# 給出位置矢量w,和三個參數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) # 創建時間點
# 調用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()
===========================================================================
scipy.integrate.odeint(func, y0, t, args=(), Dfun=None, col_deriv=0, full_output=0, ml=None, mu=None, rtol=None, atol=None, tcrit=None, h0=0.0, hmax=0.0, hmin=0.0, ixpr=0, mxstep=0, mxhnil=0, mxordn=12, mxords=5, printmessg=0)
計算常微分方程(組)
使用 FORTRAN庫odepack中的lsoda解常微分方程。這個函數一般求解初值問題。

參數:

func : callable(y, t0, ...) 計算y在t0 處的導數。
y0 : 數組 y的初值條件(可以是矢量)
t : 數組 為求出y,這是一個時間點的序列。初值點應該是這個序列的第一個元素。
args : 元組 func的額外參數
Dfun : callable(y, t0, ...) 函數的梯度(Jacobian)。即雅可比多項式。
col_deriv : boolean. True,Dfun定義列向導數(更快),否則Dfun會定義橫排導數
full_output : boolean 可選輸出,如果為True 則返回一個字典,作為第二輸出。
printmessg : boolean 是否列印convergence 消息。

返回: y : array, shape (len(y0), len(t))
數組,包含y值,每一個對應於時間序列中的t。初值y0 在第一排。
infodict : 字典,只有full_output == True 時,才會返回。
字典包含額為的輸出信息。
鍵值:

『hu』 vector of step sizes successfully used for each time step.
『tcur』 vector with the value of t reached for each time step. (will always be at least as large as the input times).
『tolsf』 vector of tolerance scale factors, greater than 1.0, computed when a request for too much accuracy was detected.
『tsw』 value of t at the time of the last method switch (given for each time step)
『nst』 cumulative number of time steps
『nfe』 cumulative number of function evaluations for each time step
『nje』 cumulative number of jacobian evaluations for each time step
『nqu』 a vector of method orders for each successful step.
『imxer』index of the component of largest magnitude in the weighted local error vector (e / ewt) on an error return, -1 otherwise.
『lenrw』 the length of the double work array required.
『leniw』 the length of integer work array required.
『mused』a vector of method indicators for each successful time step: 1: adams (nonstiff), 2: bdf (stiff)
其他參數,官方網站和文檔都沒有明確說明。相關的資料,暫時也找不到。

2. python中如何取一列數最大值

如果是從列表中找最大值,則可以使用max(),如:

In[279]:a=range(10)
In[280]:max(a)
Out[280]:9

如果是從數組找最大值,則可以使用numpy.max()函數,如:

In[281]:a=np.arange(10)
In[282]:a.max()
Out[282]:9

如果是一個二維數組,取某一列的最大值,則:

In[285]:a=np.arange(12).reshape(3,4)
In[286]:a
Out[286]:
array([[0,1,2,3],
[4,5,6,7],
[8,9,10,11]])
In[287]:a[2,:].max()
Out[287]:11
熱點內容
滑板鞋腳本視頻 發布:2025-02-02 09:48:54 瀏覽:433
群暉怎麼玩安卓模擬器 發布:2025-02-02 09:45:23 瀏覽:557
三星安卓12彩蛋怎麼玩 發布:2025-02-02 09:44:39 瀏覽:743
電腦顯示連接伺服器錯誤 發布:2025-02-02 09:24:10 瀏覽:537
瑞芯微開發板編譯 發布:2025-02-02 09:22:54 瀏覽:146
linux虛擬機用gcc編譯時顯示錯誤 發布:2025-02-02 09:14:01 瀏覽:240
java駝峰 發布:2025-02-02 09:13:26 瀏覽:652
魔獸腳本怎麼用 發布:2025-02-02 09:10:28 瀏覽:538
linuxadobe 發布:2025-02-02 09:09:43 瀏覽:212
sql2000資料庫連接 發布:2025-02-02 09:09:43 瀏覽:726