python中dt函數(shù) dt_add函數(shù)

怎么用python語句寫出公式dt=b的平方-4ac

import

成都創(chuàng)新互聯(lián)于2013年成立,先為東昌府等服務(wù)建站,東昌府等地企業(yè),進(jìn)行企業(yè)商務(wù)咨詢服務(wù)。為東昌府企業(yè)網(wǎng)站制作PC+手機(jī)+微官網(wǎng)三網(wǎng)同步一站式服務(wù)解決您的所有建站問題。

math

a,b,c

=

input("請(qǐng)輸入3個(gè)數(shù)字(空格分隔):").split()

a

=

float(a)

b

=

float(b)

c

=

float(c)

d

=

(b**2)

-

(4*a*c)

if

a==0

and

b==0

and

c==0

:

print("有無窮個(gè)解")elif

d

=

0:

x1

=

(-b-d/(2*a))

x2

=

(-b+d/(2*a))

print('結(jié)果為:%.2f,%.2f'%(x1,x2));

else:

print("無解")

如何使用python計(jì)算常微分方程?

常用形式

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)

這個(gè)微分方程的解y1=airy(t)。

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

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ù)。

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

from scipy.integrate import odeint

import numpy as np

def lorenz(w, t, p, r, b):

# 給出位置矢量w,和三個(gè)參數(shù)p, r, b 計(jì)算出

# dx/dt, dy/dt, dz/dt 的值

x, y, z = w

# 直接與lorenz 的計(jì)算公式對(duì)應(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)建時(shí)間點(diǎn)

# 調(diào)用ode 對(duì)lorenz 進(jìn)行求解, 用兩個(gè)不同的初始值

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)

計(jì)算常微分方程(組)

使用 FORTRAN庫odepack中的lsoda解常微分方程。這個(gè)函數(shù)一般求解初值問題。

參數(shù):

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

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

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

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

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

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

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

printmessg : boolean 是否打印convergence 消息。

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

數(shù)組,包含y值,每一個(gè)對(duì)應(yīng)于時(shí)間序列中的t。初值y0 在第一排。

infodict : 字典,只有full_output == True 時(shí),才會(huì)返回。

字典包含額為的輸出信息。

鍵值:

‘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)的資料,暫時(shí)也找不到。

python datetime處理時(shí)間

python時(shí)間處理方法datetime(),下面就舉幾個(gè)代碼案例進(jìn)行說明,代碼如下:

#?-*-?coding:?utf-8?-*-?

#?運(yùn)行環(huán)境:Python3.4

#datetime類

#datetime是date與time的結(jié)合體,包括date與time的所有信息。

#它的構(gòu)造函數(shù)如下:

#datetime.?datetime?(year,?month,?day[?,?hour[?,?minute[?,?second[?,?microsecond[?,?tzinfo]?]?]?]?]?)

#各參數(shù)的含義與date、time的構(gòu)造函數(shù)中的一樣,要注意參數(shù)值的范圍。

#?1.?datetime類定義的類屬性與方法:

#datetime.min、datetime.max:datetime所能表示的最小值與最大值;

#print:?datetime.max:?9999-12-31?23:59:59.999999

#print:?datetime.min:?0001-01-01?00:00:00

from??datetime??import??*?

import?time

print???('datetime.max:'?+str(datetime.max?))?

print???('datetime.min:'?+str(datetime.min))??

#datetime.resolution:datetime最小單位;

#print:?datetime.resolution:?0:00:00.000001

print???('datetime.resolution:'?+?str(datetime.resolution?))

#datetime.today():返回一個(gè)表示當(dāng)前本地時(shí)間的datetime對(duì)象;

#print:?today():?2012-09-12?19:37:50.721000

print???('today():'?+str(datetime.today()?))

#datetime.now([tz]):返回一個(gè)表示當(dāng)前本地時(shí)間的datetime對(duì)象,如果提供了參數(shù)tz,則獲取tz參數(shù)所指時(shí)區(qū)的本地時(shí)間;

#print:?now():?2012-09-12?19:37:50.738000

print???('now():'+str(?datetime.now()?))

#datetime.utcnow():返回一個(gè)當(dāng)前utc時(shí)間的datetime對(duì)象;

#print:?2012-09-12?11:37:50.739000

print???('utcnow():'?+str(datetime.utcnow()?))?

#datetime.fromtimestamp(timestamp[,?tz]):根據(jù)時(shí)間戮創(chuàng)建一個(gè)datetime對(duì)象,參數(shù)tz指定時(shí)區(qū)信息;

#print:?fromtimestamp(tmstmp):?2012-09-12?19:37:50.741000

print???('fromtimestamp(tmstmp):'?+str(datetime.fromtimestamp(time.time())?))

#datetime.utcfromtimestamp(timestamp):根據(jù)時(shí)間戮創(chuàng)建一個(gè)datetime對(duì)象;

#print:?utcfromtimestamp(tmstmp):?2012-09-12?11:37:50.742000

print???('utcfromtimestamp(tmstmp):'?+str(datetime.utcfromtimestamp(time.time()))?)

#datetime.combine(date,?time):根據(jù)date和time,創(chuàng)建一個(gè)datetime對(duì)象;

#print:?datetime.combine(date,time):??2012-09-12?19:46:05

d?=?date(2012,9,12)

from??datetime??import??*?

t?=?time(19,46,5)

print?('datetime.combine(date,time):?'+str(datetime.combine(d,t)))

#datetime.strptime(date_string,?format):將格式字符串轉(zhuǎn)換為datetime對(duì)象;

#print:?2007-03-04?21:08:12

print?(datetime.strptime("2007-03-04?21:08:12",?"%Y-%m-%d?%H:%M:%S"))

#2.?datetime類提供的實(shí)例方法與屬性

dt?=?datetime.strptime("2012-09-12?21:08:12",?"%Y-%m-%d?%H:%M:%S")

#print:?2012?9?12?21?8?12?0?None

print?(dt.year)

print(dt.month)

print(dt.day)

print(dt.hour)

print(dt.minute)

print(dt.second)

print(dt.microsecond)

print(dt.tzinfo)

print?(dt.date())

print?(dt.time())

print?(dt.replace(year?=?2013))

print?(dt.timetuple())

print?(dt.utctimetuple())

print?(dt.toordinal())

print?(dt.weekday())

print?(dt.isocalendar())

#print?dt.isoformat([sep])

#datetime.?ctime?():返回一個(gè)日期時(shí)間的C格式字符串,等效于time.ctime(time.mktime(dt.timetuple()));

#3.?格式字符串

#?datetime.?strftime?(format)

#?%a?星期的簡寫。如?星期三為Web

#?%A?星期的全寫。如?星期三為Wednesday

#?%b?月份的簡寫。如4月份為Apr

#?%B月份的全寫。如4月份為April?

#?%c:??日期時(shí)間的字符串表示。(如:?04/07/10?10:43:39)

#?%d:??日在這個(gè)月中的天數(shù)(是這個(gè)月的第幾天)

#?%f:??微秒(范圍[0,999999])

#?%H:??小時(shí)(24小時(shí)制,[0,?23])

#?%I:??小時(shí)(12小時(shí)制,[0,?11])

#?%j:??日在年中的天數(shù)?[001,366](是當(dāng)年的第幾天)

#?%m:??月份([01,12])

#?%M:??分鐘([00,59])

#?%p:??AM或者PM

#?%S:??秒(范圍為[00,61],為什么不是[00,?59],參考python手冊(cè)~_~)

#?%U:??周在當(dāng)年的周數(shù)當(dāng)年的第幾周),星期天作為周的第一天

#?%w:??今天在這周的天數(shù),范圍為[0,?6],6表示星期天

#?%W:??周在當(dāng)年的周數(shù)(是當(dāng)年的第幾周),星期一作為周的第一天

#?%x:??日期字符串(如:04/07/10)

#?%X:??時(shí)間字符串(如:10:43:39)

#?%y:??2個(gè)數(shù)字表示的年份

#?%Y:??4個(gè)數(shù)字表示的年份

#?%z:??與utc時(shí)間的間隔?(如果是本地時(shí)間,返回空字符串)

#?%Z:??時(shí)區(qū)名稱(如果是本地時(shí)間,返回空字符串)

#?%%:??%%?=?%

dt?=?datetime.now()

#print:?(%Y-%m-%d?%H:%M:%S?%f):??2012-09-12?23:04:27?145000

print?('(%Y-%m-%d?%H:%M:%S?%f):?'+?str(dt.strftime('%Y-%m-%d?%H:%M:%S?%f')))

#print:?(%Y-%m-%d?%H:%M:%S?%p):??12-09-12?11:04:27?PM

print?('(%Y-%m-%d?%H:%M:%S?%p):?'+str(dt.strftime('%y-%m-%d?%I:%M:%S?%p')))

#print:?%a:?Wed?

print?('%%a:?%s?'?%?dt.strftime('%a'))

#print:?%A:?Wednesday

print?('%%A:?%s?'?%?dt.strftime('%A'))

#print:?%b:?Sep?

print?('%%b:?%s?'?%?dt.strftime('%b'))

#print:?%B:?September

print?('%%B:?%s?'?%?dt.strftime('%B'))

#print:?日期時(shí)間%c:?09/12/12?23:04:27

print?('日期時(shí)間%%c:?%s?'?%?dt.strftime('%c'))

#print:?日期%x:09/12/12

print?('日期%%x:%s?'?%?dt.strftime('%x'))

#print:?時(shí)間%X:23:04:27

print?('時(shí)間%%X:%s?'?%?dt.strftime('%X'))

#print:?今天是這周的第3天

print?('今天是這周的第%s天?'?%?dt.strftime('%w'))

#print:?今天是今年的第256天?

print?('今天是今年的第%s天?'?%?dt.strftime('%j'))

#print:?今周是今年的第37周

print?('今周是今年的第%s周?'?%?dt.strftime('%U'))

上面代碼案例運(yùn)行結(jié)果如下:

atetime.max:9999-12-31?23:59:59.999999

datetime.min:0001-01-01?00:00:00

datetime.resolution:0:00:00.000001

today():2014-05-04?15:58:18.141186

now():2014-05-04?15:58:18.193146

utcnow():2014-05-04?07:58:18.243958

fromtimestamp(tmstmp):2014-05-04?15:58:18.291558

utcfromtimestamp(tmstmp):2014-05-04?07:58:18.342550

datetime.combine(date,time):?2012-09-12?19:46:05

2007-03-04?21:08:12

2012

9

12

21

8

12

None

2012-09-12

21:08:12

2013-09-12?21:08:12

time.struct_time(tm_year=2012,?tm_mon=9,?tm_mday=12,?tm_hour=21,?tm_min=8,?tm_sec=12,?tm_wday=2,?tm_yday=256,?tm_isdst=-1)

time.struct_time(tm_year=2012,?tm_mon=9,?tm_mday=12,?tm_hour=21,?tm_min=8,?tm_sec=12,?tm_wday=2,?tm_yday=256,?tm_isdst=0)

734758

2

(2012,?37,?3)

(%Y-%m-%d?%H:%M:%S?%f):?2014-05-04?15:58:19?326295

(%Y-%m-%d?%H:%M:%S?%p):?14-05-04?03:58:19?PM

%a:?Sun?

%A:?Sunday?

%b:?May?

%B:?May?

日期時(shí)間%c:?Sun?May??4?15:58:19?2014?

日期%x:05/04/14?

時(shí)間%X:15:58:19?

今天是這周的第0天?

今天是今年的第124天?

今周是今年的第18周

文章名稱:python中dt函數(shù) dt_add函數(shù)
分享地址:http://muchs.cn/article8/docdsop.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供App設(shè)計(jì)、全網(wǎng)營銷推廣、營銷型網(wǎng)站建設(shè)用戶體驗(yàn)、標(biāo)簽優(yōu)化電子商務(wù)

廣告

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

網(wǎng)站建設(shè)網(wǎng)站維護(hù)公司