Skip to main content
added 6 characters in body
Source Link
cvgmt
  • 84.1k
  • 6
  • 97
  • 179

Edit If

If we want to use v1 and v2,we can use With to enclose the WhenEvent

Edit If we want to use v1 and v2,we can use With to enclose WhenEvent

Edit

If we want to use v1 and v2,we can use With to enclose the WhenEvent

added 906 characters in body
Source Link
cvgmt
  • 84.1k
  • 6
  • 97
  • 179

Edit If we want to use v1 and v2,we can use With to enclose WhenEvent

Clear["Global`*"];
(*Preliminary definition*)ClearAll[ip, v1, v2];
ip[t_] := 
  Piecewise[{{Sin[2 π t/(2 0.1)], t < 0.1}, {0, 0.1 <= t}}];
(*The differential equations*)
eqn = {986. y[t] + 6. Derivative[1][y][t] + y''[t] == 
    986 ip[t] + 6. ip'[t], z''[t] == -9.81`, 
   With[{v1 = 0.9 (0.95 y'[t] + 0.15 z'[t]), 
     v2 = 0.9 (1.5 y'[t] - 0.4 z'[t])}, 
    WhenEvent[{z[t] - y[t] <= -0.125, 
      z[t] - y[t] >= 0.125}, {Derivative[1][y][t] -> v1, 
      Derivative[1][z][t] -> v2}]], y[0] == 0, 
   Derivative[1][y][0] == 0, z[0] == 0, Derivative[1][z][0] == 0};

(*Sove the equation*)
sol = First@NDSolve[eqn, {y, z}, {t, 0, 0.04}];

(*Plot the result*)
Plot[Evaluate[z[t] - y[t] /. sol], {t, 0, 0.04}, 
 Epilog -> {Pink, InfiniteLine[{0, -0.125}, {1, 0}]}]

enter image description here

Original

When we remove v1,v2, all of them work fine.

When we remove v1,v2, all of them work fine.

Edit If we want to use v1 and v2,we can use With to enclose WhenEvent

Clear["Global`*"];
(*Preliminary definition*)ClearAll[ip, v1, v2];
ip[t_] := 
  Piecewise[{{Sin[2 π t/(2 0.1)], t < 0.1}, {0, 0.1 <= t}}];
(*The differential equations*)
eqn = {986. y[t] + 6. Derivative[1][y][t] + y''[t] == 
    986 ip[t] + 6. ip'[t], z''[t] == -9.81`, 
   With[{v1 = 0.9 (0.95 y'[t] + 0.15 z'[t]), 
     v2 = 0.9 (1.5 y'[t] - 0.4 z'[t])}, 
    WhenEvent[{z[t] - y[t] <= -0.125, 
      z[t] - y[t] >= 0.125}, {Derivative[1][y][t] -> v1, 
      Derivative[1][z][t] -> v2}]], y[0] == 0, 
   Derivative[1][y][0] == 0, z[0] == 0, Derivative[1][z][0] == 0};

(*Sove the equation*)
sol = First@NDSolve[eqn, {y, z}, {t, 0, 0.04}];

(*Plot the result*)
Plot[Evaluate[z[t] - y[t] /. sol], {t, 0, 0.04}, 
 Epilog -> {Pink, InfiniteLine[{0, -0.125}, {1, 0}]}]

enter image description here

Original

When we remove v1,v2, all of them work fine.

Source Link
cvgmt
  • 84.1k
  • 6
  • 97
  • 179

When we remove v1,v2, all of them work fine.

(*Preliminary definition*)ClearAll[ip, v1, v2];
ip[t_] := 
  Piecewise[{{Sin[2 π t/(2 0.1)], t < 0.1}, {0, 0.1 <= t}}];
(*The differential equations*)
eqn = {986. y[t] + 6. Derivative[1][y][t] + y''[t] == 
    986 ip[t] + 6. ip'[t], z''[t] == -9.81`, 
   WhenEvent[{z[t] - y[t] <= -0.125, 
     z[t] - y[t] >= 0.125}, {Derivative[1][y][t] -> 
      0.9 (0.95 y'[t] + 0.15 z'[t]), 
     Derivative[1][z][t] -> 0.9 (1.5 y'[t] - 0.4 z'[t])}], y[0] == 0, 
   Derivative[1][y][0] == 0, z[0] == 0, Derivative[1][z][0] == 0};

(*Sove the equation*)
sol = First@NDSolve[eqn, {y, z}, {t, 0, 0.04}];

(*Plot the result*)
Plot[Evaluate[z[t] - y[t] /. sol], {t, 0, 0.04}, 
 Epilog -> {Pink, InfiniteLine[{0, -0.125}, {1, 0}]}]

enter image description here