The UnTutorial

A few examples of how to do all sorts of different things that show up in community college math courses

If you are looking for a tutorial on how to use Mathematica, you'll have to wait until I have more time (and get motivated).  However, in the meantime, here are a set of quick examples showing you which commands to use to draw which types of graph and how to do other things you might want to do in an upper level community college math course. I will expand this as I have time and think of things that need to be added.  (Right now, this is mostly graphs, but I expect to add some material on integration, vectors, and coordinate systems over the course of the semester.  I expect a section on differential equations and matrices will show up this summer sometime...)

Graphing

Simple curves in 2-D

Functions

Plot[(x^(Sin[x])),{x,0,2π}]

Implicit equations

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

Notice the use of the double = sign here ==  Be SURE you use that, or this won't work (and very strange things can happen...).  Now, this graph doesn't look like we generally prefer in math classes, so let's try adding in a few options:

ContourPlot[(x^2)+3(y^2)==1,{x,-1,1},{y,-1,1},Axes->True]

ContourPlot[(x^2)+3(y^2)==1,{x,-1,1},{y,-1,1},Axes->True,Frame->False]

Polar functions

PolarPlot[2Sin[3θ]+1,{θ,0,2π}]

Level curves and level surfaces

2-D

f[x_,y_]:=(x^2)-x (y^3)

ContourPlot[f[x,y],{x,-5,5},{y,-5,5}]

Or, if you prefer plain:

ContourPlot[f[x,y],{x,-5,5},{y,-5,5},ContourShading->False,ContourLabels->Automatic]

3-D

f[x_,y_,z_]:=(x^2)+(y^2)-(z^3)

ContourPlot3D[f[x,y,z],{x,-5,5},{y,-5,5},{z,-5,5}]

ContourPlot3D[f[x,y,z],{x,-5,5},{y,-5,5},{z,-5,5},Contours->7]

ContourPlot3D[f[x,y,z],{x,-5,5},{y,-5,5},{z,-5,5},Contours->{-15,0,15}]

Surfaces

Functions

f[x_,y_]:=(x^2)-x (y^3)

Plot3D[f[x,y],{x,-5,5},{y,-5,5}]

I don't really like the way Mathematica handles the areas that get cut off (top/bottom) by default, so you can fix that with the following option:

Plot3D[f[x,y],{x,-5,5},{y,-5,5},ClippingStyle->None]

Plot3D[Sin[(x^2) y],{x,-5,5},{y,-5,5}]

This is kind of a mess, so let's tell Mathematica to spend a bit more effort and clean it up; use the option PlotPoints for this (this does slow down plotting, however):

Plot3D[Sin[(x^2) y],{x,-5,5},{y,-5,5},PlotPoints->100]

Implicit surfaces

ContourPlot3D[(x^2)-(y^2)-2(z^2)==1,{x,-5,5},{y,-5,5},{z,-5,5}]

Parametric

2-D Curves

ParametricPlot[{(1-t)Sin[t],t Cos[t]},{t,0,4π}]

center[t_]:={t,1};rotate[t_]:={-Sin[t],-Cos[t]};ParametricPlot[center[t]+rotate[t],{t,0,6π}]

ParametricPlot[{center[t],t rotate[t]},{t,0,2π}]

3-D Curves

helix={t,Sin[t],Cos[t]};ParametricPlot3D[helix,{t,0,4π}]

Surfaces

cone[u_,v_]:={u Cos[v],u Sin[v],u};
ParametricPlot3D[cone[u,v],{u,-2,2},{v,0,2π}]

plane={1,1,1}u+{-1,0,1}v +{1,-2,-3};ParametricPlot3D[{cone[u,v],plane},{u,-2,2},{v,0,2π}]

Regions

2-D

RegionPlot[1<(x^2)+(y^2)<4,{x,-3,3},{y,-3,3}]

To give it a more traditional look :

RegionPlot[1<(x^2)+(y^2)<4,{x,-3,3},{y,-3,3},Axes->True,Frame->False]

RegionPlot[0≤x≤1&&x≤y≤(Sqrt[x]),{x,0,2},{y,-2,2},Axes->True,Frame->False,PlotPoints->60]

3-D

RegionPlot3D[(z^2)≥(x^2)+(y^2)&&(z^2)+(x^2)+(y^2)≤1,{x,-2,2},{y,-2,2},{z,-2,2},PlotPoints->100]

RegionPlot3D[(z^2)≤(x^2)+(y^2)&&(z^2)+(x^2)+(y^2)≤1,{x,-2,2},{y,-2,2},{z,-2,2},PlotPoints->100]

Vector Fields

2-D

Before you can use any of these graphing function, you must load the following "package":

<<VectorFieldPlots`

fred[x_,y_]={-y,x};VectorFieldPlot[fred[x,y],{x,-5,5},{y,-5,5}]

VectorFieldPlot[fred[x,y],{x,-5,5},{y,-5,5},Axes->True]

Slope fields

Before you can use any of these graphing function, you must load the following "package":

<<VectorFieldPlots`

The main thing you need to know to plot a "slope field" is that this is just a normal vector field where the first component is 1 and the second component is the slope, so the field that corresponds to the differential equation y'=(x^2)y would be {1,(x^2)y}.

VectorFieldPlot[{1,(x^2) y},{x,-5,5},{y,-5,5},Axes->True]

3-D

Before you can use any of these graphing function, you must load the following "package":

<<VectorFieldPlots`

wilma[x_,y_,z_]:={x,-y,(z/(x+y))};VectorFieldPlot3D[wilma[x,y,z],{x,-5,5},{y,-5,5},{z,-5,5}]

Let's put heads on our vectors (though sadly, only ugly ones) and tell Mathematica to shut up about the division by 0:

VectorFieldPlot3D[wilma[x,y,z],{x,-5,5},{y,-5,5},{z,-5,5},VectorHeads->True]//Quiet

Combining graphs

Similar types of graphs

Plot3D[{(x^2)y,(x^2)-(y^2)},{x,-5,5},{y,-5,5},ClippingStyle->None,PlotStyle->{White,Green}]

ParametricPlot[{{Sin[t],Cos[t]},{Sin[t],Cos[3t]}},{t,0,2π},PlotStyle->{Thick,Dashed}]

ContourPlot3D[{(x^2)+(y^2)-(z^2)==1,0.5(x^2)-(y^2)-(z^2)==1,(z^2)==(x^2)+(y^2)},{x,-5,5},{y,-5,5},{z,-5,5},ContourStyle->{Opacity[0.4],Blue,Red}]

Different types of graphs

If you want to combine different graphs together that need different commands to plot, use the Show command:

ellipse=ContourPlot[(x^2)+3(y^2)==1,{x,-2,2},{y,-2,2}]

thingy=ParametricPlot[{Sin[t],Sin[(t^2)]},{t,0,2π}]

Show[ellipse,thingy]

curve=ParametricPlot3D[{t Sin[t],t Cos[t],t},{t,0,4π},PlotStyle->Thick]

surface=ParametricPlot3D[{v Sin[u],v Cos[u],v},{u,0,2π},{v,0,4π}]

Show[surface,curve]

Only try to combine graphs that are of the same dimension:

parabola=Plot[(x^2),{x,-5,5},PlotStyle->Thick]

paraboloid=Plot3D[(x^2)+(y^2),{x,-5,5},{y,-5,5}]

Show[paraboloid,parabola]

Families of graphs

Sometimes you want to see a whole set of related graphs together at one time.  Use the Table command for this:

Plot[Table[k (x^2)+ x,{k,-5,5}],{x,-5,5},PlotRange->{-5,5}]

Animation and math toys

Animation

Animate[ParametricPlot[{Sin[t],Cos[A t]},{t,0,6π},PlotPoints->80],{A,1,10}]

Manipulate / Math toys

Manipulate[ParametricPlot[{Sin[t],Cos[A t]},{t,0,6π},PlotPoints->80],{A,1,10}]

Not much different than Animate, so far.  However, you can use as many sliders (and other controls, if you want to look up the details in the Mathematica Help):

Manipulate[ParametricPlot[{B Sin[t],Cos[A t]},{t,0,6π},PlotPoints->80,PlotRange->{{-5,5},{-5,5}},AxesOrigin->{0,0}],{A,1,10},{B,-5,5}]

I used the AxesOrigin->{0,0} to tell Mathematica to only show the coordinate axes at (0,0).  (I have no idea why they don't make this the default; the default will move them around so you can aways see them...)  I used PlotRange to keep the view window steady.

You can also use Manipulate to change the settings for the graph:

Manipulate[ContourPlot3D[(x^2)+(y^2)-(z^2),{x,-5,5},{y,-5,5},{z,-5,5},Contours->nbrContours,ContourStyle->Opacity[opaque],Mesh->showMesh],{nbrContours,1,10,1},{opaque,1,0},{showMesh,{True,False}}]

More complicated controls are also available, if you want to learn to use them:

Manipulate[ContourPlot[((((x-center[[1]]))^2)/(a^2))-((((y-center[[2]]))^2)/(b^2))==1,{x,-10,10},{y,-10,10},Axes->True,Frame->False,PlotPoints->ControlActive[40,60]],{a,.5,10},{b,.5,10},{center,{-5,-5},{5,5}}]

Notice my use of PlotPoints->ControlActive[40, 60] here.  If you cut that part out, the graph looks terrible while you move the controls (Mathematica decreases the number of PlotPoints while you move the controls so it will respond quickly and smoothly).  So, this tells Mathematica to use PlotPoints->40 while moving controls and 60 afterwards, which seems to work better.  (This is a handy trick if your graph looks really awful while you move the controls.)

Differential Equations

Single equation - Slope fields and solutions

There are several ways to plot slope fields and solution curves for a single differential equation.  I'll show you the simplest way first, then I'll show a more complicated but easier to maintain (i.e., safer) method after that.  Before you can graph a slope field, you must first load the following package (if you haven't already):

<<VectorFieldPlots`

Now, a slope field for a single first-order differential equation can be graphed just by plotting the vector (list) {1,y'}.  Thus, if your ODE is y'=(t^2)-y, you would use the method below.  Notice that you have to use two different forms of the equation, one for the vector field, one for the actual solution.  This leads to all sorts of potential errors (e.g., if you change the equation on one but forget to change the other) and just general annoyances.  However, it is relatively easy to figure out.

slopes=VectorFieldPlot[{1,(t^2)-y},{t,-5,5},{y,-5,5},Axes->True]

You can list as many initial conditions as you want here (this could be made fancier if you wish):

y0={2,1,-3}

yCurves[t_]=y[t]/.NDSolve[{y'[t]==(t^2)-y[t],y[0]==y0},y[t],{t,0,5}]

Plot[yCurves[t],{t,0,5}]

If you want to get a little fancier, you can combine this (and skip the yCurves intermediary) in a single command:

solutions=Plot[Evaluate[y[t]/.NDSolve[{y'[t]==(t^2)-y[t],y[0]==y0},y[t],{t,0,5}]],{t,0,5}]

Now, superimpose the two graphs (you need to set the PlotRange on this or your vector field gets swamped):

Show[slopes,solutions,PlotRange->{{-5,5},{-5,5}}]

Now, let's look at a fancier method that doesn't require you to enter the same ODE twice (once for the slopes, once for the solutions); it is a little more complicated, but much safer to use in the long run.  The basic idea here is to define a function for the "right-hand" side of the differential equation y'=f(t,y) in such a way that you call it with y (for the slope field) and y(t) (for the solution curves):

f[t_,y_]:=(t^2)-y

slopes=VectorFieldPlot[{1,f[t,y]},{t,-5,5},{y,-5,5},Axes->True]

To find solutions, you do the following:

y0={2,1,-3}

yCurves[t_]=y[t]/.NDSolve[{y'[t]==f[t,y[t]],y[0]==y0},y[t],{t,0,5}]

Plot[yCurves[t],{t,0,5}]

Notice, I used f[t,y[t]] for the solution curves and f[t,y] for the slope field.  For those of you in the mood, here's one big command to do it all:

f[t_,y_]:=(t^2)-y;
y0={-2,-1,1,3};
Show[
VectorFieldPlot[{1,f[t,y]},{t,-5,5},{y,-5,5},Axes->True],
Plot[Evaluate[y[t]/.NDSolve[{y'[t]==f[t,y[t]],y[0]==y0},y[t],{t,0,5}]],{t,0,5}],
PlotRange->{{-5,5},{-5,5}}
]

Systems of first-order equations - Phase portraits and solutions

Basically, this works exactly as the above, except everything has two components, of course:

slopes=VectorFieldPlot[{x y,(x^2)+y},{x,-5,5},{y,-5,5},Axes->True]

solutionVector[t_]={x[t],y[t]}/.NDSolve[{x'[t]==x[t]y[t],y'[t]==((x[t])^2)+y[t],x[0]==-1,y[0]==0},{x[t],y[t]},{t,0,0.5}]

You have to use ParametricPlot to graph the solution curves:

ParametricPlot[Evaluate[solutionVector[t]],{t,0,0.5},AxesOrigin->{0,0}]

If you want to graph a set of solutions by specifying initial conditions, things get more complicated:

pt0=Transpose[{{4,-2},{1,1},{-4,-4}}]

solutionVector[t_]=Flatten[{x[t],y[t]}/.NDSolve[{x'[t]==x[t]y[t],y'[t]==((x[t])^2)+y[t],x[0]==pt0[[1]],y[0]==pt0[[2]]},{x[t],y[t]},{t,0,0.5}]]

solutions=ParametricPlot[Transpose[solutionVector[t]],{t,0,0.5}]

Now, superimpose (you need to set the PlotRange on this or your vector field gets swamped):

Show[slopes,solutions,PlotRange->{{-5,5},{-5,5}}]

As in single equations, you can use a snazzier method to avoid retyping the same equations multiple times (and decrease the chances of screwing up).  It is similar to the trick used with single equations.  We define two functions such that x'=f(x,y), y'=g(x,y):

f[x_,y_]:=x y;g[x_,y_]:=(x^2)+y

(You can actually get fancier and define a "list" or vector combining both of these, but that requires knowing a bit more about working with lists inside NDSolve, so I think we'll skip it for now...)

slopes=VectorFieldPlot[{f[x,y],g[x,y]},{x,-5,5},{y,-5,5},Axes->True]

solutionVector[t_]={x[t],y[t]}/.NDSolve[{x'[t]==f[x[t],y[t]],y'[t]==g[x[t],y[t]],x[0]==-1,y[0]==0},{x[t],y[t]},{t,0,0.5}]

ParametricPlot[Evaluate[solutionVector[t]],{t,0,0.5},AxesOrigin->{0,0}]

Again, here is a single command that does everything at once:

f[x_,y_]:=x y;g[x_,y_]:=(x^2)+y;
pt0=Transpose[{{4,-2},{1,1},{-4,-4}}];
Show[VectorFieldPlot[{f[x,y],g[x,y]},{x,-5,5},{y,-5,5},Axes->True],
ParametricPlot[Evaluate[Transpose[Flatten[{x[t],y[t]}/.NDSolve[{x'[t]==f[x[t],y[t]],y'[t]==g[x[t],y[t]],x[0]==pt0[[1]],y[0]==pt0[[2]]},{x[t],y[t]},{t,0,0.5}]]]],{t,0,0.5}],
PlotRange->{{-5,5},{-5,5}}]

(If it seems complicate, that's because it is...)  I do have an automated version of this posted on my website if you just want to see the solutions without having to code it in Mathematica yourself.

Vectors

Vector arithmetic

Vectors are entered as a list of numbers in Mathematica, so you could define several vectors:

vA={2,4};vB={-2,3};vC={0,1};

(You don't have to do this, but when naming vectors, I prefer to use something to make it clear that's what I have; in this case, I start each one with a v.  I could make it look really cool like (Overscript[A, ->]), but that takes too much time to use a whole lot, and I'm just too lazy to do that...)

vA+vB

3vA

2vA-7vC+3vB

Mathematica uses the . for dot products and × (see the palette on your right) for cross products:

vA.vB

vD={2,4,5};vE={0,-3,4};vD\[Cross]vE

You can find the length of a vector using the Norm function:

Norm[vD]

Or, remember that the dot product is the square of the length:

(Sqrt[vD.vD])

You can display vectors in Mathematica, but it isn't as easy as it could be:

origin={0,0};Graphics[{Arrow[{origin,vA}],Arrow[{origin,vC}],Arrow[{origin,vA+vC}]}]

Vector-valued function

To make a function vector-valued, just define it as a vector:

vF[x_,y_]:={-y,(x^2)}

vF[-3,2]

To be able to easily compose vector functions, however, you need to define your function as applying to a vector, rather than just 2 variables:

vG[{x_,y_}]:={-y,(x^2)}

It's a pain to use for single points:

vG[{1,2}]

However, if you want to compose it with another function:

vPolar[{r_,θ}]:={r Cos[θ],r Sin[θ]};f[{x_,y_}]:=(x^2)-(y^2)

vG[vPolar[{r,θ}]]

As opposed to trying it on vF:

vF[vPolar[{r,θ}]]

f[vG[vPolar[{r,θ}]]]

The built-in derivative operator D can find the derivative matrix:

D[vG[{x,y}],{{x,y}}]//MatrixForm

(The MatrixForm at the end just displays it as a matrix.)  Be careful to use the double {{x,y}} in this; if you only use a single bracket {x,y}, it interprets that as asking for a second derivative.  This works for scalar functions as well:

D[f[{x,y}],{{x,y}}]//MatrixForm

You can use this in vector form as a gradient vector:

vGradf[{x_,y_}]=D[f[{x,y}],{{x,y}}]

There is a built-in function for finding the gradient, but you need to know a bit more about how Mathematica handles different coordinate systems to use that, so we'll look at that in one of the labs instead.  

You can also use the D operator on on compositions:

D[f[vG[vPolar[{r,θ}]]],{{r,θ}}]//MatrixForm

You could use this to find the local linearization for the composition vG(vPolar(r,θ)) near a point, say r=1,θ=π/3:

deriv=D[vG[vPolar[{r,θ}]],{{r,θ}}]/.r->1/.θ->π/3;
point=vG[vPolar[{r,θ}]]/.r->1/.θ->π/3;
linear[{r_,θ_}]:=deriv.(({{r-1}, {θ-π/3}}))+point//Flatten;
linear[{r,θ}]

//Flatten here is used to convert from a column vector (which is the output you would expect) to a regular row vector.  In many situations, Mathematica can accept either one, but there are times where that could get confusing.  In fact, where possible, Mathematica treats them as interchangeable; you can add a row vector to a column vector, for example.)

One warning about graphing compositions of functions like this:  Because of the way Mathematica handles parameters inside plotting functions, you may have to enclose the composition inside an Evaluate command:

Plot3D[Evaluate[f[vG[vPolar[{r,θ}]]]],{r,0,6},{θ,0,4π},ClippingStyle->None]