# Hybrid ODE simulation with very small parameters

I am trying to solve a Hybrid dynamical system using NDSolve and WhenEvent. I am able to simulate when the parameters are close to 1. However, the practical value of these parameters are close to 0.001. When I put in the actual values, one of two things happens (1) Mathematica crashes, (2) inaccurate simulation.

Parameters:

R = 0.05;G = .04;L = .001;C1 = .001;E1 = 300;Il = 1;us = 0.5;

xd = Inverse[{{R, 1 - us}, {1 - us, -G}}].{E1, 0};
tmax = 2;


Using NDSolve:

    sol = NDSolve[{
x1'[t] == -(R*x1[t] + (1 - u[t])*x2[t] - E1)/L,
x2'[t] == ((1 - u[t])*x1[t] - G*x2[t])/C1,
WhenEvent[x1[t]*xd[] - x2[t]*xd[] > 0.001, u[t] -> 0],
WhenEvent[x1[t]*xd[] - x2[t]*xd[] < -0.001, u[t] -> 1],
x1 == 1, x2 == 10, u == 0},
{x1, x2, u}, {t, 0, tmax}, DiscreteVariables -> {u}]


Plotting:

Plot[{xd[], x2[t] /. sol, x1[t] /. sol, xd[] - 5,xd[] + 5}, {t, 0, tmax}, PlotPoints -> 10000, PlotRange -> Full]
Plot[u[t] /. sol, {t, 0, tmax}, PlotPoints -> 2000]
Plot[{(x1[t]*xd[] - x2[t]*xd[]) /. sol}, {t, 0, tmax},PlotPoints -> 10000, PlotRange -> Full]


Simulation works well when L=C1=1.

Your code works for me with L = C1 = .001, it's just slow (326 seconds). Looking at the output shows why:

Plot[u[t] /. sol, {t, 0, tmax}, PlotPoints -> 2000]
Plot[u[t] /. sol, {t, tmax - 0.01, tmax}]  There are lots of events to detect!

\$Version
(* 12.0.0 for Mac OS X x86 (64-bit) (April 7, 2019) *)

• Hi, You are correct, it works well. However, I posted the wrong system yesterday. It works well because it is a linear system. I made the correction to the current system. Same issues, if L=C1=1, every simulates fine. But when put L=C1=0.001. It does not work. – kosa Sep 25 '19 at 8:54