3

Is is possible to use NDSolve with DiscreteVariables if there are no continuous-time variables?

This fails:

NDSolve[{x[0] == 1,
    WhenEvent[Mod[t, 1] == 0, x[t] -> -x[t]]}, {x}, {t, 0, 7}, 
    DiscreteVariables -> {x}]

It works if I remove the DiscreteVariables, replacing it with $x'(t)=0$:

NDSolveValue[{x[0] == 1, x'[t] == 0,
    WhenEvent[Mod[t, 1] == 0, x[t] -> -x[t]]}, {x}, {t, 0, 7}]

(This seems to defeat the purpose of DiscreteVariables.)

It works if I introduce a "dummy" continuous-time variable:

NDSolveValue[{y'[t] == 0, y[0] == 0, x[0] == 1, 
    WhenEvent[Mod[t, 1] == 0, x[t] -> -x[t]]}, {x, y}, {t, 0, 7}, 
    DiscreteVariables -> {x}]

(This seems like a hack.)

I ask because I have a system of equations

$$x_i(T_i^{k+1})=\sum_{j=1}^N a_{i,j} x_j(T_i^k), \quad i=1,\ldots,N$$

where each variable $x_i(t)$ changes only at the discrete times $\{T_i^k\}_{k \in \mathbb{N}}$.

Thoughts?

ConvexMartian
  • 1,641
  • 11
  • 18
  • 2
    I often use the dummy-variable "hack" to drive DAEs. -- Might be considered a design flaw, or one might think the D in NDSolve should mean something. :) – Michael E2 May 18 '16 at 00:40
  • 1
    I see what you mean, but if NDSolve isn't the right tool for this, is there a more appropriate function I'm overlooking? – ConvexMartian May 18 '16 at 03:52
  • I think it's the best available tool, even if it's not the best imaginable tool. NDSolve is the only built-in way to construct a discontinuous InterpolatingFunction (see here for a manual method). If if one knows ahead of time the times that the variable changes, one could directly construct a Piecewise function (see here or here).... – Michael E2 May 18 '16 at 12:07
  • ...I view your problem as an "differential-order-0" form of "integration" (in the sense of putting together a function). FunctionInterpolation takes a global approach like NIntegrate. NDSolve takes a local approach, but primarily for order > 0; however, via DAEs, it can simultaneously do order-0 integrations. I don't think there's an analog of FunctionInterpolation. – Michael E2 May 18 '16 at 12:07
  • I see. Thank you @MichaelE2 ! – ConvexMartian May 18 '16 at 13:35
  • Update: I discovered that WhenEvent also doesn't support time delays as discussed here. This made NDSolve seem ill-suited for my problem. I filed a bug/feature report with Wolfram and solved the system of equations "by hand". – ConvexMartian May 20 '16 at 13:18
  • Update: The Wolfram folks suggest using RecurrenceTable like this: RecurrenceTable[{x[t + 2] == -x[t], x[0] == 1, x[1] == 1}, x, {t, 0, 10}]. This addresses the NDSolve example above but not for the weird hybrid system of equations I wrote out in math. – ConvexMartian Jun 03 '16 at 18:28

0 Answers0