python中dt函数 dt_add函数

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

import

成都创新互联于2013年成立,先为东昌府等服务建站,东昌府等地企业,进行企业商务咨询服务。为东昌府企业网站制作PC+手机+微官网三网同步一站式服务解决您的所有建站问题。

math

a,b,c

=

input("请输入3个数字(空格分隔):").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("有无穷个解")elif

d

=

0:

x1

=

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

x2

=

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

print('结果为:%.2f,%.2f'%(x1,x2));

else:

print("无解")

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

其他参数,官方网站和文档都没有明确说明。相关的资料,暂时也找不到。

python datetime处理时间

python时间处理方法datetime(),下面就举几个代码案例进行说明,代码如下:

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

# 运行环境:Python3.4

#datetime类

#datetime是date与time的结合体,包括date与time的所有信息。

#它的构造函数如下:

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

#各参数的含义与date、time的构造函数中的一样,要注意参数值的范围。

# 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():返回一个表示当前本地时间的datetime对象;

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

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

#datetime.now([tz]):返回一个表示当前本地时间的datetime对象,如果提供了参数tz,则获取tz参数所指时区的本地时间;

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

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

#datetime.utcnow():返回一个当前utc时间的datetime对象;

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

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

#datetime.fromtimestamp(timestamp[, tz]):根据时间戮创建一个datetime对象,参数tz指定时区信息;

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

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

#datetime.utcfromtimestamp(timestamp):根据时间戮创建一个datetime对象;

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

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

#datetime.combine(date, time):根据date和time,创建一个datetime对象;

#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):将格式字符串转换为datetime对象;

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

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

#2. datetime类提供的实例方法与属性

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 ():返回一个日期时间的C格式字符串,等效于time.ctime(time.mktime(dt.timetuple()));

#3. 格式字符串

# datetime. strftime (format)

# %a 星期的简写。如 星期三为Web

# %A 星期的全写。如 星期三为Wednesday

# %b 月份的简写。如4月份为Apr

# %B月份的全写。如4月份为April 

# %c:  日期时间的字符串表示。(如: 04/07/10 10:43:39)

# %d:  日在这个月中的天数(是这个月的第几天)

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

# %H:  小时(24小时制,[0, 23])

# %I:  小时(12小时制,[0, 11])

# %j:  日在年中的天数 [001,366](是当年的第几天)

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

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

# %p:  AM或者PM

# %S:  秒(范围为[00,61],为什么不是[00, 59],参考python手册~_~)

# %U:  周在当年的周数当年的第几周),星期天作为周的第一天

# %w:  今天在这周的天数,范围为[0, 6],6表示星期天

# %W:  周在当年的周数(是当年的第几周),星期一作为周的第一天

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

# %X:  时间字符串(如:10:43:39)

# %y:  2个数字表示的年份

# %Y:  4个数字表示的年份

# %z:  与utc时间的间隔 (如果是本地时间,返回空字符串)

# %Z:  时区名称(如果是本地时间,返回空字符串)

# %%:  %% = %

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: 日期时间%c: 09/12/12 23:04:27

print ('日期时间%%c: %s ' % dt.strftime('%c'))

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

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

#print: 时间%X:23:04:27

print ('时间%%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'))

上面代码案例运行结果如下:

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 

日期时间%c: Sun May  4 15:58:19 2014 

日期%x:05/04/14 

时间%X:15:58:19 

今天是这周的第0天 

今天是今年的第124天 

今周是今年的第18周

文章名称:python中dt函数 dt_add函数
分享地址:https://www.cdcxhl.com/article8/docdsop.html

成都网站建设公司_创新互联,为您提供App设计全网营销推广营销型网站建设用户体验标签优化电子商务

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

网站建设网站维护公司