Python有条件地解延迟微分方程
Python有条件地解延迟微分方程,python,c,scipy,differential-equations,Python,C,Scipy,Differential Equations,我正在使用pydelay包的dde23来解一个延迟微分方程。我的问题是:如何有条件地编写方程式?例如,目标方程有两个选项:
when x>1, dx/dt=0.25 * x(t-tau) / (1.0 + pow(x(t-tau),10.0)) -0.1*x
otherwise, dx/dt=0.25 * x
我尝试了两种方法,但似乎两种方法都不起作用:
方法1没有抱怨,但if-else语句没有抱怨
已经解释过了
方法2产生了以下错误:
找到可执行文件c:\mingw\bin\g++.e
我正在使用pydelay
包的dde23
来解一个延迟微分方程。我的问题是:如何有条件地编写方程式?例如,目标方程有两个选项:
when x>1, dx/dt=0.25 * x(t-tau) / (1.0 + pow(x(t-tau),10.0)) -0.1*x
otherwise, dx/dt=0.25 * x
我尝试了两种方法,但似乎两种方法都不起作用:
方法1没有抱怨,但if-else语句没有抱怨
已经解释过了
方法2产生了以下错误:
找到可执行文件c:\mingw\bin\g++.exe
c:\docume~1\thao\locals~1\temp\thong\python27\u compiled\sc_f68f7a878bf7b27c6f72c9e771ec4d311.cpp:在函数“double f(double,double)”中:
c:\docume~1\thao\locals~1\temp\thong\python27\u compiled\sc\u f68f7a878bf7b27c6f72c9e771ec4d311。cpp:734:错误:“x”不能用作函数
c:\docume~1\thao\locals~1\temp\thong\python27\u compiled\sc\u f68f7a878bf7b27c6f72c9e771ec4d311。cpp:734:错误:“x”不能用作函数
c:\docume~1\thao\locals~1\temp\thong\python27_compiled\sc_f68f7a878bf7b27c6f72c9e771ec4d311.cpp:在函数“PyObject*compiled_func(PyObject*,PyObject*)”中:
c:\docume~1\thao\locals~1\temp\thong\python27\u compiled\sc_f68f7a878bf7b27c6f72c9e771ec4d311。cpp:878:警告:有符号和无符号整数表达式之间的比较
c:\docume~1\thao\locals~1\temp\thong\python27\u compiled\sc_f68f7a878bf7b27c6f72c9e771ec4d311.cpp:883:警告:有符号和无符号整数表达式之间的比较
c:\docume~1\thao\locals~1\temp\thong\python27\u compiled\sc\u f68f7a878bf7b27c6f72c9e771ec4d311.cpp:774:警告:未使用的变量“Nhistx\u ar”
c:\docume~1\thao\locals~1\temp\thong\python27\u compiled\sc_f68f7a878bf7b27c6f72c9e771ec4d311.cpp:775:警告:未使用的变量'Shistx\u ar'
c:\docume~1\thao\locals~1\temp\thong\python27\u compiled\sc\u f68f7a878bf7b27c6f72c9e771ec4d311.cpp:776:警告:未使用的变量“Dhistx\u ar”
c:\docume~1\thao\locals~1\temp\thong\python27\u compiled\sc\u f68f7a878bf7b27c6f72c9e771ec4d311.cpp:785:警告:未使用的变量“NVhistx\u ar”
c:\docume~1\thao\locals~1\temp\thong\python27\u compiled\sc\u f68f7a878bf7b27c6f72c9e771ec4d311.cpp:786:警告:未使用的变量“SVhistx\u ar”
c:\docume~1\thao\locals~1\temp\thong\python27\u compiled\sc\u f68f7a878bf7b27c6f72c9e771ec4d311.cpp:787:警告:未使用的变量“DVhistx\u ar”
c:\docume~1\thao\locals~1\temp\thong\python27\u compiled\sc\u f68f7a878bf7b27c6f72c9e771ec4d311.cpp:796:警告:未使用的变量“NThist\u ar”
c:\docume~1\thao\locals~1\temp\thong\python27\u compiled\sc\u f68f7a878bf7b27c6f72c9e771ec4d311.cpp:797:警告:未使用的变量“SThist\u ar”
c:\docume~1\thao\locals~1\temp\thong\python27\u compiled\sc\u f68f7a878bf7b27c6f72c9e771ec4d311.cpp:798:警告:未使用的变量“DThist\u ar”
c:\docume~1\thao\locals~1\temp\thong\python27\u compiled\sc\u f68f7a878bf7b27c6f72c9e771ec4d311.cpp:817:警告:未使用的变量“Ndiscont”
c:\docume~1\thao\locals~1\temp\thong\python27\u compiled\sc\u f68f7a878bf7b27c6f72c9e771ec4d311.cpp:818:警告:未使用的变量“Sdiscont”
c:\docume~1\thao\locals~1\temp\thong\python27\u compiled\sc\u f68f7a878bf7b27c6f72c9e771ec4d311.cpp:819:警告:未使用的变量“Ddiscont”
回溯(最近一次呼叫最后一次):
文件“C:\Documents and Settings\thao\Desktop\mackey glass.py”,第33行,在
dde.run()
文件“C:\Python27\lib\site packages\pydelay\u dde23.py”,第1120行,正在运行
编译器='gcc')
文件“C:\Python27\lib\site packages\scipy\weave\inline\u tools.py”,第355行,内联
**千瓦)
文件“C:\Python27\lib\site packages\scipy\weave\inline\u tools.py”,第482行,在compile\u函数中
详细=详细,**千瓦)
文件“C:\Python27\lib\site packages\scipy\weave\ext\u tools.py”,第367行,编译
详细=详细,**千瓦)
文件“C:\Python27\lib\site packages\scipy\weave\build\u tools.py”,第272行,内部版本扩展名
设置(名称=模块名称,外部模块=[ext],详细=动词)
文件“C:\Python27\lib\site packages\numpy\distutils\core.py”,第186行,在安装程序中
返回旧的\u设置(**新的\u属性)
文件“C:\Python27\lib\distutils\core.py”,第169行,在安装程序中
raise SystemExit,“错误:+str(消息)
distutils.errors.CompileError:error:Command"g++-O2-Wall-IC:\Python27\lib\site packages\scipy\weave-IC:\Python27\lib\site packages\scpy\weave\scxx-IC:\Python27\lib\site packages\numpy\core\include-IC:\Python27\include-IC:\Python27\PC-c:\docume~1\thao\locals~1\temp\thong\Python27\scu编译的\sc\u f68f7a878bf7bf7b27c6f72c9e771ec411ed311.cpp-oc:\docume~1\thao\locals~1\temp\thong\python27\u intermediate\compiler\u A777D1132635F0379270BCB96A5E542FC\Release\docume~1\thao\locals~1\temp\thong\python27\u compiled\sc\u F68F7A878BF7B27C6F7C9E771EC4D311.o“失败,退出状态为1”
[在5.8s内完成,退出代码为1]
方法1(使用if-else语句更新eqns
,这是一个python dict):
方法2(向解算器提供c代码):
第二种方法只做了一个小改动,延迟变量的值必须作为参数直接传递给函数
eqns = { 'x' : 'f(x, x(t - tau))'}
mycode = """
double f(double x, double x_tau) {
if (x > 1.0){
return 0.25 * x_tau / (1.0 + pow(x_tau, 10.0)) -0.1*x;
}
else{
return 0.45 * x;
}
}
"""
虽然这确实运行,但请注意,它不能给出非常精确的解。这是由于x=1
处的不连续性造成的。更高级的解算器可以显式处理此类不连续性(参见示例)
如果您想继续使用pydelay解算器以方便使用或只是为了获得概览,可以尝试将最大步长dtmax
设置得足够小以减少错误
eqns = { 'x' : 'f(x, tau)'}
# We can define a c function to be used in the equations
mycode = """
double f(double x, double tau) {
if (x>1){
return (0.25 * x(t-tau) / (1.0 + pow(x(t-tau),10.0)) -0.1*x);
}
else{
return (0.45 * x);
}
}
"""
dde = dde23(eqns=eqns, params={'tau': 15}, supportcode=mycode)
dde.set_sim_params(tfinal=1000, dtmax=1.0, AbsTol=10**-6, RelTol=10**-3)
histfunc = {'x': lambda t: 0.5 }
dde.hist_from_funcs(histfunc, 51)
dde.run()
sol1 = dde.sample(1, 300, 1)
x1 = sol1['x']
eqns = { 'x' : 'f(x, x(t - tau))'}
mycode = """
double f(double x, double x_tau) {
if (x > 1.0){
return 0.25 * x_tau / (1.0 + pow(x_tau, 10.0)) -0.1*x;
}
else{
return 0.45 * x;
}
}
"""