Python Solve Delay Differential Equations Conditionally
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
Solution 1:
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;
}
}
"""
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/).
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.
Post a Comment for "Python Solve Delay Differential Equations Conditionally"