Easy way to add another parameter to Manipulate

Vector definitions:

I’m sorry that I didn’t include the Vector definitions. I forgot because I had them in initialization cells. Here they are:

Vector[{ip : {_, _}, vec : {_, _}}, styles___] :=
Graphics[{Arrowheads[Medium], Thickness[Medium], styles,
Arrow[{ip, ip + vec}]}];
Vector[vec : {_, _}, styles___] := Vector[{{0, 0}, vec}, styles];
Vector[{ip : {_, _, _}, vec : {_, _, _}}, styles___] :=
Graphics3D[{GrayLevel[.4], CapForm[“Butt”], Arrowheads[Small],
styles, Arrow[Tube[{ip, ip + vec}]]}];
Vector[vec : {_, _, _}, styles___] := Vector[{{0, 0, 0}, vec}, styles]

I have:

DynamicModule[{f, plt, curve, k, r},
f[x_, y_] = 3 Exp[-x^2 – y^2];
plt = Plot3D[3 Exp[-x^2 – y^2], {x, -2, 2}, {y, -2, 2},
Mesh -> None,
BoxRatios -> Automatic];
k = .5;
kk = Sqrt[Log[3/k]];
r[s_] = {kk Cos[s/kk], kk Sin[s/kk], k};
curve = ParametricPlot3D[r[s], {s, 0, 2 \[Pi] kk},
PlotStyle -> {Blue, Thick}];
Manipulate[
Show[plt, curve,
Vector[{r[tt], r'[tt]}, Thick, Red],
Graphics3D[{
Red, PointSize[Large], Point[r[tt]]
}]
],
{{tt, 0.1}, 0.1, 2 \[Pi] kk}
]
]

Which produces:

Then suddenly I decide I want to add a slider for k. What’s the easy way to adapt my code to do this?

Reason for Dynamic Module:

This technique I use is due to a long discussion some time ago: Look Here

For example, if I take m_goldberg’s example (which is nice, and sorry I did this before he edited it—I also put f[x,y] in the Plot3D command to demonstrate what happens) and place two copies in one notebook.

This in one cell.

Manipulate[
Show[Plot3D[f[x, y], {x, -2, 2}, {y, -2, 2}, Mesh -> None,
BoxRatios -> Automatic],
ParametricPlot3D[r[s, k], {s, 0, 2 \[Pi] kk[k]},
PlotStyle -> {Blue, Thick}],
Graphics3D[{Red, Thick, Arrow[{r[tt, k], r[tt, k] + rds[tt, k]}]}],
Graphics3D[{Red, Sphere[r[tt, k], .1]}]], {{tt, 0.1}, 0.1,
2 \[Pi] kk[k], Appearance -> “Labeled”}, {{k, .5}, .1, 2.8, .1,
Appearance -> “Labeled”},
Initialization :> (f[x_, y_] = 3 Exp[-x^2 – y^2];
kk[k_] := Sqrt[Log[3/k]];
r[s_, k_] := {kk[k] Cos[s/kk[k]], kk[k] Sin[s/kk[k]], k};
rds[s_, k_] := {-Sin[s/Sqrt[Log[3/k]]], Cos[s/Sqrt[Log[3/k]]], 0})]

This in a second cell in the same notebook

Manipulate[
Show[Plot3D[f[x, y], {x, -2, 2}, {y, -2, 2}, Mesh -> None,
BoxRatios -> Automatic],
ParametricPlot3D[r[s, k], {s, 0, 2 \[Pi] kk[k]},
PlotStyle -> {Blue, Thick}],
Graphics3D[{Red, Thick, Arrow[{r[tt, k], r[tt, k] + rds[tt, k]}]}],
Graphics3D[{Red, Sphere[r[tt, k], .1]}]], {{tt, 0.1}, 0.1,
2 \[Pi] kk[k], Appearance -> “Labeled”}, {{k, .5}, .1, 2.8, .1,
Appearance -> “Labeled”},
Initialization :> (f[x_, y_] = 3 Exp[x^2 – y^2];
kk[k_] := Sqrt[Log[3/k]];
r[s_, k_] := {kk[k] Cos[s/kk[k]], kk[k] Sin[s/kk[k]], k};
rds[s_, k_] := {-Sin[s/Sqrt[Log[3/k]]], Cos[s/Sqrt[Log[3/k]]], 0})]

Notice, the only difference between the two is in the Initialization section, where the function f is different. Other than that, they are identical. Now, save the notebook with both pieces of code in separate cells.

Evaluate the notebook. Then put your cursor in the first cell and evaluate that cell. You will note that you get the same graph in both Manipulate outputs. Now, put your cursor in the second cell and evaluate that cell. You will see both Manipulate outputs change to that function.

The dynamic module prevents this. When I am writing a notebook to help my students, I might have several Manipulate demos. If I am teaching the position vector in calculus, typically title something like →r(t)=cost ˆi+sint ˆj,\vec r(t)=\cos t\ \hat{i}+\sin t\ \hat{j}, then I’ll be using a function r[t_] in the Manipulate demos and it has to be protected.

Two Additional Questions:

In my dynamic module, can the lines before the Manipulate command have any access to the variable, such as tt, in the Manipulate body?
Can anyone duplicate my demonstration with a complete Dynamic Module and no use of Manipulate? I might be at a level where I might understand if I can get an example.

=================

2

 

why put Manipulate inside DynamicModule? Manipulate is already a DynamicModule? can you not just do everything using Manipulate directly?
– Nasser
Jul 21 ’15 at 23:52

  

 

Then suddenly I decide I want to add a slider for k I am also little confused in how to help. Why can’t you just add a slider for k in the same way you did for the tt? Without explaining more the user functionality you are trying to achieve, this would be the obvious way to do it. Also, making everything in just one Manipulate will simplify things. Manipulate only runs in the UI, so making it work as a function to be called indirectly is a little strange to me.
– Nasser
Jul 22 ’15 at 0:31

  

 

@Nasser The answer is here.
– David
Jul 22 ’15 at 2:07

=================

2 Answers
2

=================

How about something like this?

Clear[f, plt, kk, curve]

f[x_, y_] = 3 Exp[-x^2 – y^2];
plt = Plot3D[3 Exp[-x^2 – y^2], {x, -2, 2}, {y, -2, 2}, Mesh -> None, BoxRatios -> Automatic];

Manipulate[
kk = Sqrt[Log[3/k]];
curve = ParametricPlot3D[{kk Cos[s/kk], kk Sin[s/kk], k}, {s, 0, 2 Pi kk}, PlotStyle -> {Blue, Thick}];

Show[
plt,
curve,
(*Vector[{r[tt],r'[tt]},Thick,Red],*)
Graphics3D[{Red, Ball[{kk Cos[tt/kk], kk Sin[tt/kk], k}, 0.15]}]
],

{{tt, 0.1}, 0.1, 2 Pi kk},
{{k, 0.5}, 0.1, 2.5, 0.1}
]

Unfortunately you didn’t include the definition of Vector, so I wasn’t able to test that one out. I also swapped out your 2D Point for a 3D Ball object that looks better in the 3D plot, at least in my opinion. Since the f and plt are not dynamic, they can also be taken outside of the Manipulate body.

  

 

Nice answer, but see my update in my original post. I should have been more specific as to what I needed.
– David
Jul 22 ’15 at 2:43

This isn’t very different from MarcoB’s solution, but I did implement the tangent vector, 🙂

Clear[k];
With[{kk = Sqrt[Log[3/k]]},
Manipulate[
Show[
Plot3D[3 Exp[-x^2 – y^2], {x, -2, 2}, {y, -2, 2},
Mesh -> None, BoxRatios -> Automatic],
ParametricPlot3D[r[s, k], {s, 0, 2 π kk},
PlotStyle -> {Blue, Thick}],
Graphics3D[{Red, Thick, Arrow[{r[tt, k], r[tt, k] + rds[tt, k]}]}],
Graphics3D[{Red, Sphere[r[tt, k], .1]}]],
{{tt, 0.1}, 0.1, 2 Ï€ kk, Appearance -> “Labeled”},
{{k, .5}, .1, 2.8, .1, Appearance -> “Labeled”},
Initialization :> (
r[s_, k_] := {kk Cos[s/kk], kk Sin[s/kk], k};
rds[s_, k_] := {-Sin[s/kk], Cos[s/kk], 0})]]

Introducing k as a controlled variable isn’t exactly easy because changing it from a parameter to a variable makes r a function of both tt and k.

  

 

I agree, the sphere looks really cool. I’ve gotta try that. However, please see the update to my original post.
– David
Jul 22 ’15 at 2:44