# Why can't I manipulate a whole set of code?

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, x' == v0 Cos[\[Theta]], y == 0,
y' == v0 Sin[\[Theta]]}

(*Solve*)
eqn := Append[diff, initcond];
s = DSolve[eqn, {x[t], y[t]}, t] // Simplify
y[t_] = y[t] /. s[]

(*Time of Flight*)
tof = Solve[y[t] == 0, t]; // Quiet
T = t /. tof[]

(*Plot*)
ParametricPlot[{x[t], y[t]} /. s, {t, 0, T},
PlotRange -> All], {k, 0, 1}]


## 2 Answers

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, x' == v0 Cos[θ], y == 0,
y' == v0 Sin[θ]};
(*Solve*)
eqn := Append[diff, initcond];
s = DSolve[eqn, {x[t], y[t]}, t] // Simplify; y[t_] = y[t] /. s[];
(*Time of Flight*) tof = Solve[y[t] == 0, t] // Quiet;
T = t /. tof[];
(*Plot*)
ParametricPlot[{x[t], y[t]} /. s, {t, 0, T},
PlotRange -> All,
AspectRatio -> 1],
{{k, 0.01}, 0, 1, 0.01, Appearance -> "Labeled"}] However, in general to optimize performance of the Manipulate, as much of the computation as possible should be done outside the Manipulate.

• 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). – Choop Apr 24 at 1:42

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. 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, x' == v0 Cos[theta], y == 0, y' == 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}
]

• Thank you, this worked. What does the @ mean? I can't find its documentation. And how does Module work within Manipulate? – Choop Apr 24 at 1:43
• @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. – Nasser Apr 24 at 15:21