TransWikia.com

How can I use WhenEvent with multiple conditions inside NDSolve?

Mathematica Asked by Mariana Sá on April 22, 2021

I would like to use WhenEvent but I’m not getting what I want: I set the initial condition varepsilon=0;
I want varepsilon to change continuously over time, from to 0.1 if s'[t]>=0 or to -0.1 if s'[t]<0 whenever theta is greater than 2pi. I also need it to change to zero again when H[t] reaches the value Hf I define. Initially, H[t] is constant and equal to H0. When theta>=2pi, H[t] stops being constant. When Hf is reached, H[t] is constant and equal to Hf. Can someone help me? Thank you in advance.

 Hf=-0.4;
   sol = NDSolve[{s''[t] == -(1/s[t]) + 1^2/s[t]^3 + var[Epsilon][t], 
    var[Epsilon]'[t] == 0, [Theta]'[t] == 1/s[t]^2, 
    H'[t] == s'[t] var[Epsilon][t], s[0] == 1, 
    s'[0] == 0, [Theta][0] == 0, var[Epsilon][0] == 0, 
    H[0] == -(1/2), 
    WhenEvent[s'[t] > 0, 
     If[[Theta][t] >= 2 [Pi], var[Epsilon][t] -> 0.1]], 
    WhenEvent[s'[t] < 0, 
     If[[Theta][t] >= 2 [Pi], var[Epsilon][t] -> -0.1]], 
    WhenEvent[H[t] == Hf, var[Epsilon][t] -> 0]}, {s, s', [Theta],
     var[Epsilon], H},
   {t, 0, 100}, 
   Method -> {"ExplicitRungeKutta", "DifferenceOrder" -> 4}];

One Answer

I don't know if this really solves your problem, but I added a WhenEvent to get things started.

Hf = -0.4; 
sol = NDSolve[{Derivative[2][s][t] == -(1/s[t]) + 1^2/s[t]^3 + 
      var[Epsilon][t], 
         Derivative[1][var[Epsilon]][t] == 0, 
    Derivative[1][[Theta]][t] == 1/s[t]^2, 
         Derivative[1][H][t] == Derivative[1][s][t]*var[Epsilon][t], 
    s[0] == 1, 
         Derivative[1][s][0] == 0, [Theta][0] == 0, 
    var[Epsilon][0] == 0, H[0] == -(1/2), 
         WhenEvent[[Theta][t] >= 2*Pi, 
     If[Derivative[1][s][t] >= 0, var[Epsilon][t] -> 0.1]], 
         WhenEvent[Derivative[1][s][t] > 0, 
     If[[Theta][t] >= 2*Pi, var[Epsilon][t] -> 0.1]], 
         WhenEvent[Derivative[1][s][t] < 0, 
     If[[Theta][t] >= 2*Pi, var[Epsilon][t] -> -0.1]], 
         WhenEvent[H[t] == Hf, var[Epsilon][t] -> 0]}, {s, 
    Derivative[1][s], [Theta], var[Epsilon], H}, 
       {t, 0, 100}, 
   Method -> {"ExplicitRungeKutta", "DifferenceOrder" -> 4}]; 

Answered by Cassini on April 22, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP