I want to solve this simple equation with a event:
sol = DSolve[{x'[t] == v[t], x[0] == 0, v[t] == 1,
WhenEvent[Evaluate[t == 2], {v[t] -> -v[t]}]}, {x[t], v[t]}, {t, 0,
10}]
Plot[x[t] /. sol, {t, 0, 4}]
Help appreciated. Thanks.
Mathematica Stack Exchange is a question and answer site for users of Wolfram Mathematica. It only takes a minute to sign up.
Sign up to join this communityI think you are missing the option DiscreteVariables
which tells NDSolve
which of the variables have to be understood as discrete variables, that is variables which only change due to WhenEvents
(see the documentation for DiscreteVariables
for details). For those, you have to provide an initial condition (v[0]==1.
) but no differential equation. The following code will do what I think you want:
sol = NDSolve[{
x'[t] == v[t],
x[0] == 0,
v[0] == 1,
WhenEvent[t == 2, v[t] -> -v[t]]
},
{x[t], v[t]},
{t, 0, 10},
DiscreteVariables -> {v}
]
Plot[x[t] /. sol, {t, 0, 10}]
NDSolve
$\endgroup$v[t] == 2
does not make sense because it never happens. Lets sayt==2
would be helpful, but I get this error:Unable to reinitialize the system at t = 2. within specified \ tolerances.
$\endgroup$