最常用的是在類定義的方法,給一個property的裝飾器,可以安裝調(diào)用屬性的方式調(diào)用
創(chuàng)新互聯(lián)公司服務(wù)項目包括衡陽網(wǎng)站建設(shè)、衡陽網(wǎng)站制作、衡陽網(wǎng)頁制作以及衡陽網(wǎng)絡(luò)營銷策劃等。多年來,我們專注于互聯(lián)網(wǎng)行業(yè),利用自身積累的技術(shù)優(yōu)勢、行業(yè)經(jīng)驗、深度合作伙伴關(guān)系等,向廣大中小型企業(yè)、政府機構(gòu)等提供互聯(lián)網(wǎng)行業(yè)的解決方案,衡陽網(wǎng)站推廣取得了明顯的社會效益與經(jīng)濟效益。目前,我們服務(wù)的客戶以成都為中心已經(jīng)輻射到衡陽省份的部分城市,未來相信會繼續(xù)擴大服務(wù)區(qū)域并繼續(xù)獲得客戶的支持與信任!
不寫出y=f(x)這樣的表達式,由隱函數(shù)的等式直接繪制圖像,以x2+y2+xy=1的圖像為例,使用sympy間接調(diào)用matplotlib工具的代碼和該二次曲線圖像如下(注意python里的乘冪符號是**而不是^,還有,python的sympy工具箱的等式不是a==b,而是a-b或者Eq(a,b),這幾點和matlab的區(qū)別很大)
直接在命令提示行的里面運行代碼的效果
from sympy import *;
x,y=symbols('x y');
plotting.plot_implicit(x**2+y**2+x*y-1);
常用形式
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是額外的參數(shù)。
用法請參看下面的例子。這是一個洛侖茲曲線的求解,并且用matplotlib繪出空間曲線圖。(來自《python科學計算》)
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 處的導數(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會定義橫排導數(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)的資料,暫時也找不到。
def use_list(): str_before=input("請輸入明文:") str_change=str_before.lower() str_list=list(str_change) str_list_change=str_list i=0 whilei
文章標題:python求解隱函數(shù) 如何求隱函數(shù)
鏈接URL:http://chinadenli.net/article38/hjjepp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供服務(wù)器托管、軟件開發(fā)、用戶體驗、做網(wǎng)站、搜索引擎優(yōu)化、網(wǎng)站營銷
聲明:本網(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)