มาใช้ Mathematica ในการเรียนการสอนกัน ด้วยคำสั่ง Manipulate 3

จากตอนที่แล้ว เดี๋ยวผมจะพูดถึงการใช้งาน slider กับตัวควบคุมอื่นๆ ของ Manipulate ครับ

ดูตัวอย่างไปก่อนนะครับ

Manipulate[
Module[{anglegraph, maingraph},
anglegraph[th_, showtext_] := Show[
Graphics[{
{Lighter[Gray, 0.5], Circle[{0, 0}, 1]},
{Darker[Green, 0.2], Thick, Circle[{0, 0}, 1, {0, th}]},
{Lighter[Gray, 0.5], Line[{{0, 0}, {Cos[th], Sin[th]}}]},
{Red, Thick, Line[{{Cos[th], 0}, {Cos[th], Sin[th]}}]},
If[showtext,
Rotate[Text[
Style[N[Sin[th]], 11], {Cos[th] – 0.15, Sin[th]/2}],
3 Pi/2, {Cos[th] – 0.15, Sin[th]/2}], {}],
{Lighter[Red, 0.3], Dashing[Medium],
Line[{{Cos[th], Sin[th]}, {2, Sin[th]}}]}
}],
PlotRange -> 1, ImageSize -> 145, BaseStyle -> {12}, Axes -> True,
Ticks -> {{-1, 1}, {-1, 1}}, PlotRange -> {{-1, 1}, {-1, 1}},
PlotRangePadding -> 0.25];

maingraph[th_, showtext_] := Module[{},
Show[Plot[{Sin[x]}, {x, 0.0001, th},
PlotRange -> {{0, 2 Pi}, {-1, 1}},
PlotRangePadding -> {0, 0.25},
ImagePadding -> {{30, 12}, {0, 0}}, PlotRangeClipping -> False,
PlotStyle -> Gray,
Ticks -> {Table[{n Pi/4, n Pi/4}, {n, 0, 8}],
Table[n, {n, -1, 1, 1/2}]},
GridLines -> {Table[{n Pi/4, Lighter[Gray, 0.7]}, {n, -2, 8}],
Table[{n, Lighter[Gray, 0.7]}, {n, -1, 1, 1/2}]},
ImageSize -> {Automatic, 145}],
Graphics[{If[showtext,
Rotate[Text[Style[N[Sin[th]], 11], {th + 0.15, Sin[th]/2}],
3 Pi/2, {th + 0.15, Sin[th]/2}], {}],
{Lighter[Red, 0.3], Dashing[Medium],
Line[{{th, Sin[th]}, {-2, Sin[th]}}]},
{Darker[Green, 0.2], Thick, Line[{{0, 0}, {th, 0}}]},
{Red, Thick, Line[{{th, 0}, {th, Sin[th]}}]}
}], AspectRatio -> Automatic, BaseStyle -> {12}]];

DynamicModule[{pt = {Cos[ptctrl], Sin[ptctrl]}, pt2 = {ptctrl, 0}},
Labeled[Grid[{
{LocatorPane[Dynamic[pt,
{(pt = {Cos[pt2[[1]]], Sin[pt2[[1]]]}) &,
(pt = Normalize[#];
pt2 = {If[pt2 == {2 Pi, 0}, 2 Pi,
Mod[ArcTan[#[[1]], #[[2]]], 2 Pi]], 0}) &,
(pt = Normalize[#]; ptctrl = pt2[[1]]) &}],
Dynamic[
anglegraph[
If[pt2 == {2 Pi, 0}, 2 Pi,
Mod[ArcTan[pt[[1]], pt[[2]]], 2 Pi]], showvalue]]],
LocatorPane[Dynamic[pt2,
{(pt2 = {If[pt2 == {2 Pi, 0}, 2 Pi,
Mod[ArcTan[pt[[1]], pt[[2]]], 2 Pi]], 0}) &,
(pt2 = {#[[1]], 0}; pt = {Cos[#[[1]]], Sin[#[[1]]]}) &,
(pt2 = {#[[1]], 0}; ptctrl = #[[1]]) &}],
Dynamic[
maingraph[
If[pt2 == {2 Pi, 0}, 2 Pi,
Mod[ArcTan[pt[[1]], pt[[2]]], 2 Pi]], showvalue]]]}},
Spacings ->
0], {Row[{Style[“Illustrating “, “Label”, 20, Gray],
Text@Style[“sin(“, Red, 24],
Text@Style[“x”, Italic, Bold, Darker[Green, 0.3], 24],
Style[“)”, Red, 24]}],
Style[“”, 10, Lighter[Gray, 0.7], “Label”]}, {{Top,
Left}, {Bottom, Right}}]]
],
{{showvalue, False, “show value”}, {False, True}},
{{ptctrl, Pi/6, “angle”}, 0, 2 Pi},
TrackedSymbols :> {showvalue, ptctrl}]

ส่วนอันนี้เป็นตัวอย่างที่เราสามารถลากเขียนกราฟได้ครับ

Manipulate[
points = If[Last[points] == p, points, points~Join~{p}];
ListLinePlot[points, PlotRange -> {{-100, 100}, {-100, 100}},
PlotLabel -> (“จำนวนจุด ” <> ToString@Length[points])],
Button[“Clear”, clear = True; points = {{0, 0}};
p = {0, 0};], {{p, {0, 0}}, Locator},
Initialization :> {
points = {{0, 0}};
clear = False;
}
]

 

Manipulate[Quiet@DynamicModule[{g=9.81,m=1.,deqns,xsol,ysol,times,periods,periodNumber},

deqns={(*parabolic*){a (1+x[t]^2) Derivative[2][x][t]==-\[Mu] Derivative[1][x][t]/m-x[t] (g+2 a Derivative[1][x][t]^2)},(*elliptic*){\[Mu] Derivative[1][x][t]/m+Derivative[2][x][t]==x[t] (-g Sqrt[1-x[t]^2]/a+Derivative[1][x][t]^2/(-1+x[t]^2))}};
{xsol,times}=Reap@NDSolveValue[{deqns[[i]],x[0]==.999,Derivative[1][x][0]==0,WhenEvent[x[t]==0,Sow[t]]},x,{t,tMax}];
ysol[t_]:={(*parabolic path*)a (xsol[t]^2-1),(*elliptic path*)-a Sqrt[1-xsol[t]^2]}[[i]];
periodNumber=Position[times[[1]],Nearest[times[[1]],tt][[1]]][[1,1]]-1;
periods=Differences[times[[1]]];
Column[{Row[{“swing# “,periodNumber,”: “,If[periodNumber!=0,periods[[periodNumber]],0],” seconds”}],Dynamic@Show[{Plot[{a (x^2-1),-a Sqrt[1-x^2]}[[i]],{x,-1,1},PlotStyle->Thick],Graphics[{Red,Disk[.98 {xsol[tt],ysol[tt]},.02]}]},AspectRatio->Automatic,ImageSize->400,PlotRange->{{-1,1},{-1.25,0.05}}]}]],{{i,1,”path”},{1->”parabolic”,2->”elliptic”},Setter},{{a,1},.5,1.25,.001,Appearance->”Labeled”},{{\[Mu],.15},0,.25,.01,Appearance->”Labeled”},Control[{{tt,0},0,.5 tMax,.01,Trigger,AnimationRate->1}],{{tMax,100},None}]