1
$\begingroup$

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[[2]] - x2[t]*xd[[1]] > 0.001, u[t] -> 0],
   WhenEvent[x1[t]*xd[[2]] - x2[t]*xd[[1]] < -0.001, u[t] -> 1],
   x1[0] == 1, x2[0] == 10, u[0] == 0},
  {x1, x2, u}, {t, 0, tmax}, DiscreteVariables -> {u}]

Plotting:

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

Simulation works well when L=C1=1.

$\endgroup$
3
$\begingroup$

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}]

Mathematica graphics Mathematica graphics

There are lots of events to detect!

$Version
(* 12.0.0 for Mac OS X x86 (64-bit) (April 7, 2019) *)
| improve this answer | |
$\endgroup$
  • $\begingroup$ 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. $\endgroup$ – kosa Sep 25 '19 at 8:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.