Group Abstract Group Abstract

Message Boards Message Boards

What is wrong with WhenEvent ?

Posted 2 days ago

I posted this question on StaxExchange, and while I received a couple of responses, I'm not satisfied. In the attached very simple code, when WhenEvent is not present, the computation runs smoothly. When WhenEvent is present, which only checks on the time value and does not address values of the dependent variables, the error is generated which makes (to me) no sense. How could the presence of such WhenEvent operator spoil the code in the 1st place ?

Could someone please tell me what is going on.

POSTED BY: Mikhail Khenner
2 Replies

It seems a simple syntax error. The syntax for NDSolve[] for an ODE system is

NDSolve[system, Vars, time, opts...]

Here system is to include all the differential equations, boundary/initial conditions, and WhenEvent[] events of the system. And Vars specifies the dependent variables and their output forms, and time specifies the independent variable and domain limits, which is {t, t0, tf} in your code. Your NDSolve call, if the comment signs (*, *) are removed, has the form

NDSolve[system, WhenEvent[...], Vars, time, opts...]

The WhenEvent code is not part of the system but a separate argument, the second argument. For NDSolve[], the second argument is taken to be a dependent variable output specification. The WhenEvent[...] code is not a good dependent variable specification at all, and the error message is a bit confused by it. The message contains "the arguments of the dependent variable in...", which is about the best hint to what is going wrong: There is something wrong with the second argument that specifies the dependent variable output. (NDSolve[] stops here; otherwise I would expect errors arising from Vars being the independent variable and domain spec.) The WhenEvent[...] should be made part of the system, which you call ODEandICTable in your notebook.

POSTED BY: Michael Rogers

The WhenEvent must be in the same list as the equations, for example this way:

Append[ODEandICTable, WhenEvent[Mod[t, 1] == 0, Print["t =", t]]]

The error message makes little sense, but so does the input.

POSTED BY: Gianluca Gorni
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard