1
$\begingroup$

I'm trying to make a graph change based on k I used Manipulate on the whole code but it just gives a lot of errors

    Manipulate[
 Remove["Global`*"]
   (*Constants*)g = 9.8;
 
 (*Differential Equation*)
 xcomp := x''[t] == -k x'[t];
 ycomp := y''[t] == -k y'[t] - g;
 diff := {xcomp, ycomp}
   
   (*Initial Conditions*)
   v0 = 600; \[Theta] = 60 Degree;
 initcond = {x[0] == 0, x'[0] == v0 Cos[\[Theta]], y[0] == 0, 
    y'[0] == v0 Sin[\[Theta]]}
   
   (*Solve*)
   eqn := Append[diff, initcond];
 s = DSolve[eqn, {x[t], y[t]}, t] // Simplify
     y[t_] = y[t] /. s[[1]]
       
       (*Time of Flight*)
       tof = Solve[y[t] == 0, t]; // Quiet
      T = t /. tof[[2]]
      
      (*Plot*)
      ParametricPlot[{x[t], y[t]} /. s, {t, 0, T}, 
       PlotRange -> All], {k, 0, 1}]
$\endgroup$
2
$\begingroup$

Each line of the expression being manipulated -- except the last -- needs to end with a semi-colon. This makes a single compound expression.

Manipulate[
 Clear["Global`*"]; (* use Clear rather than Remove *)
 (*Constants*)
 g = 9.8;
 (*Differential Equation*)
 xcomp := x''[t] == -k x'[t];
 ycomp := y''[t] == -k y'[t] - g;
 diff := {xcomp, ycomp};
 (*Initial Conditions*) 
 v0 = 600;
 θ = 60 Degree;
 initcond = {x[0] == 0, x'[0] == v0 Cos[θ], y[0] == 0, 
   y'[0] == v0 Sin[θ]};
 (*Solve*) 
 eqn := Append[diff, initcond];
 s = DSolve[eqn, {x[t], y[t]}, t] // Simplify; y[t_] = y[t] /. s[[1]];
 (*Time of Flight*) tof = Solve[y[t] == 0, t] // Quiet;
 T = t /. tof[[2]];
 (*Plot*) 
 ParametricPlot[{x[t], y[t]} /. s, {t, 0, T},
  PlotRange -> All,
  AspectRatio -> 1],
 {{k, 0.01}, 0, 1, 0.01, Appearance -> "Labeled"}]

enter image description here

However, in general to optimize performance of the Manipulate, as much of the computation as possible should be done outside the Manipulate.

$\endgroup$
1
  • $\begingroup$ Thanks. I tried using this code but it didn't work. Probably because I'm using an older version (my school didn't buy an update). $\endgroup$ – Choop Apr 24 at 1:42
2
$\begingroup$

I just saw Bob's solution as I was writing this. Might as well throw this in. You had few syntax error as mentioned.

It is better to first solve the problem outside Manipulate. To make sure you get the code working first. You can set some variable to some values if needed. Then make the plot. All before adding Manipulate.

Now that you have it working OK, you can then move the code inside Manipulate. This is much easier than having to figure what is wrong as you also using Manipulate.

enter image description here

Manipulate[
 Module[{h, x, t, v0, theta, xSol, ySol, tof},
  g = 9.8; v0 = 600; theta = 60 Degree;
  xcomp = x''[t] == -k x'[t];(*Differential Equation*)
  ycomp = y''[t] == -k y'[t] - g;
  initcond = {x[0] == 0, x'[0] == v0 Cos[theta], y[0] == 0, y'[0] == v0 Sin[theta]};
  {xSol, ySol} = {x[t], y[t]} /. First@DSolve[{xcomp, ycomp, initcond}, {x[t], y[t]}, t];
  tof = Quiet@Max[t /. Solve[ySol == 0, t]];
  ParametricPlot[{xSol, ySol}, {t, 0, tof}, AxesOrigin -> {0, 0}, 
   PlotRange -> {{0, 30000}, {0, 15000}}, GridLines -> Automatic, 
   GridLinesStyle -> LightGray]
  ],
 {{k, 0, "k"}, 0, .1, .001, Appearance -> "Labeled"},
 TrackedSymbols :> {k}
 ]
$\endgroup$
2
  • $\begingroup$ Thank you, this worked. What does the @ mean? I can't find its documentation. And how does Module work within Manipulate? $\endgroup$ – Choop Apr 24 at 1:43
  • 1
    $\begingroup$ @Choop the @ just short hand for function call. Instead of writing f[g], you can write f@g, and using Module inside Manipulate allows you to protect symbols used from global scope, so it is a good thing to do. $\endgroup$ – Nasser Apr 24 at 15:21

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.