Day One Mathematica

This is the file that I walked through during the first day of class.  You will notice, however, that I have deleted the code that will plot the cobweb diagram itself.  The main component of the first lab assignment is to write a small program to do this.  Shift-Enter will allow you to execute commands.  Please use the Mathematica help guides for syntax - this will allow you to really get “the feel” of working with this language.  

I provide some guideposts for creating this program on the assignment itself  - please make sure you look there.  

The part that will take some thought and puzzling is creating the list of points for your lines to be drawn through.  I encourage you to generate these lists for a particular example (perhaps through liberal use of copy-paste) and get a diagram up and running before you extend the program to full generality.  For instance, plot the cobweb diagram for f(x) = 4x(1-x), initial condition x_0 = .1 and four iterations.  

Press Shift-Enter to execute the commands below.

f[x_]:=4*x*(1-x)

Plot[{x, f[x]}, {x, 0, 1}]

Table[k^2, {k, 1, 10}]

Nest is useful!   In place of writing f[f[f[.1]]], you write Nest[f, .1, 3].  
One thing to note is that f must be a function that you’ve previously defined, you cannot, for instance write Nest[x^2, .1, 3] to repeatedly apply f[x] = x^2.

SomeValues={.1, f[.1], Nest[f, .1, 2], Nest[f, .1, 3]}

If you would like to look at a time series for your system, you can use ListPlot to plot the location of your system over time.

ListPlot[SomeValues]

Now that you’re ready to start “cobwebbing”, it might be helpful to play with an example or two below.  

Within Plot, the command Prolog->{Line[{{0, 0}, {.1, .3}, {.8, .5}}]} will plot a line from (0, 0) to (.1, .3) and then from (.1, .3) to (.8, .5).  (If your list is longer, it keeps going in this way.)

Ask yourself: What are the endpoints of the lines in my cobweb diagram?  How can I make a list (using Table/Nest) that will have these points formatted correctly?

ListName = {{0, 0}, {.1, .3}, {.8, .5}}

Plot[x, {x,0, 1},  Prolog->{Line[ListName]}]

Flatten will remove all braces, if you use Flatten[Set, 2], it will remove slightly different sets of braces (using some logic that I can’t remember right now).

Flatten[{{{{0, 0}, {1, 1}}}, {{0, 0}, {2, 2}}}]

Flatten[{{{{0, 0}, {1, 1}}}, {{0, 0}, {2, 2}}}, 2]

Partition will allow you to add some in.

Partition[{0, 0, 1, 1}, 2]

Take will allow you to take the first n entries of your list.  Using “-n” instead will allow you to take the last n entries of your list.  And [[3]] will take the third element of your list.

Take[{0, 0, 1, 1}, 2]

{2, 5, 7, 12}[[2]]

I’ve included some of the formatting that I used to make the picture look nice (and readable on the screen in the classroom)... change the colors and thicknesses to see what happens.

Plot[{Sqrt[x], x}, {x,0, 1},PlotStyle->{{Thick, Red}, {Dashed, Thick,  Black}},  Prolog->{Darker[Blue], Thickness[.004], Line[ListName]}]