Python有条件地求解延迟微分方程 [英] Python solve delay differential equations conditionally

查看:331
本文介绍了Python有条件地求解延迟微分方程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 pydelay 软件包的 dde23 来求解延迟微分方程。我的问题:如何有条件地编写方程式?例如,目标方程式有两个选择:

I am using dde23 of pydelay package to solve a delay differential equation. My question: How to code a equation conditionally? For example the target equation has two options:

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

我尝试了两种方法,但似乎没有一个起作用:

I tried two approaches, but it seems like neither one worked:


  1. 方法1没有抱怨,但是if else语句没有被解释

  1. Approach 1 did not complain, but it the if else statement has not been interpreted.

方法2产生了以下错误:

Approach 2 generated the following errors:

找到可执行文件c:\mingw\bin\g ++。exe
c:\docume〜1\thao\locals〜1\temp\ \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_f68f7a878bf7b27c6f72c9e771ec4: 734:错误: x不能用作函数
c:\docume〜1\thao\lo cals〜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_compiled\ sc_f68f7a878bf7b27c6f72c9e771ec4d311.cpp:774:警告:未使用的变量'Nhistx_ar'
c:\docume〜1\thao\locals〜1\temp\thong\python27_compiled\sc_f68f7a878bf7b27c6f72c9:771.7714未使用的变量'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\thon g\python27_compiled\sc_f68f7a878bf7b27c6f72c9e771ec4d311.cpp:785:警告:未使用的变量'NVhistx_ar'
c:\docume〜1\thao\locals〜1\temp\thong\python27_compiled\sc_f68f7a878bfb27d 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: umedocume〜1\thao\locals〜1\temp\thong\python27_compiled\sc_f68f7a878bf7b27c6f72c9e771ec4d311.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 \python27_compiled\sc_f68f7a878bf7b27c6f72c9e771ec4d311.cpp:817:警告:未使用的变量'Ndi scont'
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'
回溯(最近一次调用):
文件 C:\Documents and Settings\thao\Desktop\mackey-glass.py,第33行,在
dde.run()中
文件 C:\Python27 \lib\site-packages\pydelay_dde23.py,行1120,正在运行
编译器='gcc')
文件 C:\Python27\lib\site\packages\ \scipy\weave\inline_tools.py,行355,内联
** kw)
文件 C:\Python27\lib\site-packagesscipy\weave inline_tools.py,第482行,在compile_function中
verbose = verbose,** kw)
文件 C:\Python27\lib\site-packagesscipy\weave\ \ext_tools.py,行367,在编译
中verbose = verbose,** kw)
文件 C:\Python27\lib\site-packages\scipy weave\build_tools.py,第272行,位于build_extension
设置中(名称= module_name,ext_modules = [ext],verbose = verb)
文件 C:\Python27\lib\ 站点包 numpy distutils core.py,第186行,在安装程序
中返回old_setup(** new_attr)
文件 C:\Python27\lib\distutils \core.py,行169,在安装程序
中引发SystemExit,错误: + str(msg)
distutils.errors.CompileError:错误:命令 g ++ -O2 -Wall -IC: \Python27\lib\站点包\scipy\weave -IC:\Python27\lib\站点-软件包\scipy\weave\scxx -IC:\Python27\lib\站点包-numpy核心-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 \locals〜1\temp\thong\python27_compiled\sc_f68f7a878bf7b27c6f72c9e771ec4d311.o失败,退出状态为1
[在5.8s中完成,退出代码为1]

Found executable c:\mingw\bin\g++.exe c:\docume~1\thao\locals~1\temp\thong\python27_compiled\sc_f68f7a878bf7b27c6f72c9e771ec4d311.cpp: In function 'double f(double, double)': c:\docume~1\thao\locals~1\temp\thong\python27_compiled\sc_f68f7a878bf7b27c6f72c9e771ec4d311.cpp:734: error: 'x' cannot be used as a function c:\docume~1\thao\locals~1\temp\thong\python27_compiled\sc_f68f7a878bf7b27c6f72c9e771ec4d311.cpp:734: error: 'x' cannot be used as a function c:\docume~1\thao\locals~1\temp\thong\python27_compiled\sc_f68f7a878bf7b27c6f72c9e771ec4d311.cpp: In function 'PyObject* compiled_func(PyObject*, PyObject*)': c:\docume~1\thao\locals~1\temp\thong\python27_compiled\sc_f68f7a878bf7b27c6f72c9e771ec4d311.cpp:878: warning: comparison between signed and unsigned integer expressions c:\docume~1\thao\locals~1\temp\thong\python27_compiled\sc_f68f7a878bf7b27c6f72c9e771ec4d311.cpp:883: warning: comparison between signed and unsigned integer expressions c:\docume~1\thao\locals~1\temp\thong\python27_compiled\sc_f68f7a878bf7b27c6f72c9e771ec4d311.cpp:774: warning: unused variable 'Nhistx_ar' c:\docume~1\thao\locals~1\temp\thong\python27_compiled\sc_f68f7a878bf7b27c6f72c9e771ec4d311.cpp:775: warning: unused variable 'Shistx_ar' c:\docume~1\thao\locals~1\temp\thong\python27_compiled\sc_f68f7a878bf7b27c6f72c9e771ec4d311.cpp:776: warning: unused variable 'Dhistx_ar' c:\docume~1\thao\locals~1\temp\thong\python27_compiled\sc_f68f7a878bf7b27c6f72c9e771ec4d311.cpp:785: warning: unused variable 'NVhistx_ar' c:\docume~1\thao\locals~1\temp\thong\python27_compiled\sc_f68f7a878bf7b27c6f72c9e771ec4d311.cpp:786: warning: unused variable 'SVhistx_ar' c:\docume~1\thao\locals~1\temp\thong\python27_compiled\sc_f68f7a878bf7b27c6f72c9e771ec4d311.cpp:787: warning: unused variable 'DVhistx_ar' c:\docume~1\thao\locals~1\temp\thong\python27_compiled\sc_f68f7a878bf7b27c6f72c9e771ec4d311.cpp:796: warning: unused variable 'NThist_ar' c:\docume~1\thao\locals~1\temp\thong\python27_compiled\sc_f68f7a878bf7b27c6f72c9e771ec4d311.cpp:797: warning: unused variable 'SThist_ar' c:\docume~1\thao\locals~1\temp\thong\python27_compiled\sc_f68f7a878bf7b27c6f72c9e771ec4d311.cpp:798: warning: unused variable 'DThist_ar' c:\docume~1\thao\locals~1\temp\thong\python27_compiled\sc_f68f7a878bf7b27c6f72c9e771ec4d311.cpp:817: warning: unused variable 'Ndiscont' c:\docume~1\thao\locals~1\temp\thong\python27_compiled\sc_f68f7a878bf7b27c6f72c9e771ec4d311.cpp:818: warning: unused variable 'Sdiscont' c:\docume~1\thao\locals~1\temp\thong\python27_compiled\sc_f68f7a878bf7b27c6f72c9e771ec4d311.cpp:819: warning: unused variable 'Ddiscont' Traceback (most recent call last): File "C:\Documents and Settings\thao\Desktop\mackey-glass.py", line 33, in dde.run() File "C:\Python27\lib\site-packages\pydelay_dde23.py", line 1120, in run compiler = 'gcc') File "C:\Python27\lib\site-packages\scipy\weave\inline_tools.py", line 355, in inline **kw) File "C:\Python27\lib\site-packages\scipy\weave\inline_tools.py", line 482, in compile_function verbose=verbose, **kw) File "C:\Python27\lib\site-packages\scipy\weave\ext_tools.py", line 367, in compile verbose = verbose, **kw) File "C:\Python27\lib\site-packages\scipy\weave\build_tools.py", line 272, in build_extension setup(name = module_name, ext_modules = [ext],verbose=verb) File "C:\Python27\lib\site-packages\numpy\distutils\core.py", line 186, in setup return old_setup(**new_attr) File "C:\Python27\lib\distutils\core.py", line 169, in setup raise SystemExit, "error: " + str(msg) distutils.errors.CompileError: error: Command "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 -c c:\docume~1\thao\locals~1\temp\thong\python27_compiled\sc_f68f7a878bf7b27c6f72c9e771ec4d311.cpp -o c:\docume~1\thao\locals~1\temp\thong\python27_intermediate\compiler_a77d1132635f0379270bcb96a5e542fc\Release\docume~1\thao\locals~1\temp\thong\python27_compiled\sc_f68f7a878bf7b27c6f72c9e771ec4d311.o" failed with exit status 1 [Finished in 5.8s with exit code 1]



方法1(使用if else语句更新 eqns 是python字典):



Approach 1 (use the if else statement to update eqns which is a 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代码):



Approach 2 (provide c code to the solver):

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']


推荐答案

第二种方法只有一个小改动。

The second approach works with one minor change. The value of the delayed variable has to be passed as an argument to the function directly.

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

Although this does run, note that it cannot give a very accurate solution. This is due to the discontinuity at x=1. More advanced solvers can explicitly handle such discontinuities (see e.g. http://www.radford.edu/~thompson/ffddes/).

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

If you want to stick with the pydelay solver for convenience or just to get an overview, you can try to set the maximum step size dtmax small enough to reduce the error.

这篇关于Python有条件地求解延迟微分方程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆