Mathematica Graphics

by
K. D.  Stroyan
University of Iowa

copyright 2007 by Keith Stroyan - All rights reserved.

Review: Closed and Open Mathematica Cells

The NoteBook MmaNtBkCells introduces you to use of cells.

To open a closed cell, use your mouse to position the cursor over the dark arrowhead and click the button on the mouse twice rapidly.  (This is called "double clicking.")  When you are done looking at a section, you can position the cursor at the far right bracket again and double click to close that section.

Open the cell containing "Notebook Overview" now.  Then close this cell.

Notebook Overview: What's expected?

This NoteBook introduces you to graphic computations in Mathematica.

The only requirement of the NoteBook is for you to work through the Exercises below.  The remaining computations give you a tour of some of the graphics in Mathematica.

References to the Text

This NoteBook goes with Chapter 1 of the text.

Graphics in Mathematica

Mathematica also has the ability to draw various kinds of graphs.

2-D Plots

The Plot command below will draw a graph a function of  x.  The numbers in curly braces tell Mathematica the range of x-values to use for the plot.

To draw the plot, use the mouse to position the cursor inside the cell, click once to get a vertical insert bar and then press <Enter> on the number pad or hold down <Shift> and press <Return>.

Clear[x,p];
p[x_]:=(x^4)+3 (x^3)-16 (x^2)-9 x+9;
a=-6;
b=4;
Plot[p[x],{x,a,b}]
Print["p[x] = ",p[x]];

Ranges

Mathematica sets the  y  scale unless you specify otherwise, but the  x  range you choose can give a graph different appearances. <Enter> the following:

a=-0.1;
b=0.1;
Plot[p[x],{x,a,b}]

a=-1;
b=1;
Plot[p[x],{x,a,b}]

a=-10;
b=10;
Plot[p[x],{x,a,b}]

a=-100;
b=100;
Plot[p[x],{x,a,b}]

Scales

You may want to set the  y  scale yourself.  Here's how:

a=-10;
b=10;
Plot[p[x],{x,a,b},PlotRange->{0,10}]

Ugly, isn't it?  Compare this to the plot from -10 to 10 above.  The ugly one is right, if that's what you want.

AspectRatio

What does the graph of  dy = m dx look like when  m takes various different values?  The simple answer is: Lines of different slopes.  Try and see:

Clear[dx,dy,m];
dy:=m dx
m=1;
a=-4;
b=4;
Plot[dy,{dx,a,b}]
Print["A line of slope ",m];

Disgusting!  It doesn't even get  45 degrees.  This is because Mathematica chooses the 1/Golden ratio as the default "aspect ratio" or ratio of y to x axes.  Here's a fix for that:

Clear[dx,dy,m];
dy:=m dx
m=1;
a=-4;
b=4;
Plot[dy,{dx,a,b},AspectRatio->Automatic]
Print["A line of slope ",m];

Ok, we got slope 1 right.  Try this:

Clear[dx,dy,m];
dy:=m dx
m=2;
a=-4;
b=4;
Plot[dy,{dx,a,b},AspectRatio->1]
Print["A line of slope ",m];

Bummer.  What's wrong with this picture?  Try it "Automatic" in the next cell:

Clear[dx,dy,m];
dy:=m dx
m=2;
a=-4;
b=4;
Plot[dy,{dx,a,b},AspectRatio->Automatic]
Print["A line of slope ",m];

Now let's control AspectRatio and PlotRange:

Clear[dx,dy,m];
dy:=m dx
m=2;
a=-4;
b=4;
Plot[dy,{dx,a,b},AspectRatio->1,PlotRange->{a,b}]
Print["A line of slope ",m];

Practice Exercise: Now you try

Draw the graph of an new function  q[x_] := (x^2)+2 x+1   for  -3 < x < 4  by imitating a previous computation in  the next cell.  (You can even copy that cell and edit it.)

Information?

The command that follows gives you more information about the Plot command.  (This works with any Mathematica command, <??ValidCommand> as Input produces information as Output.)

??Plot

Information["Plot",LongForm->True]

3-D Plots

Mathematica  can also draw three-dimensional surface plots.  The Plot3D command below will draw a graph of a function of two variables.  <Enter> it and see.  We will return to 3-D plots later.

Plot3D[Cos[x] Sin[x+y],{x,-2,2},{y,-2,2}]
Plot3D[(E^(-((x^2)+((y^2)/3)))),{x,-2,2},{y,-2,2}]

List Plots

Mathematica  works well with Lists of numbers such as the data in the preview section of Chapter 1.  <Enter> the next cell and see a List computation. (The NoteBook MmaLists explains more about the use of lists.)

Clear[x,y,m,b];
xList={1,2,5,6.3};
Print["The list of x values =",xList];
y[x_]:=m x+b;
m=-2;
b=5;
yList=y[xList];
Print["The list of y values =",yList];
pairs=Transpose[{xList,yList}];
Print["The list of linear (x,y) pairs = ",pairs];

Once we have a list of pairs, it is easy to plot them:

ListPlot[pairs]

We can connect the dots:

ListPlot[pairs,Joined->True]

We can make fat dots:

ListPlot[pairs,Prolog->AbsolutePointSize[6]]

Information?

The command that follows gives you more information about the ListPlot command.

??ListPlot

Exercises

Graph a Line

The line through  (5,-4)  with slope -3 has local equation:

dy = -3  dx

Plot this line with Mathematica.  (Compare to the plots in the 2-D Plots section if needed.)  We have given you some help, but you must type correct commands where we have "??".

Clear[dx,dy];

dy := ??
		
Plot[ ??, {dx,??,???}];

The local coordinates are

dy = y + 4   and   dx = x - 5

so we can re-write the equation

dy = -3 dx
	
(y+4) = -3(x-5)
	
y = -4 - 3(x-5)

Plot the same line as you did above using the (x,y) coordiantes:

Clear[x,y,m,x0,y0];

y[x_] := y0 + m(x-x0);

x0 = ??;
y0 = ??;
m = ??;

Plot[ y[x] , {x,??, ??}];
Print["y[x] = ",y[x]];

Graph a Curve

Use Mathematica to plot the curve

y = (x^3)

for  -2 < x < 4.  (Compare with the samples above as needed.)

Clear[x,y,m,x0,y0];

y[x_] := ??;

x0 = ??;
y0 = ??;
m = ??;

Plot[ y[x] , {x,??, ??}];
Print["y[x] = ",y[x]];