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 $\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.
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 fork
in the same way you did for thett
? 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. $\endgroup$