0
$\begingroup$

I'm defined W[t] and Q[t] like below. This code is an example that shows my problem. My real code is very long and complex that i cant make change in it. I solved my differential equations but sum function that defined by derivatives the same of W[t] and Q[t] can't be used in animation graphics. If i define them without D[,t] function i could use them in animated graphics but i need them in this form. I can Plot W[t] and Q[t] and i can gather this function output in a desired time easily but i can't animate a ball or line with using W[t] and Q[t]. /.sol//Evaluate Works for plot and animated Plot but didn't work for Animated graphics. please help me for use of my function in animated Plot. My real code is very long and i cant make change in body of code.

ClearAll[W, Q, x, sol, a]
W[t_] := Max[Piecewise[{{-1, Q[t] <= 0}, {1, Q[t] > 0}}], 0];
Q[t_] := D[x[t], {t, 2}] + x[t];
sol = NDSolve[{Q[t] + x[t] == 1, x[0] == 1, x'[0] == 2}, {x, x'}, {t,0,2}];
Plot[(Q[t] + W[t]) /. sol // Evaluate, {t, 0, 2}]       (*Correctly worked*)
Animate[Plot[(Q[a] + W[a]) /. sol // Evaluate, {a, 0, t}, 
  PlotRange -> {{0, 2}, {-1, 1.5}}], {t, 0, 2}]         (*Correctly worked*)
(Q[t] + W[t]) /. sol /. t -> 0.7                        (*Correctly worked*)
Animate[Graphics[{{Circle[{t, (Q[t] + W[t]) /. sol // Evaluate},0.1]}}, PlotRange -> {{0, 2}, {-1, 1.5}}], {t, 0,2}] (*!! Didn't work*)

I tried for move a ball with radius of 0.1 with this Ndsolved functions but my efforts didn't be successful.

$\endgroup$

1 Answer 1

2
$\begingroup$
W[t_] := Max[Piecewise[{{-1, Q[t] <= 0}, {1, Q[t] > 0}}], 0];
Q[t_] := D[x[t], {t, 2}] + x[t];
sol = NDSolve[{Q[t] + x[t] == 1, x[0] == 1, x'[0] == 2}, {x, x'}, {t, 
    0, 2}];
T = Table[
   Flatten[{t1, Q[t] + W[t] /. sol /. t -> t1}], {t1, 0., 2, .01}];

L = ListAnimate[
  Table[Graphics[{{Red, Circle[T[[i]], 0.1]}, {Green, 
      Rectangle[{-.1, -1.2}, {2.2, -1.1}]}}, 
    PlotRange -> {{-.1, 2.2}, {-1.2, 1.5}}], {i, 1, Length[T]}]] 

fig1

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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