(* Content-type: application/vnd.wolfram.mathematica *) (*** Wolfram Notebook File ***) (* http://www.wolfram.com/nb *) (* CreatedBy='Mathematica 11.0' *) (*CacheID: 234*) (* Internal cache information: NotebookFileLineBreakTest NotebookFileLineBreakTest NotebookDataPosition[ 158, 7] NotebookDataLength[ 38177, 1059] NotebookOptionsPosition[ 34688, 943] NotebookOutlinePosition[ 35047, 959] CellTagsIndexPosition[ 35004, 956] WindowFrame->Normal*) (* Beginning of Notebook Content *) Notebook[{ Cell[CellGroupData[{ Cell["Solutions to the exercises from last time:", "Section", CellChangeTimes->{{3.621180076112595*^9, 3.6211800851370907`*^9}, { 3.696238646345399*^9, 3.696238669488076*^9}}], Cell[CellGroupData[{ Cell["\<\ Exercise 1. Make an interactive model that reads the value of two variables, \ total and sum, represented by two different controls, and outputs their sum, \ total+sum. Does the example below do that? \ \>", "Subsection", CellDingbat->"\[FilledSquare]", CellChangeTimes->{{3.620447133268231*^9, 3.620447196828288*^9}, { 3.620447614477296*^9, 3.6204476416878357`*^9}, 3.6204495418065853`*^9, { 3.620493148092608*^9, 3.6204931514192123`*^9}, {3.651886868820175*^9, 3.651886873204561*^9}}, FontSize->18], Cell[BoxData[ RowBox[{"Manipulate", "[", "\[IndentingNewLine]", " ", RowBox[{ RowBox[{ RowBox[{"total", "=", RowBox[{"total", "+", "step"}]}], ";", "\[IndentingNewLine]", RowBox[{"{", RowBox[{"step", ",", "total"}], "}"}]}], ",", "\[IndentingNewLine]", RowBox[{"{", RowBox[{ RowBox[{"{", RowBox[{"total", ",", "0"}], "}"}], ",", RowBox[{"-", "1000"}], ",", "1000", ",", "1"}], "}"}], ",", RowBox[{"{", RowBox[{ RowBox[{"{", RowBox[{"step", ",", "0"}], "}"}], ",", RowBox[{"-", "10"}], ",", "10", ",", "1"}], "}"}], ",", "\[IndentingNewLine]", RowBox[{"FrameLabel", "\[Rule]", "\"\\""}]}], "]"}]], "Input", CellChangeTimes->{{3.6204450441644983`*^9, 3.620445044165217*^9}, { 3.620447495199263*^9, 3.6204475027974863`*^9}, {3.620447540852353*^9, 3.620447545875905*^9}, {3.620447589701573*^9, 3.620447597363188*^9}, { 3.620458568238985*^9, 3.620458595142166*^9}}, Background->RGBColor[0.87, 0.94, 1]], Cell[CellGroupData[{ Cell["Solution :", "Subsubsection", CellChangeTimes->{{3.696238701360075*^9, 3.696238713176509*^9}}], Cell[BoxData[ RowBox[{"Manipulate", "[", "\[IndentingNewLine]", " ", RowBox[{ RowBox[{ RowBox[{"sum", "=", RowBox[{"total", "+", "step"}]}], ";", "\[IndentingNewLine]", RowBox[{"{", RowBox[{"step", ",", " ", "total", ",", "sum"}], "}"}]}], ",", "\[IndentingNewLine]", RowBox[{"{", RowBox[{ RowBox[{"{", RowBox[{"total", ",", "0"}], "}"}], ",", RowBox[{"-", "1000"}], ",", "1000", ",", "1"}], "}"}], ",", RowBox[{"{", RowBox[{ RowBox[{"{", RowBox[{"step", ",", "0"}], "}"}], ",", RowBox[{"-", "10"}], ",", "10", ",", "1"}], "}"}], ",", "\[IndentingNewLine]", RowBox[{"FrameLabel", "\[Rule]", "\"\\""}]}], "]"}]], "Input", CellChangeTimes->{{3.696238734852435*^9, 3.696238754050724*^9}, { 3.696238863266848*^9, 3.696238871056551*^9}}, Background->RGBColor[0.88, 1, 0.88]] }, Open ]] }, Open ]], Cell[CellGroupData[{ Cell[TextData[{ "Exercise 2. A way to compute the sum of the first m positive integers in ", StyleBox["Mathematica", FontSlant->"Italic"], " is to do a For loop, like the one written below. Turn the code below into \ an interactive model where m will be allowed to take any values in the set \ {10, 20, 30, 40, 50}. " }], "Subsection", CellDingbat->"\[FilledSquare]", CellChangeTimes->{{3.620447757692485*^9, 3.620447762500182*^9}, { 3.620448651813036*^9, 3.620448716410104*^9}, {3.620448773793989*^9, 3.620448999525469*^9}, {3.6204490558175097`*^9, 3.6204490817380733`*^9}, { 3.651886879161338*^9, 3.651886883281176*^9}, {3.6518878250903788`*^9, 3.651887852115736*^9}, {3.6518938447716618`*^9, 3.65189385958366*^9}}, TextJustification->1., FontSize->18], Cell[BoxData[{ RowBox[{ RowBox[{"m", "=", "5"}], ";"}], "\[IndentingNewLine]", RowBox[{ RowBox[{"s", "=", "0"}], ";"}], "\[IndentingNewLine]", RowBox[{ RowBox[{"For", "[", RowBox[{ RowBox[{"i", "=", "1"}], ",", RowBox[{"i", "\[LessEqual]", "m"}], ",", RowBox[{"i", "++"}], ",", " ", RowBox[{"s", "=", RowBox[{"s", "+", "i"}]}]}], "]"}], ";"}], "\[IndentingNewLine]", "s"}], "Input", CellChangeTimes->{{3.620448746142027*^9, 3.620448769506248*^9}}, Background->RGBColor[0.87, 0.94, 1]], Cell[CellGroupData[{ Cell["Imperfect Solution :", "Subsubsection", CellChangeTimes->{{3.696238701360075*^9, 3.696238713176509*^9}, { 3.6962390159693947`*^9, 3.696239017600274*^9}}], Cell[BoxData[ RowBox[{"Manipulate", "[", "\[IndentingNewLine]", RowBox[{ RowBox[{ RowBox[{"s", "=", "0"}], ";", "\[IndentingNewLine]", RowBox[{"For", "[", RowBox[{ RowBox[{"i", "=", "1"}], ",", RowBox[{"i", "\[LessEqual]", "m"}], ",", RowBox[{"i", "++"}], ",", " ", RowBox[{"s", "=", RowBox[{"s", "+", "i"}]}]}], "]"}], ";", "\[IndentingNewLine]", "s"}], ",", "\[IndentingNewLine]", RowBox[{"{", RowBox[{"m", ",", RowBox[{"{", RowBox[{"10", ",", "20", ",", "30", ",", "40", ",", "50"}], "}"}]}], "}"}]}], "]"}]], "Input", CellChangeTimes->{{3.620448746142027*^9, 3.620448769506248*^9}, { 3.696058519401379*^9, 3.6960586331245203`*^9}, {3.6962390294184923`*^9, 3.696239032154481*^9}}, FontSize->14, Background->RGBColor[1, 0.85, 0.85]] }, Open ]], Cell[CellGroupData[{ Cell["Solution 1 :", "Subsubsection", CellChangeTimes->{{3.696238701360075*^9, 3.696238713176509*^9}, { 3.6962390159693947`*^9, 3.696239051009142*^9}, 3.696239152478363*^9, { 3.696247960499373*^9, 3.696247962786343*^9}}], Cell[BoxData[ RowBox[{"Manipulate", "[", RowBox[{ RowBox[{"Module", "[", RowBox[{ RowBox[{"{", RowBox[{"s", ",", "i"}], "}"}], ",", "\[IndentingNewLine]", RowBox[{ RowBox[{"s", "=", "0"}], ";", "\[IndentingNewLine]", RowBox[{"For", "[", RowBox[{ RowBox[{"i", "=", "1"}], ",", RowBox[{"i", "\[LessEqual]", "m"}], ",", RowBox[{"i", "++"}], ",", " ", RowBox[{"s", "=", RowBox[{"s", "+", "i"}]}]}], "]"}], ";", "\[IndentingNewLine]", "s"}]}], "]"}], ",", "\[IndentingNewLine]", RowBox[{"{", RowBox[{"m", ",", RowBox[{"{", RowBox[{"10", ",", "20", ",", "30", ",", "40", ",", "50"}], "}"}]}], "}"}]}], "]"}]], "Input", CellChangeTimes->{{3.620448746142027*^9, 3.620448769506248*^9}, { 3.696058519401379*^9, 3.6960586331245203`*^9}}, FontSize->14, Background->RGBColor[0.88, 1, 0.88]] }, Open ]], Cell[CellGroupData[{ Cell["Solution 2 :", "Subsubsection", CellChangeTimes->{{3.696238701360075*^9, 3.696238713176509*^9}, { 3.6962390159693947`*^9, 3.696239051009142*^9}, {3.696239096659676*^9, 3.6962390967702923`*^9}, 3.6962391540843143`*^9}], Cell[BoxData[ RowBox[{"Manipulate", "[", "\[IndentingNewLine]", RowBox[{ RowBox[{ RowBox[{"s", "=", "0"}], ";", "\[IndentingNewLine]", RowBox[{"For", "[", RowBox[{ RowBox[{"i", "=", "1"}], ",", RowBox[{"i", "\[LessEqual]", "m"}], ",", RowBox[{"i", "++"}], ",", " ", RowBox[{"s", "=", RowBox[{"s", "+", "i"}]}]}], "]"}], ";", "\[IndentingNewLine]", "s"}], ",", "\[IndentingNewLine]", RowBox[{"{", RowBox[{"m", ",", RowBox[{"{", RowBox[{"10", ",", "20", ",", "30", ",", "40", ",", "50"}], "}"}]}], "}"}], ",", " ", RowBox[{"TrackedSymbols", "\[RuleDelayed]", RowBox[{"{", "m", "}"}]}]}], "]"}]], "Input", CellChangeTimes->{{3.620448746142027*^9, 3.620448769506248*^9}, { 3.696058519401379*^9, 3.69605858586753*^9}}, FontSize->14, Background->RGBColor[0.88, 1, 0.88]] }, Open ]] }, Open ]], Cell[CellGroupData[{ Cell["\<\ Exercise 3. Do an interactive model using Manipulate[...] that displays the \ pair of numbers x and ax^2+bx+c, where a,b,c are real parameters, and x is a \ randomly generated number, between min and max, x=RandomReal[{min,max}]. The \ model will have 5 controls that can be set by the user: a,b,c,min, max (a, \ b, c are real numbers between 1 and 10, min is an integer number in the set \ {2,3,4}, and max is also an integer number in the set {5,6,7}).\ \>", "Subsection", CellDingbat->"\[FilledSquare]", CellChangeTimes->{{3.620447757692485*^9, 3.620447762500182*^9}, { 3.620448651813036*^9, 3.620448716410104*^9}, {3.620448773793989*^9, 3.620448999525469*^9}, {3.6204490558175097`*^9, 3.6204490817380733`*^9}, { 3.651886879161338*^9, 3.651886883281176*^9}, {3.6518878250903788`*^9, 3.651887852115736*^9}, {3.6518938447716618`*^9, 3.65189385958366*^9}, { 3.696054608714691*^9, 3.696054890847954*^9}, {3.6960550285481577`*^9, 3.696055104620757*^9}, {3.696055869247199*^9, 3.696056085269106*^9}}, TextJustification->1., FontSize->18], Cell[CellGroupData[{ Cell["Imperfect Solution :", "Subsubsection", CellChangeTimes->{{3.696238701360075*^9, 3.696238713176509*^9}, { 3.6962390159693947`*^9, 3.696239017600274*^9}}], Cell[BoxData[ RowBox[{"Manipulate", "[", "\[IndentingNewLine]", RowBox[{ RowBox[{ RowBox[{"x", "=", RowBox[{"RandomReal", "[", RowBox[{"{", RowBox[{"min", ",", " ", "max"}], "}"}], "]"}]}], ";", "\[IndentingNewLine]", RowBox[{"Row", "[", RowBox[{"{", RowBox[{ "\"\\"", ",", "x", ",", "\"\< a=\>\"", ",", "a", ",", "\"\< b=\>\"", ",", "b", ",", "\"\< c=\>\"", ",", "c", ",", " ", "\"\< \!\(\*SuperscriptBox[\(ax\), \(2\)]\)+bx+c=\>\"", ",", " ", RowBox[{ RowBox[{"a", "*", RowBox[{"x", "^", "2"}]}], "+", RowBox[{"b", "*", "x"}], "+", "c"}]}], "}"}], "]"}]}], ",", "\[IndentingNewLine]", RowBox[{"{", RowBox[{"a", ",", "1", ",", "10"}], "}"}], ",", "\[IndentingNewLine]", RowBox[{"{", RowBox[{"b", ",", "1", ",", "10"}], "}"}], ",", "\[IndentingNewLine]", RowBox[{"{", RowBox[{"c", ",", "1", ",", "10"}], "}"}], ",", " ", "\[IndentingNewLine]", RowBox[{"{", RowBox[{"min", ",", RowBox[{"{", RowBox[{"2", ",", "3", ",", "4"}], "}"}]}], "}"}], ",", "\[IndentingNewLine]", RowBox[{"{", RowBox[{"max", ",", RowBox[{"{", RowBox[{"5", ",", "6", ",", "7"}], "}"}]}], "}"}], ",", "\[IndentingNewLine]", RowBox[{"ContentSize", "\[Rule]", RowBox[{"{", RowBox[{"400", ",", "80"}], "}"}]}]}], "]"}]], "Input", CellChangeTimes->{{3.621577945501072*^9, 3.6215780576898003`*^9}, { 3.6215785094934683`*^9, 3.621578509923173*^9}, {3.621578580180879*^9, 3.621578627543001*^9}, {3.653097294775337*^9, 3.653097326547574*^9}, { 3.6530973571486273`*^9, 3.653097359466344*^9}, {3.653097399737907*^9, 3.653097430510686*^9}, {3.696057818972686*^9, 3.696057822667268*^9}, { 3.696239628148497*^9, 3.696239879246036*^9}}, FontSize->14, Background->RGBColor[1, 0.85, 0.85]] }, Open ]] }, Open ]] }, Open ]], Cell[CellGroupData[{ Cell["Using Dynamic[...]", "Section", CellChangeTimes->{{3.621180076112595*^9, 3.6211800851370907`*^9}}], Cell["\<\ Let us define the variable t and then perform a simple computation, like \ t^2+5.\ \>", "Text", CellChangeTimes->{{3.621180089137369*^9, 3.621180098905321*^9}, { 3.621573739245213*^9, 3.621573778403679*^9}}, FontSize->16], Cell[BoxData[ RowBox[{ RowBox[{"t", "=", "10"}], ";"}]], "Input", CellChangeTimes->{{3.621180104344993*^9, 3.6211801183696404`*^9}}, FontSize->16, Background->RGBColor[0.87, 0.94, 1]], Cell[CellGroupData[{ Cell["This is a static output:", "Subsubsection", CellChangeTimes->{{3.621183834949822*^9, 3.6211838408131227`*^9}, { 3.621573793660677*^9, 3.621573799203683*^9}}], Cell[BoxData[ RowBox[{ RowBox[{"t", "^", "2"}], "+", "5"}]], "Input", CellChangeTimes->{{3.621180123977481*^9, 3.621180133393751*^9}}, FontSize->16, Background->RGBColor[0.87, 0.94, 1]] }, Open ]], Cell[CellGroupData[{ Cell["This is a dynamic output: ", "Subsubsection", CellChangeTimes->{{3.621183834949822*^9, 3.6211838408131227`*^9}, { 3.62157380621942*^9, 3.6215738130277767`*^9}}], Cell[BoxData[ RowBox[{"Dynamic", "[", RowBox[{ RowBox[{"t", "^", "2"}], "+", "5"}], "]"}]], "Input", CellChangeTimes->{{3.621180140868025*^9, 3.621180152051159*^9}}, FontSize->16, Background->RGBColor[0.87, 0.94, 1]], Cell["\<\ At first glance, the static output and the dynamic output look the same. \ However, notice what happens to the output of Dynamic[t^2+5] when we change \ the value of t.\ \>", "Text", CellChangeTimes->{{3.621183853277131*^9, 3.62118389093398*^9}, { 3.621573822099491*^9, 3.621573879558776*^9}}, TextJustification->1., FontSize->16], Cell[BoxData[ RowBox[{ RowBox[{"t", "=", "6"}], ";"}]], "Input", CellChangeTimes->{{3.621180165921866*^9, 3.621180167017509*^9}, 3.621573892367889*^9}, FontSize->16, Background->RGBColor[0.87, 0.94, 1]], Cell["\<\ The output of Dynamic[t^2+5] has changed to reflect the current value of t. \ The output of t^2+5 remains the same, until we compile it again \ (Shift+Enter). Run also the following lines to understand the difference \ between static and dynamic output.\ \>", "Text", CellChangeTimes->{{3.621183853277131*^9, 3.62118389093398*^9}, { 3.621573822099491*^9, 3.621573879558776*^9}, {3.621573920343536*^9, 3.62157393647855*^9}, {3.621573967446558*^9, 3.621574015390749*^9}, { 3.621615900136466*^9, 3.621615902356427*^9}}, FontSize->16, FontColor->GrayLevel[0]], Cell[BoxData[ RowBox[{"MousePosition", "[", "]"}]], "Input", CellChangeTimes->{{3.621060023280203*^9, 3.621060028057797*^9}}, FontSize->16, Background->RGBColor[0.87, 0.94, 1]], Cell[BoxData[ RowBox[{"Dynamic", "[", RowBox[{"MousePosition", "[", "]"}], "]"}]], "Input", CellChangeTimes->{{3.621060020809596*^9, 3.62106002081079*^9}}, FontSize->16, Background->RGBColor[0.87, 0.94, 1]], Cell["Dynamic[...] can give infinite loops :", "Text", CellChangeTimes->{{3.621574280248806*^9, 3.621574304435237*^9}, { 3.621575481364974*^9, 3.621575487085273*^9}}, FontSize->16], Cell[BoxData[{ RowBox[{ RowBox[{"y", "=", "0"}], ";"}], "\[IndentingNewLine]", RowBox[{"Dynamic", "[", RowBox[{"y", "=", RowBox[{"y", "+", "1"}]}], "]"}]}], "Input", CellChangeTimes->{{3.621574317137621*^9, 3.621574338177497*^9}, { 3.696242336290431*^9, 3.696242341823969*^9}}, FontSize->16, Background->RGBColor[0.87, 0.94, 1]], Cell["\<\ However, an assignment like t = RandomReal[ ...] inside Dynamic[ ...] does \ not constantly update itself as fast as possible, because this function is \ not \"ticklish\", in the sense that it does not trigger updates.\ \>", "Text", CellChangeTimes->{{3.621574540666182*^9, 3.621574615754306*^9}, { 3.621574737211232*^9, 3.621574737643209*^9}}, TextJustification->1., FontSize->16], Cell[BoxData[{ RowBox[{ RowBox[{"t", "=", "0"}], ";"}], "\[IndentingNewLine]", RowBox[{"Dynamic", "[", RowBox[{"t", "=", RowBox[{"RandomReal", "[", "10", "]"}]}], "]"}]}], "Input", CellChangeTimes->{{3.621574629783342*^9, 3.621574649622836*^9}, { 3.621574733456224*^9, 3.621574747307871*^9}, {3.6962428393616*^9, 3.696242839872497*^9}, {3.6962428709234753`*^9, 3.696242871819119*^9}}, FontSize->16, Background->RGBColor[0.87, 0.94, 1]] }, Open ]] }, Open ]], Cell[CellGroupData[{ Cell["Using DynamicModule[...] and Dynamic[...]", "Section", CellChangeTimes->{{3.621575106102281*^9, 3.621575117453587*^9}, { 3.621575148065096*^9, 3.62157516946173*^9}, {3.621575439436845*^9, 3.621575457525155*^9}}], Cell["\<\ Suppose now that we want to evaluate a polynomial expression like ax^2+bx+c \ such that the output always reflects the current value of x. The coefficients \ a, b and c are assumed fixed, so any changes to these coefficients later on \ in the notebook should not trigger any retroactive dynamic updates of the \ expression ax^2+bx+c.\ \>", "Text", CellChangeTimes->{{3.6215755058944263`*^9, 3.621575525141094*^9}, { 3.621575622030567*^9, 3.621575852478266*^9}, {3.621575920332952*^9, 3.6215759229731483`*^9}}, TextJustification->1., FontSize->16], Cell[BoxData[{ RowBox[{ RowBox[{"a", "=", "1"}], ";"}], "\[IndentingNewLine]", RowBox[{ RowBox[{"b", "=", "2"}], ";"}], "\[IndentingNewLine]", RowBox[{ RowBox[{"c", "=", "3"}], ";"}], "\[IndentingNewLine]", RowBox[{ RowBox[{"x", "=", "0"}], ";"}], "\[IndentingNewLine]", RowBox[{"Dynamic", "[", RowBox[{ RowBox[{"a", "*", RowBox[{"x", "^", "2"}]}], "+", RowBox[{"b", "*", "x"}], "+", "c"}], "]"}]}], "Input", CellChangeTimes->{{3.621575957968865*^9, 3.621576044904512*^9}, { 3.621576161196393*^9, 3.6215761829644136`*^9}, {3.6215762850655727`*^9, 3.621576288299081*^9}}, FontSize->14, Background->RGBColor[0.87, 0.94, 1]], Cell["\<\ Try now to change the value of x to 1, then to change the value of c to 1000. \ What do you notice?\ \>", "Text", CellChangeTimes->{{3.6215760599316263`*^9, 3.621576096012384*^9}, { 3.621576208327376*^9, 3.6215762138872557`*^9}}, FontSize->16], Cell[BoxData[ RowBox[{ RowBox[{"x", "=", "1"}], ";"}]], "Input", CellChangeTimes->{{3.6215768602693167`*^9, 3.62157686395026*^9}}, FontSize->14, Background->RGBColor[0.87, 0.94, 1]], Cell[BoxData[ RowBox[{ RowBox[{"c", "=", "1000"}], ";"}]], "Input", CellChangeTimes->{{3.621576873820804*^9, 3.621576876390287*^9}}, FontSize->14, Background->RGBColor[0.87, 0.94, 1]], Cell[CellGroupData[{ Cell[TextData[{ StyleBox["DynamicModule[{x,y,\[Ellipsis]},expr]", FontColor->GrayLevel[0]], "\[LineSeparator]represents an object which maintains the same local \ instance of the local variables x, y, \[Ellipsis] in the course of all \ evaluations of Dynamic objects in expr. Variables specified in a \ DynamicModule will by default have their values maintained from one dynamic \ update to the next. " }], "Subsection", CellChangeTimes->{ 3.621180891138138*^9, 3.6211811694025593`*^9, {3.696241047747867*^9, 3.6962410502757177`*^9}, 3.696241224370274*^9}], Cell[CellGroupData[{ Cell["Differences between Module and DynamicModule", "Subsubsection", CellChangeTimes->{{3.696241180805106*^9, 3.696241205596609*^9}}], Cell["\<\ Module[..] and DynamicModule[..] are both used for localization of variables \ (creating local variables). Module[..] stores the variables in the kernel and \ is used for evaluations, DynamicModule[..] stores its variables in the \ front-end and is used for Dynamic output and interface constructions.\ \>", "Text", CellChangeTimes->{{3.696240812833644*^9, 3.69624088646175*^9}, { 3.6962409838643837`*^9, 3.6962410270101748`*^9}, {3.6962410753455048`*^9, 3.696241152793894*^9}}], Cell["\<\ Let us now clear our variables and try again, this time using a \ DynamicModule[...].\ \>", "Text", CellChangeTimes->{{3.621576698008013*^9, 3.6215767007479467`*^9}, { 3.6215767720317593`*^9, 3.6215767837512827`*^9}, 3.62161601535655*^9}, FontSize->16, FontColor->GrayLevel[0]], Cell[BoxData[{ RowBox[{"Clear", "[", RowBox[{"a", ",", "b", ",", "c", ",", "x"}], "]"}], "\[IndentingNewLine]", RowBox[{"DynamicModule", "[", RowBox[{ RowBox[{"{", RowBox[{"a", ",", "b", ",", "c"}], "}"}], ",", "\[IndentingNewLine]", RowBox[{ RowBox[{"a", "=", "1"}], ";", "\[IndentingNewLine]", RowBox[{"b", "=", "2"}], ";", "\[IndentingNewLine]", RowBox[{"c", "=", "3"}], ";", "\[IndentingNewLine]", RowBox[{"x", "=", "0"}], ";", "\[IndentingNewLine]", RowBox[{"Dynamic", "[", RowBox[{ RowBox[{"a", "*", RowBox[{"x", "^", "2"}]}], "+", RowBox[{"b", "*", "x"}], "+", "c"}], "]"}]}]}], "]"}]}], "Input", CellChangeTimes->{{3.621575957968865*^9, 3.621576044904512*^9}, { 3.621576161196393*^9, 3.6215761829644136`*^9}, {3.6215762850655727`*^9, 3.621576288299081*^9}, {3.621576367303056*^9, 3.621576383029752*^9}, { 3.62157645728028*^9, 3.62157645896891*^9}, {3.621576562618976*^9, 3.6215765668088913`*^9}, {3.621576616225738*^9, 3.621576617369712*^9}, { 3.621577488308707*^9, 3.6215774893548594`*^9}, {3.621577712567995*^9, 3.621577714759163*^9}, {3.69624006184226*^9, 3.696240063422756*^9}}, FontSize->14, Background->RGBColor[0.87, 0.94, 1]], Cell["\<\ Try again to change the value of x to 1, then to change the value of c to \ 1000. What do you notice?\ \>", "Text", CellChangeTimes->{{3.6215760599316263`*^9, 3.621576096012384*^9}, { 3.621576208327376*^9, 3.6215762138872557`*^9}, {3.621578424139832*^9, 3.621578425642486*^9}, {3.6962403043888702`*^9, 3.696240318141117*^9}}, FontSize->16] }, Open ]] }, Open ]] }, Open ]], Cell[CellGroupData[{ Cell["Back to Exercise 3", "Section", CellChangeTimes->{{3.621579245813595*^9, 3.621579247012198*^9}, { 3.696240118552713*^9, 3.696240122327668*^9}}], Cell[CellGroupData[{ Cell["\<\ Rewrite the solution to Exercise 3, so that the value of x gets updated ONLY \ when the parameters min,max are changed, and NOT when we change a,b,c. \ \>", "Subsubsection", CellChangeTimes->{{3.621578986324168*^9, 3.6215790449316893`*^9}, { 3.621579077299336*^9, 3.621579101523472*^9}, {3.6962401548207283`*^9, 3.6962402019651413`*^9}}, TextJustification->1.], Cell[BoxData[ RowBox[{"Manipulate", "[", RowBox[{ RowBox[{"DynamicModule", "[", RowBox[{ RowBox[{"{", "x", "}"}], ",", "\[IndentingNewLine]", RowBox[{ RowBox[{"x", "=", RowBox[{"RandomReal", "[", RowBox[{"{", RowBox[{"min", ",", " ", "max"}], "}"}], "]"}]}], ";", "\[IndentingNewLine]", RowBox[{"Dynamic", "[", RowBox[{"Row", "[", RowBox[{"{", RowBox[{ "\"\\"", ",", "x", ",", "\"\< a=\>\"", ",", "a", ",", "\"\< b=\>\"", ",", "b", ",", "\"\< c=\>\"", ",", "c", ",", " ", "\"\< \!\(\*SuperscriptBox[\(ax\), \(2\)]\)+bx+c=\>\"", ",", " ", RowBox[{ RowBox[{"a", "*", RowBox[{"x", "^", "2"}]}], "+", RowBox[{"b", "*", "x"}], "+", "c"}]}], "}"}], "]"}], "]"}]}]}], "]"}], ",", "\[IndentingNewLine]", RowBox[{"{", RowBox[{"a", ",", "1", ",", "10"}], "}"}], ",", "\[IndentingNewLine]", RowBox[{"{", RowBox[{"b", ",", "1", ",", "10"}], "}"}], ",", "\[IndentingNewLine]", RowBox[{"{", RowBox[{"c", ",", "1", ",", "10"}], "}"}], ",", " ", "\[IndentingNewLine]", RowBox[{"{", RowBox[{"min", ",", RowBox[{"{", RowBox[{"2", ",", "3", ",", "4"}], "}"}]}], "}"}], ",", "\[IndentingNewLine]", RowBox[{"{", RowBox[{"max", ",", RowBox[{"{", RowBox[{"5", ",", "6", ",", "7"}], "}"}]}], "}"}], ",", "\[IndentingNewLine]", RowBox[{"ContentSize", "\[Rule]", RowBox[{"{", RowBox[{"400", ",", "80"}], "}"}]}]}], "]"}]], "Input", CellChangeTimes->{{3.621577945501072*^9, 3.6215780576898003`*^9}, { 3.6215785094934683`*^9, 3.621578509923173*^9}, {3.621578580180879*^9, 3.621578627543001*^9}, {3.653097294775337*^9, 3.653097326547574*^9}, { 3.6530973571486273`*^9, 3.653097359466344*^9}, {3.653097399737907*^9, 3.653097430510686*^9}, {3.696057818972686*^9, 3.696057822667268*^9}, { 3.696239628148497*^9, 3.696239879246036*^9}, {3.696241305906369*^9, 3.6962413781160173`*^9}, {3.696242972593285*^9, 3.69624298077667*^9}}, FontSize->14, Background->RGBColor[0.88, 1, 0.88]], Cell[TextData[{ "Recall that ", StyleBox["Row[..]", FontWeight->"Bold"], ", ", StyleBox["Column[..]", FontWeight->"Bold"], ", ", StyleBox["GraphicsRow[..],", FontWeight->"Bold"], " ", StyleBox["GraphicsColumn[..]", FontWeight->"Bold"], ", ", StyleBox["GraphicsGrid[..]", FontWeight->"Bold"], " are just different ways of combining different graphics elements in the \ same row, column, table, etc. They are very useful when doing interactive \ models." }], "Text", CellChangeTimes->{{3.69624860968983*^9, 3.696248724974901*^9}, { 3.696248867540805*^9, 3.696248879787073*^9}}] }, Open ]] }, Open ]], Cell["Graphs Revisited", "Section", CellChangeTimes->{{3.696246551930511*^9, 3.696246568208753*^9}}], Cell[CellGroupData[{ Cell["Recall :", "Section", CellChangeTimes->{{3.651473083862565*^9, 3.651473097310361*^9}, { 3.651476077165557*^9, 3.6514760809511833`*^9}, {3.651478423772172*^9, 3.6514784247229652`*^9}}], Cell[TextData[{ "Recall that we can define a graph by specifying the list of vertices and \ the list of edges and other options.The generic command is ", StyleBox["Graph[VertexList, EdgeList, Options]", FontWeight->"Bold"], ". Sometimes it is desired to label the nodes of the graph using something \ other than numbers." }], "Text", CellChangeTimes->{{3.651476092140902*^9, 3.651476134930942*^9}, { 3.651476688823387*^9, 3.6514768039053698`*^9}}], Cell[BoxData[ RowBox[{"G", "=", RowBox[{"Graph", "[", RowBox[{ RowBox[{"{", RowBox[{"1", ",", "2", ",", "3", ",", "4"}], "}"}], ",", RowBox[{"{", RowBox[{ RowBox[{"1", "\[Rule]", "2"}], ",", RowBox[{"2", "\[Rule]", "3"}], ",", RowBox[{"2", "\[Rule]", "4"}], ",", RowBox[{"4", "\[Rule]", "3"}], ",", RowBox[{"4", "\[Rule]", "1"}], ",", RowBox[{"4", "\[Rule]", "2"}]}], "}"}], ",", RowBox[{"VertexLabels", "\[Rule]", RowBox[{"{", RowBox[{ RowBox[{"1", "\[Rule]", "\"\\""}], ",", RowBox[{"2", "\[Rule]", "\"\\""}], ",", RowBox[{"3", "\[Rule]", "\"\\""}], ",", RowBox[{"4", "\[Rule]", "\"\\""}]}], "}"}]}], ",", RowBox[{"EdgeLabels", "\[Rule]", RowBox[{"{", RowBox[{ RowBox[{ RowBox[{"(", RowBox[{"4", "\[Rule]", "1"}], ")"}], "->", "\"\\""}], ",", " ", RowBox[{ RowBox[{"(", RowBox[{"2", "\[Rule]", "3"}], ")"}], "->", "\"\\""}]}], "}"}]}]}], "]"}]}]], "Input", CellChangeTimes->{{3.65147682753122*^9, 3.6514770051939373`*^9}, { 3.6514771018212147`*^9, 3.65147711201649*^9}, {3.651477155365938*^9, 3.651477178010784*^9}, {3.651477214470396*^9, 3.65147721492614*^9}, { 3.6962466862347507`*^9, 3.696246708881813*^9}, {3.696246756563952*^9, 3.696246775131537*^9}}, Background->RGBColor[0.87, 0.94, 1]], Cell[TextData[{ StyleBox["VertexList[..]", FontWeight->"Bold"], " and ", StyleBox["EdgeList[..]", FontWeight->"Bold"], " give the list of vertices and respectively the list of edges of a graph. \ ", StyleBox["PropertyValue[..]", FontWeight->"Bold"], " can be used to retrieve the list of labels." }], "Text", CellChangeTimes->{{3.651478153228176*^9, 3.65147827190137*^9}}], Cell[BoxData[{ RowBox[{"VertexList", "[", "G", "]"}], "\[IndentingNewLine]", RowBox[{"EdgeList", "[", "G", "]"}], "\[IndentingNewLine]", RowBox[{"PropertyValue", "[", RowBox[{"G", ",", "VertexLabels"}], "]"}], "\[IndentingNewLine]", RowBox[{"PropertyValue", "[", RowBox[{"G", ",", "EdgeLabels"}], "]"}]}], "Input", CellChangeTimes->{{3.651477222560692*^9, 3.651477250172134*^9}, { 3.69624682239736*^9, 3.6962468232962217`*^9}}, Background->RGBColor[0.87, 0.94, 1]], Cell[TextData[{ "If the ordered list of labels is desired, one can use ", StyleBox["Sort[..]", FontWeight->"Bold"], "." }], "Text", CellChangeTimes->{{3.651478285832387*^9, 3.651478349682342*^9}}], Cell[BoxData[{ RowBox[{ RowBox[{"L", "=", RowBox[{"PropertyValue", "[", RowBox[{"G", ",", "VertexLabels"}], "]"}]}], ";"}], "\[IndentingNewLine]", RowBox[{"Sort", "[", RowBox[{"L", ",", "Greater"}], "]"}], "\[IndentingNewLine]", RowBox[{"Sort", "[", "L", "]"}]}], "Input", CellChangeTimes->{{3.651478360396891*^9, 3.651478389411085*^9}}, Background->RGBColor[0.87, 0.94, 1]], Cell[TextData[{ "There are several options to plot graphs nicely, like ", StyleBox["GraphPlot[..]", FontWeight->"Bold"], " and ", StyleBox["HighlightGraph[..].", FontWeight->"Bold"] }], "Text", CellChangeTimes->{{3.696246888927333*^9, 3.696246974904154*^9}}], Cell[BoxData[{ RowBox[{"HighlightGraph", "[", RowBox[{"G", ",", RowBox[{"{", RowBox[{"1", ",", "4", ",", RowBox[{"2", "\[Rule]", "3"}]}], "}"}]}], "]"}], "\[IndentingNewLine]", RowBox[{"HighlightGraph", "[", RowBox[{"G", ",", RowBox[{"{", RowBox[{ RowBox[{"Style", "[", RowBox[{ RowBox[{"{", RowBox[{"1", ",", "4"}], "}"}], ",", "Green"}], "]"}], ",", RowBox[{"Style", "[", RowBox[{ RowBox[{"2", "\[Rule]", "3"}], ",", "Blue"}], "]"}]}], "}"}], ",", " ", RowBox[{"VertexSize", "\[Rule]", "Medium"}]}], "]"}]}], "Input", CellChangeTimes->{{3.651479266235929*^9, 3.651479293611443*^9}, { 3.651481962966771*^9, 3.651481963134536*^9}, {3.651482000100148*^9, 3.6514820005319366`*^9}, {3.696247418182379*^9, 3.6962474956865177`*^9}}, Background->RGBColor[0.87, 0.94, 1]] }, Open ]], Cell[CellGroupData[{ Cell["Exercise :", "Section", CellChangeTimes->{{3.6514784366713877`*^9, 3.651478444592558*^9}, { 3.696247995173765*^9, 3.696247995771352*^9}}], Cell[TextData[{ "Open the ", StyleBox["Mathematica", FontSlant->"Italic"], " notebook Map.nb (MetroStationMap).The variable called ", StyleBox["Metro", FontWeight->"Bold"], " is a list of 5 graphs with the metro networks in Seoul, New York, Paris, \ Mexico City and London. The vertices of the graphs are labeled with the names \ of the stations. The variable ", StyleBox["CityName", FontWeight->"Bold"], " is a list which contains the names of the cities." }], "Text", CellChangeTimes->{{3.651473125445464*^9, 3.651473194254459*^9}, { 3.651476055689809*^9, 3.6514760645593643`*^9}, {3.6514773816897383`*^9, 3.651477556778808*^9}, {3.651478579565559*^9, 3.651478633494419*^9}, { 3.651478714327673*^9, 3.65147874669571*^9}, {3.651478978689926*^9, 3.651478991357911*^9}, 3.651479180970708*^9, {3.651480531613593*^9, 3.651480536102968*^9}, 3.651484158166214*^9, {3.654488222420248*^9, 3.654488241635373*^9}, {3.6962479080196533`*^9, 3.696247933857871*^9}}], Cell[TextData[{ StyleBox["(a).", FontWeight->"Bold"], " Write an interactive program using ", StyleBox["Manipulate[...]", FontWeight->"Bold"], " that has a control button with five values 1,2,3,4,5. When the user \ selects a given value, you display the name of the city and the corresponding \ metro map. Because your program uses some global variables defined outside \ Manipulate (like ", StyleBox["Metro", FontWeight->"Bold"], "), you should also set ", StyleBox["SaveDefinitions\[Rule]True", FontWeight->"Bold"], ". " }], "Text", CellDingbat->"\[FilledSquare]", CellChangeTimes->{{3.6205018504224777`*^9, 3.6205019558072147`*^9}, { 3.620502092122567*^9, 3.620502118642055*^9}, {3.620504107861274*^9, 3.620504118677207*^9}, {3.620504650373715*^9, 3.620504654356748*^9}, { 3.651479172022374*^9, 3.651479205931744*^9}, {3.65148416454828*^9, 3.651484169758423*^9}, {3.696247763255722*^9, 3.6962477638969183`*^9}}], Cell[TextData[{ StyleBox["(b).", FontWeight->"Bold"], " Expand the model from part (a) with a second control which gets populated \ with the vertices of the corresponding graph. When the user selects a vertex \ from the list, you show the corresponding metro station highlighted in green \ (or with increased size) on the metro network. " }], "Text", CellDingbat->"\[FilledSquare]", CellChangeTimes->{{3.620454257072255*^9, 3.620454320940864*^9}, { 3.620459117742785*^9, 3.620459123757498*^9}, {3.620459179741609*^9, 3.620459252592271*^9}, {3.620501146329939*^9, 3.620501150841432*^9}, { 3.620502274706925*^9, 3.620502438138816*^9}, {3.620502893541544*^9, 3.6205028981979094`*^9}, {3.6205029373089857`*^9, 3.620502940413299*^9}, { 3.620504087401848*^9, 3.6205040899771357`*^9}, {3.6205046592762547`*^9, 3.620504725990114*^9}, {3.651479573995905*^9, 3.651479739435196*^9}, { 3.65148036745498*^9, 3.651480374362545*^9}, {3.651482762863699*^9, 3.651482764361018*^9}, {3.6514828404851418`*^9, 3.651482879777258*^9}}], Cell[TextData[{ StyleBox["(c).", FontWeight->"Bold"], " Make an interactive model from part (b) with nodes v an u as parameters, \ that highlights the shortest path from v to u, if there exists such a path in \ the graph, otherwise it only highlights the two vertices. Use the function ", StyleBox["FindShortestPath[...].", FontWeight->"Bold"] }], "Text", CellDingbat->"\[FilledSquare]", CellChangeTimes->{{3.620504056472845*^9, 3.620504071805513*^9}, { 3.620504206142955*^9, 3.620504340855125*^9}, {3.620504667580283*^9, 3.620504669860676*^9}, {3.651482755732914*^9, 3.6514828175540037`*^9}, 3.696247840068157*^9}], Cell[TextData[{ StyleBox["(d).", FontWeight->"Bold"], " Make an interactive model that highlights a vertex v of G2 together with \ its parents and children (i.e. if there is an edge v->u, then v is called a \ parent node and u a child node). The function NeighborhoodGraph[...] might be \ of help." }], "Text", CellDingbat->"\[FilledSquare]", CellChangeTimes->{{3.6205029070584927`*^9, 3.620503009132915*^9}, { 3.620503098625575*^9, 3.620503161638495*^9}, {3.620503648006332*^9, 3.620503662445695*^9}, {3.62050436923568*^9, 3.62050437314615*^9}, { 3.6205044349466553`*^9, 3.6205044354664383`*^9}, {3.620504674724834*^9, 3.620504676620702*^9}, {3.620504754604332*^9, 3.620504768330378*^9}, { 3.620504968099605*^9, 3.620504991866219*^9}, {3.620505033972052*^9, 3.620505037762969*^9}, {3.65148045998134*^9, 3.651480475475801*^9}, { 3.651482627201078*^9, 3.651482630305168*^9}}], Cell[TextData[{ StyleBox["(e). Optional (Combined Interactive Model): ", FontWeight->"Bold"], "Parameters: nodes v and u, and an extra parameter \ \[OpenCurlyDoubleQuote]option\[CloseCurlyDoubleQuote] with values {b,c,d}. If \ \[OpenCurlyDoubleQuote]option\[CloseCurlyDoubleQuote] b is selected, then the \ model outputs the same thing as part (b), If \[OpenCurlyDoubleQuote]option\ \[CloseCurlyDoubleQuote] c is selected, then the model outputs the same thing \ as part (c) etc. If you skipped some part, say (d), then you should allow \ only two values for \[OpenCurlyDoubleQuote]option\[CloseCurlyDoubleQuote], \ {b,c}." }], "Text", CellDingbat->"\[FilledSquare]", CellChangeTimes->{{3.620504822508637*^9, 3.620504936927032*^9}, { 3.620505074629816*^9, 3.6205052702011347`*^9}, {3.6514831088995037`*^9, 3.651483145658943*^9}, {3.6514838088733664`*^9, 3.651483814500269*^9}, 3.696244701940352*^9}] }, Open ]] }, WindowSize->{808, 606}, WindowMargins->{{Automatic, 185}, {Automatic, 0}}, FrontEndVersion->"11.0 for Mac OS X x86 (32-bit, 64-bit Kernel) (September \ 21, 2016)", StyleDefinitions->"Default.nb" ] (* End of Notebook Content *) (* Internal cache information *) (*CellTagsOutline CellTagsIndex->{} *) (*CellTagsIndex CellTagsIndex->{} *) (*NotebookFileOutline Notebook[{ Cell[CellGroupData[{ Cell[580, 22, 178, 2, 64, "Section"], Cell[CellGroupData[{ Cell[783, 28, 523, 10, 88, "Subsection"], Cell[1309, 40, 1014, 25, 133, "Input"], Cell[CellGroupData[{ Cell[2348, 69, 101, 1, 35, "Subsubsection"], Cell[2452, 72, 874, 23, 133, "Input"] }, Open ]] }, Open ]], Cell[CellGroupData[{ Cell[3375, 101, 773, 15, 88, "Subsection"], Cell[4151, 118, 529, 15, 112, "Input"], Cell[CellGroupData[{ Cell[4705, 137, 162, 2, 35, "Subsubsection"], Cell[4870, 141, 830, 22, 139, "Input"] }, Open ]], Cell[CellGroupData[{ Cell[5737, 168, 227, 3, 35, "Subsubsection"], Cell[5967, 173, 912, 25, 139, "Input"] }, Open ]], Cell[CellGroupData[{ Cell[6916, 203, 231, 3, 35, "Subsubsection"], Cell[7150, 208, 866, 23, 139, "Input"] }, Open ]] }, Open ]], Cell[CellGroupData[{ Cell[8065, 237, 1064, 17, 134, "Subsection"], Cell[CellGroupData[{ Cell[9154, 258, 162, 2, 35, "Subsubsection"], Cell[9319, 262, 1877, 47, 237, "Input"] }, Open ]] }, Open ]] }, Open ]], Cell[CellGroupData[{ Cell[11257, 316, 105, 1, 64, "Section"], Cell[11365, 319, 235, 6, 33, "Text"], Cell[11603, 327, 189, 5, 52, "Input"], Cell[CellGroupData[{ Cell[11817, 336, 166, 2, 35, "Subsubsection"], Cell[11986, 340, 191, 5, 52, "Input"] }, Open ]], Cell[CellGroupData[{ Cell[12214, 350, 169, 2, 35, "Subsubsection"], Cell[12386, 354, 226, 6, 52, "Input"], Cell[12615, 362, 345, 8, 55, "Text"], Cell[12963, 372, 212, 6, 52, "Input"], Cell[13178, 380, 575, 11, 77, "Text"], Cell[13756, 393, 180, 4, 52, "Input"], Cell[13939, 399, 213, 5, 52, "Input"], Cell[14155, 406, 184, 3, 33, "Text"], Cell[14342, 411, 345, 9, 79, "Input"], Cell[14690, 422, 396, 8, 77, "Text"], Cell[15089, 432, 455, 10, 79, "Input"] }, Open ]] }, Open ]], Cell[CellGroupData[{ Cell[15593, 448, 223, 3, 64, "Section"], Cell[15819, 453, 564, 11, 99, "Text"], Cell[16386, 466, 661, 18, 139, "Input"], Cell[17050, 486, 257, 6, 33, "Text"], Cell[17310, 494, 187, 5, 50, "Input"], Cell[17500, 501, 189, 5, 50, "Input"], Cell[CellGroupData[{ Cell[17714, 510, 567, 11, 144, "Subsection"], Cell[CellGroupData[{ Cell[18306, 525, 135, 1, 35, "Subsubsection"], Cell[18444, 528, 494, 8, 68, "Text"], Cell[18941, 538, 293, 7, 33, "Text"], Cell[19237, 547, 1231, 25, 183, "Input"], Cell[20471, 574, 356, 7, 55, "Text"] }, Open ]] }, Open ]] }, Open ]], Cell[CellGroupData[{ Cell[20888, 588, 152, 2, 64, "Section"], Cell[CellGroupData[{ Cell[21065, 594, 377, 7, 59, "Subsubsection"], Cell[21445, 603, 2146, 52, 262, "Input"], Cell[23594, 657, 599, 21, 68, "Text"] }, Open ]] }, Open ]], Cell[24220, 682, 101, 1, 64, "Section"], Cell[CellGroupData[{ Cell[24346, 687, 195, 3, 50, "Section"], Cell[24544, 692, 455, 9, 68, "Text"], Cell[25002, 703, 1446, 37, 91, "Input"], Cell[26451, 742, 385, 12, 49, "Text"], Cell[26839, 756, 481, 9, 112, "Input"], Cell[27323, 767, 202, 6, 30, "Text"], Cell[27528, 775, 395, 9, 91, "Input"], Cell[27926, 786, 267, 8, 30, "Text"], Cell[28196, 796, 856, 21, 70, "Input"] }, Open ]], Cell[CellGroupData[{ Cell[29089, 822, 146, 2, 64, "Section"], Cell[29238, 826, 989, 20, 68, "Text"], Cell[30230, 848, 943, 22, 87, "Text"], Cell[31176, 872, 1039, 17, 68, "Text"], Cell[32218, 891, 635, 13, 68, "Text"], Cell[32856, 906, 897, 16, 68, "Text"], Cell[33756, 924, 916, 16, 87, "Text"] }, Open ]] } ] *)