Solutions to the exercises from last time:

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?

Manipulate[
 total=total+step;
{step,total},
{{total,0},-1000,1000,1},{{step,0},-10,10,1},
FrameLabel->"Good or bad?"]

Solution :

Manipulate[
 sum=total+step;
{step, total,sum},
{{total,0},-1000,1000,1},{{step,0},-10,10,1},
FrameLabel->"Good"]

Exercise 2. A way to compute the sum of the first m positive integers in Mathematica 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}.

m=5;
s=0;
For[i=1,i≤m,i++, s=s+i];
s

Imperfect Solution :

Manipulate[
s=0;
For[i=1,i≤m,i++, s=s+i];
s,
{m,{10,20,30,40,50}}]

Solution 1 :

Manipulate[Module[{s,i},
s=0;
For[i=1,i≤m,i++, s=s+i];
s],
{m,{10,20,30,40,50}}]

Solution 2 :

Manipulate[
s=0;
For[i=1,i≤m,i++, s=s+i];
s,
{m,{10,20,30,40,50}}, TrackedSymbols:>{m}]

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}).

Imperfect Solution :

Manipulate[
x=RandomReal[{min, max}];
Row[{"x=",x," a=",a," b=",b," c=",c, " (ax^2)+bx+c=", a*x^2+b*x+c}],
{a,1,10},
{b,1,10},
{c,1,10}, 
{min,{2,3,4}},
{max,{5,6,7}},
ContentSize->{400,80}]

Using Dynamic[...]

Let us define the variable t and then perform a simple computation, like t^2+5.

t=10;

This is a static output:

t^2+5

This is a dynamic output:

Dynamic[t^2+5]

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.

t=6;

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.

MousePosition[]

Dynamic[MousePosition[]]

Dynamic[...] can give infinite loops :

y=0;
Dynamic[y=y+1]

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.

t=0;
Dynamic[t=RandomReal[10]]

Using DynamicModule[...] and Dynamic[...]

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.

a=1;
b=2;
c=3;
x=0;
Dynamic[a*x^2+b*x+c]

Try now to change the value of x to 1, then to change the value of c to 1000. What do you notice?

x=1;

c=1000;

DynamicModule[{x,y,…},expr]
represents an object which maintains the same local instance of the local variables x, y, … 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.

Differences between Module and DynamicModule

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.

Let us now clear our variables and try again, this time using a DynamicModule[...].

Clear[a,b,c,x]
DynamicModule[{a,b,c},
a=1;
b=2;
c=3;
x=0;
Dynamic[a*x^2+b*x+c]]

Try again to change the value of x to 1, then to change the value of c to 1000. What do you notice?

Back to Exercise 3

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.

Manipulate[DynamicModule[{x},
x=RandomReal[{min, max}];
Dynamic[Row[{"x=",x," a=",a," b=",b," c=",c, " (ax^2)+bx+c=", a*x^2+b*x+c}]]],
{a,1,10},
{b,1,10},
{c,1,10}, 
{min,{2,3,4}},
{max,{5,6,7}},
ContentSize->{400,80}]

Recall that Row[..], Column[..], GraphicsRow[..], GraphicsColumn[..], GraphicsGrid[..] are just different ways of combining different graphics elements in the same row, column, table, etc. They are very useful when doing interactive models.

Graphs Revisited

Recall :

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 Graph[VertexList, EdgeList, Options]. Sometimes it is desired to label the nodes of the graph using something other than numbers.

G=Graph[{1,2,3,4},{1->2,2->3,2->4,4->3,4->1,4->2},VertexLabels->{1->"a",2->"b",3->"c",4->"d"},EdgeLabels->{(4->1)->"Edge da", (2->3)->"Edge bc"}]

VertexList[..] and EdgeList[..] give the list of vertices and respectively the list of edges of a graph. PropertyValue[..] can be used to retrieve the list of labels.

VertexList[G]
EdgeList[G]
PropertyValue[G,VertexLabels]
PropertyValue[G,EdgeLabels]

If the ordered list of labels is desired, one can use Sort[..].

L=PropertyValue[G,VertexLabels];
Sort[L,Greater]
Sort[L]

There are several options to plot graphs nicely, like GraphPlot[..] and HighlightGraph[..].

HighlightGraph[G,{1,4,2->3}]
HighlightGraph[G,{Style[{1,4},Green],Style[2->3,Blue]}, VertexSize->Medium]

Exercise :

Open the Mathematica notebook Map.nb (MetroStationMap).The variable called Metro 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 CityName is a list which contains the names of the cities.

(a). Write an interactive program using Manipulate[...] 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 Metro), you should also set SaveDefinitions->True.

(b). 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.

(c). 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 FindShortestPath[...].

(d). 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.

(e). Optional (Combined Interactive Model): Parameters: nodes v and u, and an extra parameter “option” with values {b,c,d}. If “option” b is selected, then the model outputs the same thing as part (b), If “option” 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 “option”, {b,c}.