Python有条件地解决延迟微分方程

我使用dde23包的pydelay来解决延迟微分方程。 我的问题:如何有条件地编码方程式? 例如,目标方程有两个选项:

 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. 方法1没有抱怨,但if else语句没有被解释。

  2. 方法2产生了以下错误:

    找到可执行文件c:\ mingw \ bin \ g ++。exe c:\ docume~1 \ thao \ locals~1 \ temp \ thong \ python27_compiled \ sc_f68f7a878bf7b27c6f72c9e771ec4d311.cpp:在函数’double f(double,double)’中:c:\ docume~1 \ thao \ locals~1 \ temp \ thong \ python27_compiled \ sc_f68f7a878bf7b27c6f72c9e771ec4d311.cpp:734:错误:’x’不能用作函数c:\ docume~1 \ thao \ locals~1 \ temp \ thong \ python27_compiled \ sc_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_compiled \ sc_f68f7a878bf7b27c6f72c9e771ec4d311.cpp:878:警告:有符号和无符号整数表达式之间的比较c:\ docume~1 \ thao \ locals~1 \ temp \ thong \ python27_compiled \ sc_f68f7a878bf7b27c6f72c9e771ec4d311.cpp:883:警告:有符号和无符号整数表达式之间的比较c:\ docume~1 \ thao \ locals~1 \ temp \ thong \ python27_c ompiled \ sc_f68f7a878bf7b27c6f72c9e771ec4d311.cpp:774:警告:未使用的变量’Nhistx_ar’c:\ docume~1 \ thao \ locals~1 \ temp \ thong \ python27_compiled \ sc_f68f7a878bf7b27c6f72c9e771ec4d311.cpp:775:警告:未使用的变量’Shistx_ar’c:\ docume~1 \ thao \ locals~1 \ temp \ thong \ python27_compiled \ sc_f68f7a878bf7b27c6f72c9e771ec4d311.cpp:776:警告:未使用的变量’Dhistx_ar’c:\ docume~1 \ thao \ locals~1 \ temp \ thong \ python27_compiled \ sc_f68f7a878bf7b27c6f72c9e771ec4d311。 cpp:785:警告:未使用的变量’NVhistx_ar’c:\ docume~1 \ thao \ locals~1 \ temp \ thong \ python27_compiled \ sc_f68f7a878bf7b27c6f72c9e771ec4d311.cpp:786:警告:未使用的变量’SVhistx_ar’c:\ docume~1 \ thao \ locals~1 \ temp \ thong \ python27_compiled \ sc_f68f7a878bf7b27c6f72c9e771ec4d311.cpp:787:警告:未使用的变量’DVhistx_ar’c:\ docume~1 \ thao \ locals~1 \ temp \ thong \ python27_compiled \ sc_f68f7a878bf7b27c6f72c9e771ec4d311.cpp:796:警告:未使用的变量’NThist_ar’c:\ docume~1 \ thao \ locals~1 \ temp \ thong \ python27_compiled \ sc_f68f7a878bf7b27c6f72 c9e771ec4d311.cpp:797:警告:未使用的变量’SThist_ar’c:\ docume~1 \ thao \ locals~1 \ temp \ thong \ python27_compiled \ sc_f68f7a878bf7b27c6f72c9e771ec4d311.cpp:798:警告:未使用的变量’DThist_ar’c:\ docume~ 1 \ thao \ locals~1 \ temp \ thong \ python27_compiled \ sc_f68f7a878bf7b27c6f72c9e771ec4d311.cpp:817:警告:未使用的变量’Ndiscont’c:\ docume~1 \ thao \ locals~1 \ temp \ thong \ python27_compiled \ sc_f68f7a878bf7b27c6f72c9e771ec4d311.cpp: 818:警告:未使用的变量’Sdiscont’c:\ docume~1 \ thao \ locals~1 \ temp \ thong \ python27_compiled \ sc_f68f7a878bf7b27c6f72c9e771ec4d311.cpp:819:警告:未使用的变量’Ddiscont’Trackback(最近一次调用最后一次):文件“C:\ Documents and Settings \ thao \ Desktop \ mackey-glass.py”,第33行,在dde.run()文件“C:\ Python27 \ lib \ site-packages \ pydelay_dde23.py”中,第1120行, run compiler =’gcc’)文件“C:\ Python27 \ lib \ site-packages \ scipy \ weave \ inline_tools.py”,第355行,内联** kw)文件“C:\ Python27 \ lib \ site-packages \ scipy \ weave \ inline_tools.py“,第482行,在compile_f中 unction verbose = verbose,** kw)文件“C:\ Python27 \ lib \ site-packages \ scipy \ weave \ ext_tools.py”,第367行,编译时verbose = verbose,** kw)文件“C:\ Python27 \ lib \ site-packages \ scipy \ weave \ build_tools.py“,第272行,在build_extension设置中(name = module_name,ext_modules = [ext],verbose = verb)文件”C:\ Python27 \ lib \ site-packages \ numpy \ distutils \ core.py“,第186行,在设置中返回old_setup(** new_attr)文件”C:\ Python27 \ lib \ distutils \ core.py“,第169行,在setup raise SystemExit中,”错误:“+ str(msg)distutils.errors.CompileError:error:命令“g ++ -O2 -Wall -IC:\ Python27 \ lib \ site-packages \ scipy \ weave -IC:\ Python27 \ lib \ site-packages \ scipy \ weave \ scxx -IC:\ Python27 \ lib \ site-packages \ numpy \ core \ include -IC:\ Python27 \ include -IC:\ Python27 \ PC -cc:\ docume~1 \ thao \ locals~1 \ temp \ thong \ python27_compiled \ sc_f68f7a878bf7b27c6f72c9e771ec4d311.cpp -oc:\ docume~1 \ thao \ locals~1 \ temp \ thong \ python27_intermediate \ compiler_a77d1132635f0379270bcb96a5e542fc \ Release \ docume~1 \ thao \ locals~1 \ temp \ thong \ python27_ 编译\ sc_f68f7a878bf7b27c6f72c9e771ec4d311.o“退出状态1失败[退出代码为1的5.8s完成]

方法1(使用if else语句更新eqns ,这是一个python dict):

 import numpy as np import pylab as pl from pydelay import dde23 eqn_1a='0.25 * x(t-tau) / (1.0 + pow(x(t-tau),10.0)) -0.1*x' eqn_1b='0.45 * x' eqns = { 'x' : eqn_1a if 'x>1' else eqn_1b} dde = dde23(eqns=eqns, params={'tau': 15}) 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(2, 3, 0.1) x1 = sol1['x'] 

方法2(向求解器提供c代码):

 eqns = { 'x' : 'f(x, tau)'} # We can define ac 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; } } """ 

尽管确实如此,但请注意它无法提供非常准确的解决方案。 这是由于x=1的不连续性。 更高级的求解器可以明确地处理这种不连续性(参见例如http://www.radford.edu/~thompson/ffddes/ )。

如果您想方便地使用pydelay求解器或只是为了获得概述,可以尝试将最大步长dtmax设置得足够小以减少错误。