2
$\begingroup$

While modeling a falling object, the following code works fine:

h[t_] = 10 - g*t^2/2;

Manipulate[
 Graphics3D[{Blue, Sphere[{0, 0, h[t]}, 2]} {t, 0, 6}]

But if I try to define a center like:

h[t_] = 10 - g*t^2/2;
center = {0, 0, h[t]}
Manipulate[
 Graphics3D[{Blue, Sphere[center, 2]} {t, 0, 6}]

It throws the error:

Coordinate {0, 0, 10 - 4.905 $CellContext`t^2} should be a triple of numbers, or a Scaled form.

What is going on?

$\endgroup$
0

1 Answer 1

2
$\begingroup$

I fixed the syntax errors and added definition. This works

g = 9.81;
h[t_] := 10 - g*t^2/2;
center[t_] := {0, 0, h[t]};
Manipulate[
 Graphics3D[{Blue, Sphere[center[t], 2]}, Axes -> True], {t, 0, 6},
 TrackedSymbols :> {t}
 ]

Now center is used since it depends on t and Manipulate tracks t

$\endgroup$
5
  • $\begingroup$ It's also a good idea to add the setting SaveDefinitions -> True in situations like this. $\endgroup$ Apr 18, 2020 at 5:22
  • $\begingroup$ @J.M. yes, But myself, I do not write things like the above. I would put all functions needed by Manipulate in its Initialization section. $\endgroup$
    – Nasser
    Apr 18, 2020 at 5:24
  • $\begingroup$ Yes, I agree that's even better practice. $\endgroup$ Apr 18, 2020 at 5:27
  • $\begingroup$ It's clear now. What do you mean by "put all functions needed by Manipulate in its Initialization section". $\endgroup$
    – Chegon
    Apr 18, 2020 at 17:28
  • $\begingroup$ @Chegon it is like this Initialization where functions used by Manipulate are put inside Manipulate initialization section. $\endgroup$
    – Nasser
    Apr 18, 2020 at 19:19

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.