Mathematica 9 is now available
Student Support Forum
-----
Student Support Forum: 'Demonstrations Code' topicStudent Support Forum > General > Archives > "Demonstrations Code"

Next Comment >Help | Reply To Topic
Author Comment/Response
Greg
01/09/08 10:32pm

I found a demonstration that's really helpful for manipulating graphs, and I'd like to try it on some different polynomials, but I can't figure out some of the code. What part of the code below allows you to drag the black dot along the line? (the demonstration can be found at http://demonstrations.wolfram.com/TheDefinitionOfTheDerivative/)

f[x_] := Sqrt[x];

a = 1;

pf = Plot[f[x], {x, 0, 2.5}, PlotStyle -> Thickness[0.005]];

m[x_] := If[x == a, f'[a], (f[x] - f[a])/(x - a)];

secantLine[x_] :=
Plot[f[a] + m[x]*(t - a), {t, 0, 2.5},
PlotStyle -> {If[x != a , Black, White], Thickness[0.004]}];
(* the secant line disappears when x = a. The slope m[a] is defined \
to avoid error messages. *)

fig[xx_] := Show[secantLine[xx], pf, Graphics[{PointSize[.02],
{Black, Point[{xx, f[xx]}]},
{Red, Point[{a, f[a]}]},
Text[
Style["The derivative is the slope of the tangent line.",
Bold], {0.1, 1.65}, {-1, 1}],
Text[
"The tangent line is the limit of secant lines.", {0.1,
1.55}, {-1, 1}],
Text["Drag the black point.", {2.2, 1.25}],
Text[
TraditionalForm[Row[{HoldForm[f[x]], " = "}]], {1.8, .9}, {1,
0}], (* Text is positioned so the = signs line up *)
Text["\!\(\*SqrtBox[\"x\"]\)", {1.8, .9}, {-1, 0}],
Text[
TraditionalForm[
Row[{"The red point is at (1, ", HoldForm@f[1],
") = "}]], {1.8, 0.75}, {1, 0}],
Text["(1, 1)", {1.8, 0.75}, {-1, 0}],
Text[
TraditionalForm[
Row[{"The black point is at (", HoldForm@x , ", ",
HoldForm@f[x], "\!\(\*
StyleBox[\")\", \"InlineFormula\"]\) = "}]], {1.8, .6}, {1, 0}],
Text[
"(" <> ToString[xx] <> ", " <> ToString[f[xx]] <>
")", {1.8, .6}, {-1, 0}],
Text["The slope of secant line is \!\(\*FractionBox[
RowBox[{
RowBox[{\"f\",
RowBox[{\"(\", \"x\", \")\"}]}], \" \", \"-\", \" \",
RowBox[{\"f\",
RowBox[{\"(\", \"1\", \")\"}]}]}],
RowBox[{\"x\", \"-\", \"1\"}]]\) = ", {1.8, .4}, {1, 0}],
Text[ ToString[(f[xx] - f[a])/(xx - a)], {1.8, .4}, {-1, 0}],
Text[
TraditionalForm[
Row[{"The derivative of ", HoldForm@f, " at 1 is ",
HoldForm@f'[1], " = "}]], {1.8, .2}, {1, 0}],
Text["\!\(\*UnderscriptBox[
RowBox[{\"lim\", \" \"}],
RowBox[{\"x\", \"\[Rule]\", \"1\"}]]\)\!\(\*FractionBox[
RowBox[{
RowBox[{\"f\",
RowBox[{\"(\", \"x\", \")\"}]}], \"-\",
RowBox[{\"f\",
RowBox[{\"(\", \"1\", \")\"}]}]}],
RowBox[{\"x\", \"-\", \"1\"}]]\) = 0.5", {1.8, .2}, {-1, 0}]
}], PlotRange -> {{0, 2.5}, {0, 1.6}}, AxesOrigin -> {0, 0},
AxesLabel -> {Style[x, Italic], Style[y, Italic]},
AspectRatio -> Automatic, ImageSize -> 385];

myRound[x_, n_] := Round[x*10^n]/10.^n;
(* Rounds to n decimal places. This is allows user to get x = 1 \
exactly. *)

pt2x[pt_] := Min[Max[0, myRound[pt[[1]], 3] ], 2.5];
(* Only take the x coordinate of the locator, rounded to three \
decimal places, and put it in the interval [0, 2.5] *)

finalFig[pt_] :=
Quiet[fig[pt2x[pt]], {Power::"infy", \[Infinity]::"indet"}];

Manipulate[
finalFig[pt], {{pt, {2, 0}}, {0, 0}, {2, 2}, Locator,
Appearance -> None}, SaveDefinitions -> True ]

URL: ,

Subject (listing for 'Demonstrations Code')
Author Date Posted
Demonstrations Code Greg 01/09/08 10:32pm
Re: Demonstrations Code Xavier 02/25/08 10:03am
Next Comment >Help | Reply To Topic