1
$\begingroup$

I am using Animate to animate the result of a system of coupled differential equations as I vary an initial condition A:

l = 1.0;
m = 1.0;
g = 10.0;
h = 10;
u = 0.05;    
Animate[sol3 = 
      NDSolve[{m*
          x''[t] == ((((m*g)/l)*(l/2 - (x[t]))*k[t]*
             u) - (((m*g)/l)*(l/2 + (x[t])))*j[t]*u), 
        WhenEvent[x[t] == 0, {Tn[t] -> t, V0[t] -> Abs[x'[t]]}], 
        WhenEvent[
         t == Tn[t] + ((3.14*(2*l/(u*g))^0.5)/
             4) + ((3.0*V0[t]*h)/(4*g*(l/2 - r[t]))) + 0.01, 
         x'[t] -> 
          x'[t]*(1 - (((V0[t] + 1)*h*u)/(20*(1 + 1.2*V0[t])*Abs[x'[t]]*2*
                 l)))], WhenEvent[x'[t] == 0, r[t] -> Abs[x[t]]], 
        x[0] == A, x'[0] == 0, V0[0] == 0, Tn[0] == 0, 
        a[t] == Piecewise[{{0, 
            Tn[t] + ((3.14*(2*l/(u*g))^0.5)/
                4) + ((3*V0[t]*h)/(4*g*(l/2 - r[t]))) > t > 
             Tn[t] + ((3.14*(2*l/(u*g))^0.5)/
                4) - ((1*V0[t]*h)/(4*g*(l/2 - r[t])))}, {1, 
            t < Tn[t] + ((3.14*(2*l/(u*g))^0.5)/
                4) - ((1*V0[t]*h)/(4*g*(l/2 - r[t])))}, {1, 
            t > Tn[t] + ((3.14*(2*l/(u*g))^0.5)/
                4) + ((3*V0[t]*h)/(4*g*(l/2 - r[t])))}}], 
        WhenEvent[x[t] <= 0, b[t] -> 1], WhenEvent[x[t] > 0, b[t] -> 0], 
        WhenEvent[x[t] <= 0, c[t] -> 0], WhenEvent[x[t] > 0, c[t] -> 1], 
        d[t] == a[t] + b[t], f[t] == a[t] + c[t], k[t] == Tanh[100*d[t]], 
        j[t] == Tanh[100*f[t]]}, {a, x, Tn, V0, b, c, d, f, k, j, r}, {t, 
        0, 100}, DiscreteVariables -> {Tn, V0, b, c, r}]; 
     Plot[x[t] /. sol3[[1]], {t, 0, 100}, PlotRange -> All], {A, 0.03, 
      0.045}]

However, the result I obtain by doing this is an extremely sloppy and non smooth animation. What could I do to make this animation smooth?

$\endgroup$
3
$\begingroup$

When you use a complicated calculation for an animation, you get smoother plots with ListAnimate because you cache the plots first.

l = 1.0;
m = 1.0;
g = 10.0;
h = 10;
u = 0.05;

sol3 := NDSolve[{m*
     x''[t] == ((((m*g)/l)*(l/2 - (x[t]))*k[t]*
        u) - (((m*g)/l)*(l/2 + (x[t])))*j[t]*u), 
   WhenEvent[x[t] == 0, {Tn[t] -> t, V0[t] -> Abs[x'[t]]}], 
   WhenEvent[
    t == Tn[t] + ((3.14*(2*l/(u*g))^0.5)/
        4) + ((3.0*V0[t]*h)/(4*g*(l/2 - r[t]))) + 0.01, 
    x'[t] -> 
     x'[t]*(1 - (((V0[t] + 1)*h*u)/(20*(1 + 1.2*V0[t])*Abs[x'[t]]*2*
            l)))], WhenEvent[x'[t] == 0, r[t] -> Abs[x[t]]], 
   x[0] == A, x'[0] == 0, V0[0] == 0, Tn[0] == 0, 
   a[t] == Piecewise[{{0, 
       Tn[t] + ((3.14*(2*l/(u*g))^0.5)/
           4) + ((3*V0[t]*h)/(4*g*(l/2 - r[t]))) > t > 
        Tn[t] + ((3.14*(2*l/(u*g))^0.5)/
           4) - ((1*V0[t]*h)/(4*g*(l/2 - r[t])))}, {1, 
       t < Tn[t] + ((3.14*(2*l/(u*g))^0.5)/
           4) - ((1*V0[t]*h)/(4*g*(l/2 - r[t])))}, {1, 
       t > Tn[t] + ((3.14*(2*l/(u*g))^0.5)/
           4) + ((3*V0[t]*h)/(4*g*(l/2 - r[t])))}}], 
   WhenEvent[x[t] <= 0, b[t] -> 1], WhenEvent[x[t] > 0, b[t] -> 0], 
   WhenEvent[x[t] <= 0, c[t] -> 0], WhenEvent[x[t] > 0, c[t] -> 1], 
   d[t] == a[t] + b[t], f[t] == a[t] + c[t], k[t] == Tanh[100*d[t]], 
   j[t] == Tanh[100*f[t]]}, {a, x, Tn, V0, b, c, d, f, k, j, r}, {t, 
   0, 100}, DiscreteVariables -> {Tn, V0, b, c, r}]

tb = Table[
   Plot[Evaluate[x[t] /. sol3[[1]]], {t, 0, 100}, 
    PlotRange -> {-.4, .4}], {A, 0.03, 0.045, .0002}];

ListAnimate[tb]

enter image description here

Also, when you use PlotRange->All with an animation, the scale adjusts itself with each frame. It is better to use a fixed PlotRange. Notice I also used := because NDSolve requires all variables to be defined, and A is not defined until the Plot command. This method may use more memory than the Animate command because each frame is cached in advance, but it animates much smoother than Animate which must calculate each frame as it is plotting.

$\endgroup$
5
$\begingroup$

You can use ParametricNDSolveValue to create a list of parametric interpolation functions using A as the parameter:

pndsv = ParametricNDSolveValue[{m*x''[t] == ((((m*g)/l)*(l/2 - (x[t]))*k[t]* u) - 
      (((m*g)/l)*(l/2 + (x[t])))*j[t]*u), 
    WhenEvent[x[t] == 0, {Tn[t] -> t, V0[t] -> Abs[x'[t]]}], 
    WhenEvent[t == Tn[t] + ((3.14*(2*l/(u*g))^0.5)/4) +
     ((3.0*V0[t]*h)/(4*g*(l/2 - r[t]))) + 0.01, 
     x'[t] ->  x'[t]*(1 - (((V0[t] + 1)*h*u)/(20*(1 + 1.2*V0[t])*Abs[x'[t]]*2* l)))], 
    WhenEvent[x'[t] == 0, r[t] -> Abs[x[t]]], 
    x[0] == A, x'[0] == 0, V0[0] == 0, Tn[0] == 0, 
    a[t] == Piecewise[{{0, Tn[t] + ((3.14*(2*l/(u*g))^0.5)/4) + 
       ((3*V0[t]*h)/(4*g*(l/2 - r[t]))) > t > Tn[t] +
       ((3.14*(2*l/(u*g))^0.5)/4) - ((1*V0[t]*h)/(4*g*(l/2 - r[t])))},
      {1, t < Tn[t] + ((3.14*(2*l/(u*g))^0.5)/ 4) - ((1*V0[t]*h)/(4*g*(l/2 - r[t])))}, 
      {1, t > Tn[t] + ((3.14*(2*l/(u*g))^0.5)/4) +
        ((3*V0[t]*h)/(4*g*(l/2 - r[t])))}}], 
    WhenEvent[x[t] <= 0, b[t] -> 1], WhenEvent[x[t] > 0, b[t] -> 0], 
    WhenEvent[x[t] <= 0, c[t] -> 0], WhenEvent[x[t] > 0, c[t] -> 1], 
    d[t] == a[t] + b[t], f[t] == a[t] + c[t], k[t] == Tanh[100*d[t]], 
    j[t] == Tanh[100*f[t]]},
    {a, x, Tn, V0, b, c, d, f, k, j, r}, 
    {t, 0, 100}, 
    {A}, 
    DiscreteVariables -> {Tn, V0, b, c, r}];

it is impossible to distinguish the functions when you plot all 11 functions together:

Manipulate[Plot[Evaluate@Through@pndsv[A][t], {t, 0, 100}, PlotRange -> All, 
   PlotLegends -> {a, x, Tn, V0, b, c, d, f, k, j, r}], 
 {A, 0.03, 0.045}]

enter image description here

An alternative approach is to plot the functions separately in grid:'

Animate[GraphicsGrid[
  Partition[#, 4, 4, 1, {}] &@
   MapThread[Plot[#@t, {t, 0, 100}, PlotStyle -> ColorData[97][#2], 
      PlotLabel -> #3, PlotRange -> All, ImageSize -> Small] &,
    {pndsv[A], Range[11], {a, x, Tn, V0, b, c, d, f, k, j, r}}]], 
  {A, 0.03, 0.045, 0.0025}, DisplayAllSteps -> True]

enter image description here

$\endgroup$

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.