Yet another question about solving an ODE with inputs using DifferentialEquations

This is yet another question related to the obviously popular basic problem of solving an ODE (or a system of ODEs) with inputs in Julia (using DifferentialEquations package).

Let’s consider a single (scalar) ODE of the form

dx/dt = f(x,u),

where x(0) and u(t) for t in [0,tmax] are specified. A simplest possible example is

dx/dt = -x(t) + u(t),

where x(0)=1 and u(t) = 0 for t below (before) t=1 and it equals 1 afterwards, that is, for t>=1 (shifted/delayed Heavyside step function).

A MWE in Julia is (after changing the notation to the one favoured by the DifferentialEquations, namely, x is relabelled to u and the old u turns into a parameter p):

using DifferentialEquations
using Plots
pyplot()

f(u,p,t) = -1.0*u + 1.0*p(t)

u0 = 1.0
p(t) = (1-t) <= 0 ? 1 : 0
tspan = (0.0,10.0)

prob = ODEProblem(f,u0,tspan,p)
sol = solve(prob)

plot(sol)

The example seems perfectly functional. However, looking at the definition of f above, that is, f(u,p,t) = -1.0*u + 1.0*p(t), it cannot be unnoticed that u is called without the argument t whereas p needs to have it, that is, it appears as p(t). Not a problem, it is just that while still learning basics of Julia, I was wondering if the code could be modified so that p is handled in the same way as the variable u, that is, without appending the (t).

Perhaps this might be more related to the basics of Julia rather than DifferentialEquations (I still find myself rather clumsy with anonymous functions and stuff).

You could put a DSL over it, but intrinsically at the bottom this is what it looks like in any modeling language after transformation.

1 Like