python計算微分函數(shù) python 求微分

python 的scipy 里的 odeint 這個求微分方程的函數(shù)怎么用啊

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)

成都創(chuàng)新互聯(lián)公司為您提適合企業(yè)的網(wǎng)站設(shè)計?讓您的網(wǎng)站在搜索引擎具有高度排名,讓您的網(wǎng)站具備超強的網(wǎng)絡(luò)競爭力!結(jié)合企業(yè)自身,進行網(wǎng)站設(shè)計及把握,最后結(jié)合企業(yè)文化和具體宗旨等,才能創(chuàng)作出一份性化解決方案。從網(wǎng)站策劃到成都網(wǎng)站建設(shè)、成都網(wǎng)站制作, 我們的網(wǎng)頁設(shè)計師為您提供的解決方案。

實際使用中,還是主要使用前三個參數(shù),即微分方程的描寫函數(shù)、初值和需要求解函數(shù)值對應(yīng)的的時間點。接收數(shù)組形式。這個函數(shù),要求微分方程必須化為標(biāo)準(zhǔn)形式,即dy/dt=f(y,t,)。

fromscipyimportodeint

y=odeint(dy/dt=r*y*(1-y/k),y(0)=0.1,t)

對于微分方程全還給老師了,

python里怎么樣求解微分方程

有很多大學(xué)生問我,學(xué)習(xí)python有什么用呢?我說:你至少可以用來解微分方程,如下面的例子,就是解決微分方程:

y"+a*y'+b*y=0?

代碼如下:

[python]?view plain?copy

#y"+a*y'+b*y=0

from?scipy.integrate?import?odeint

from?pylab?import?*

def?deriv(y,t):????????#?返回值是y和y的導(dǎo)數(shù)組成的數(shù)組

a?=?-2.0

b?=?-0.1

return?array([?y[1],?a*y[0]+b*y[1]?])

time?=?linspace(0.0,50.0,1000)

yinit?=?array([0.0005,0.2])?????#?初值

y?=?odeint(deriv,yinit,time)

figure()

plot(time,y[:,0],label='y')????#y[:,0]即返回值的第一列,是y的值。label是為了顯示legend用的。

plot(time,y[:,1],label="y'")?????#y[:,1]即返回值的第二列,是y’的值

xlabel('t')

ylabel('y')

legend()

show()

輸出結(jié)果如下:

如何使用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]

得到的解與精確值相比,誤差相當(dāng)小。

=======================================================================================================

args是額外的參數(shù)。

用法請參看下面的例子。這是一個洛侖茲曲線的求解,并且用matplotlib繪出空間曲線圖。(來自《python科學(xué)計算》)

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 的計算公式對應(yīng)

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()

===========================================================================

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解常微分方程。這個函數(shù)一般求解初值問題。

參數(shù):

func : callable(y, t0, ...) 計算y在t0 處的導(dǎo)數(shù)。

y0 : 數(shù)組 y的初值條件(可以是矢量)

t : 數(shù)組 為求出y,這是一個時間點的序列。初值點應(yīng)該是這個序列的第一個元素。

args : 元組 func的額外參數(shù)

Dfun : callable(y, t0, ...) 函數(shù)的梯度(Jacobian)。即雅可比多項式。

col_deriv : boolean. True,Dfun定義列向?qū)?shù)(更快),否則Dfun會定義橫排導(dǎo)數(shù)

full_output : boolean 可選輸出,如果為True 則返回一個字典,作為第二輸出。

printmessg : boolean 是否打印convergence 消息。

返回: y : array, shape (len(y0), len(t))

數(shù)組,包含y值,每一個對應(yīng)于時間序列中的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)

其他參數(shù),官方網(wǎng)站和文檔都沒有明確說明。相關(guān)的資料,暫時也找不到。

當(dāng)前文章:python計算微分函數(shù) python 求微分
鏈接分享:http://muchs.cn/article6/doshcog.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供移動網(wǎng)站建設(shè)、軟件開發(fā)靜態(tài)網(wǎng)站、商城網(wǎng)站、小程序開發(fā)App設(shè)計

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

h5響應(yīng)式網(wǎng)站建設(shè)