Basic Mathematica for MATH 616

Note: Mathematica is not the CAS that I normally use, so there may be more elegant ways to do these things.

Plotting

To plot functions in Mathematica, use the Plot[expression,{variable,lower limit, upper limit}] syntax:

plot1=Plot[Sin[x],{x,0,2*Pi}]

Note that if you double-click on a function, then go to the Help menu, you’ll see an option for “Find selected function”, which will give you more details/examples about the syntax, options, etc.  (For instance, you can use the help function for the Plot command to find out how to change the axes ranges, turn lines different colors, etc.)

To plot more than one graph together, use the Show[objects] command:

plot2=Plot[Cos[x],{x,0,2*Pi}]
Show[plot1,plot2]

To graph a particular level set, use the ContourPlot[equation,{variable1,lower limit,upper limit},{variable2,lower limit,upper limit}] command:

ContourPlot[x^2+y^2==1,{x,-2,2},{y,-2,2}]

Phase Planes

One way to visualize a phase plane is by using the StreamPlot command.  This command plots the vector field, so the syntax is StreamPlot[{xdot,ydot},{x,lower limit,upper limit},{y,lower limit,upper limit}]:

xdot = -2*x+y
ydot = -3*x+y
stplot=StreamPlot[{xdot,ydot},{x,-0.5,0.5},{y,-0.5,0.5}]

To plot individual trajectories, you can use the online tools referenced from the course Web page, or the EquationTrekker command, whose syntax is tricky.  First we have to load the appropriate library:

<< EquationTrekker`

Next, the full equation has to be written out and the t variable must be indicated explicitly:

system={x'[t]==-2*x[t]+y[t],y'[t]==-3*x[t]+y[t]}

Then the syntax is EquationTrekker[system,{x,y},{t,0,tmax}].  This will span a new window; clicking on the pen and then the phase plane will draw a trajectory emanating from that point.  Closing the window will cause the plot to embed in the notebook.

trajplot=EquationTrekker[system,{x,y},{t,0,5}]

Maps

For maps, first we define the map as a function and give an initial condition in the form (0,(Subscript[x, 0])).

newx[x_]:=3.2*x*(1-x)
pointlist={{0,0.35}}

Next we create a For loop that generates iterates 1 through 20, incrementing i once each time through the loop (i++).  The Last command gets the last iterate, and applying Last again gets the x-value.  Then we create the next iterate, and use AppendTo to add it on to pointlist.

For[i=1,i<21,i++,
xn = Last[Last[pointlist]];
nextpt={i,newx[xn]};
AppendTo[pointlist,nextpt]
]

Hence our final result has all the iterates, and we can use ListPlot to display it.

pointlist
ListPlot[pointlist]

Note that for a two-dimensional map, you would just define another function newy, and the iterates would be of the form {x,y}, not {n,x}.