F. Fassò.    University of Padova. 
Introduction to the use of Mathematica in Mathematics and Science. SoftSkills course for the PhD program in Mathematics
Lecture 3 (October 20, 2023)

A first look at Mathematica: Graphics (1)

0. Graphics

A few clear basic ideas on the graphics in Mathematica are advisable (if not even necessary, because otherwise one may have the impression that things are obscure)

There are different types of graphics:
  - 2D graphics  ->    Head  “Graphics”
  - 3D graphics  ->    Head  “Graphics3D”

These are different object:
- they are created and modified with different commands
- they cannot be mixed
- etc
Nevertheless, the two sets of commands share some common  structure

1. Plots of functions

There are several commands to create plots of functions, each one in 2D and 3D versions.
Which one to use?
You should think of the matematical object you want to create

1.a. Graphs of functions   (Reals) -> (Reals)  ,    (((Reals))^2)-> (Reals)  ,   ℕ -> (Reals)

Plot[ f(x), {x,x1,x2}]     ->   graph is a curve

Plot[ { f(x) , g(x) } , { x, x1, x2}]     ->   plot both curves

Plot3D[ f(x,y), {x,x1,x2}, {y,y1,y2} ]     ->   graph is a surface

ListPlot[ list ]     ->   graph is a collection of points

Exercise: plot the graphs of:
  1.   ((x^2)/(1+(x^2)))  for -2 < x < 2
  2.   Sin[(x^2)+(y^2)] in the square  (-π,π) × (-π, π) 
  3.   Sin[(n/π)]  for  n = -10,-9, ...., 9 , 10      (need to create the list with Table)

Plot[((x^2)/(1+(x^2))) ,{x,-2,2}]

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

ListPlot[          Table[Sin[(n/Pi)] ,{n,-10,10,.1}]       ]

1.b. Image (‘support’) of
 \[Application] curves  in  (((Reals))^2) or in  (((Reals))^3) 
 \[Application] parametrized surfaces  (((Reals))^2) -> (((Reals))^3)

Curve in  (((Reals))^2) :  
   γ:  (Reals) -> (((Reals))^2) 
   γ(t) = (x(t), y(t) )

ParametricPlot[   { x(t) , y(t) } ,    {t, t1 , t2}

Curve in  (((Reals))^3) :  
   γ:  (Reals) -> (((Reals))^3) 
   γ(t) = (x(t), y(t),z(t) )

ParametricPlot3D[ { x(t) , y(t) , z(t)},  {t, t1, t2} ]

(Parametrized) surface in  (((Reals))^3) :  
   s:  (((Reals))^2) -> (((Reals))^3) 
   s(u,v) = (x(u,v) ,  y(u,v) , z(u,v) )

ParametricPlot3D[ { x(u,v),y(u,v),z(u,v)},   {u,u1,u2} , {v,v1,v2} ]

Exercise

Exercises:

1. Plot the arc of the Archimedean spiral
   t -> (t cos(t), t sin(t) ) ,     0 < t < 5π
 
 2. Plot the arc of the 3d spiral
   t -> (t cos(t),  t sin(t) ,  t ) ,     0 < t < 5π

 3. Plot a sphere as a parametrized surface  (via spherical coordinates)
 
 4. Plot a torus (donught) as a parametrized surface

1.

ParametricPlot[ {t Cos[t],t Sin[t]}, {t,0,5π}]

2.

ParametricPlot3D[ {t Cos[t],t Sin[t],t}, {t,0,5π}]

3

Parametrization of the sphere (without the poles)
                 x(θ,ϕ) = sin(θ) cos(ϕ), 
                 y(θ,ϕ) = sin(θ) sin(ϕ), 
                 z(θ,ϕ) = cos(θ)  
with     0 < θ < π ,     0 < ϕ < 2π

ParametricPlot3D[{Sin[θ]Cos[ϕ],  Sin[θ]Sin[ϕ], Cos[θ]},     {θ,0,Pi},{ϕ,0,2Pi} ]

Don’t we like the mesh?

ParametricPlot3D[{Sin[θ]Cos[ϕ],  Sin[θ]Sin[ϕ], Cos[θ]},     {θ,0,Pi},{ϕ,0,2Pi} ,Mesh->None]

4.

Choose the two radii of the torus as 3 and 1

Parametrization:  
                 x(t,u) = (3 + cos(u)) cos(t), 
                 y(t,u) = (3 + cos(u)) sin(t),
                 z(t,u) = sin(u)  ,
with    0 < t < 2π ,     0 < u < 2π

ParametricPlot3D[{(3+Cos[u])Cos[t],    (3+Cos[u])Sin[t],   Sin[u]},   {u,0,2Pi} , {t,0,2Pi}   ]

1.c. Level sets of functions
     \[Application]  (R^2) -> R    (curves)
     \[Application]  (R^3) -> R    (surfaces)

\[Application]  ContourPlot
\[Application]  ContourPlot3D 
plot one or more level sets

ContourPlot[  f(x,y),   {x, x1, x2},   {y, y1, y2} ]       ->    plots a (significant) number of level curves

ContourPlot[ f(x,y) == 1 ,  {x, x1, x2},  {y, y1, y2} ]     ->    plots a single, chosen level curve

ContourPlot[ f(x,y) == {1,2} ,  {x, x1, x2},  {y, y1, y2} ]     ->    plots the two specified level curves

ContourPlot3D[ f(x,y,z),  {x,x1,x2}, {y,y1,y2} , {z,z1,z2} ]       ->    plots a (significant) number of level surfaces

ContourPlot3D[ f(x,y,z) == 1,  {x,x1,x2}, {y,y1,y2} , {z,z1,z2} ]       ->    plots a single level surface

ContourPlot3D[ f(x,y,z) == {1,2,3} ,  {x,x1,x2}, {y,y1,y2} , {z,z1,z2} ]       ->    plots the three specified level surface

Exercise

Exercises:
1. plot a few  level curves of   (x^2)-(y^2) 
2. plot the ellipsoid of equation  (x^2)+4(y^2)+ 9(z^2) = 1

1.

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

2.

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

2. Structure of graphics: Primitives, Options, Directives

gr=Plot[1,{x,0,1}]

What is (for Mathematica) this graphical object?
Let’s look its InputForm

gr//InputForm

Graphics are lists which consist of:
• Graphics Primitives  (Point, Line, Circle, Text (inside graphics), ....)
• Graphics Directives  (Hue, Thickness, RGBColor, .....)
• Graphics Options     (AspectRatio, Axes, ......, PlotRange, ....)

Graphics Primitives

Can be used to draw objects such as a circles and lines

Graphics[{ 
Circle[{0,0},1] , 
Line[{{0,0},{1,1}} ]}
]

but (in the everyday use of a mathematician) are usually built and used by other plotting commands (e.g., Plot etc)

Graphics Directives

Graphics Directives: specify properties of the individual graphics primitives which form the graphics (color, lines’ thickenesse, points’ size ...)

Example:

Graphics[{ 
{Red,Circle[{0,0},1] }, 
{Thickness[.05],RGBColor[0,1,0],Line[{{0,0},{1,1}} ]}}
]

Graphics Options

Graphics Options : specify  global properties of the graphics (dimensions, is there a frame?,  background color ...)

Example:

Graphics[{ 
Circle[{0,0},1] , 
Line[{{0,0},{1,1}} ]},
  Frame->True
]

Structure of graphics

Structure of a list representing a graphics object:
                    Graphics[   { List of graphics directives and primitives } ,  option1->...., option2->...  } ]
                   
                    Graphics3D[   { List of graphics directives and primitives } ,  option1->...., option2->...  } ]

2.a Options

The options create and specify additional elements of the graphics (axes, etc).
Are like routines which create some graphics primitives and inserte them in the graphics.

Options can be specified in two ways

1) When the graphics is created.
E.g.:
      Plot[ function,   interval,    option1 -> value1, option2 -> value2, ...]

Example:

- plot the graph of the two functions  x-> x  and  x-> x (e^(-x))   (e.g., in -2 ≤ x ≤ 2)

Then modify it as follows:

- Fix the vertical interval as (-1,1)  (Option “PlotRange -> {-1,1} ”)  
- Use equal scaling factors on the two axes (Option “AspectRatio -> Automatic”)
- Fix the dimension of the image (Option “ImageSize”; possible values    400, 500, .... , Small, Medium, ... )
- Remove the ticks from the axes (Option “Ticks->None”)
- Remove the axes (ption “Axes -> False”)

Plot[{x,x (E^(-x))},{x,-2,2}]

Plot[{x,x (E^(-x))},{x,-2,2}, PlotRange->{-1,1}]

Plot[{x,x (E^(-x))},{x,-2,2}, PlotRange->{-1,1},AspectRatio->Automatic]

Plot[{x,x (E^(-x))},{x,-2,2}, PlotRange->{-1,1},AspectRatio->Automatic,ImageSize->Small]

Plot[{x,x (E^(-x))},{x,-2,2}, PlotRange->{-1,1},AspectRatio->Automatic,Ticks->None]

Plot[{x,x (E^(-x))},{x,-2,2}, PlotRange->{-1,1},AspectRatio->Automatic,Axes->False]

2) They can be modified re-desplyaing the graphics with
         Show[...., Option->newvalue ]

gr=Plot[{x,x (E^(-x))},{x,-2,2},Background->Green]

Show[ gr, Background->Green]

2.b Directives

Graphics directives specify properties of the invidual graphics primitives
(thickness of lines, colour of points, lines and surfaces, size of  points, etc)

How to specify the graphics directives?
•  If you build the graphics “assembling” the graphics primitives:
         	{ directive1 , primitive1, directive2, primitive 2, ....  }
•  Within commands such as Plot, Plot3D, ... using certain options: e.g.
	PlotStyle -> lista di direttive

Note:
Each directive applies to all primitives that follow it in the (sub)list

Example: plot a lone horizontal green line:

a) Using graphics primitives and directives:
            Graphics[  {directive, primitive}]
    primitive:   Line[{{ 0,0  },{3,0}}]
    directive:    Hue[.4]  or  Green  or RGB[0,1,0]

b) Plotting the function    x -> 1
             Plot[ function, interval, options]
     options:     PlotStyle->Green
                          Asex->False

Graphics[
                 { Hue[.4], Line[{{ 0,0  },{3,0}}]  } 
                   ]

Graphics[
                 { Hue[.4],Thickness[.1],  Line[{{ 0,0  },{3,0}}]  } 
                   ]

Plot[0,{x,0,3},   PlotStyle->Hue[.4]   ]

Plot[0,{x,0,3},   PlotStyle->{Thickness[.1],Hue[.4]  }, Axes -> False ]

Exercise : plot together the graphs of sine and  cosine in different colors and thicknesses

Plot[  {Sin[x],Cos[x]},     {x,0,2Pi}  ]

Plot[
{Sin[x],Cos[x]},{x,0,2Pi}  ,
         PlotStyle->{Thickness[.015]  ,  {Red,Thickness[.035]}  }  
 ]

Plot[
{Sin[x],Cos[x]},{x,0,2Pi}  ,  
  PlotStyle->{ {} ,  {Thickness[.05],Green} }]

3.  Tables of graphics (GraphicsRow, GraphicsGrid,....)

To display different graphics in a 1-d or 2-d assembly, can simply use Table[ ]

gr=Table[    Plot[x^n,{x,-1,1}], {n,0,5}]

but formatting is poor.
Better using      GraphicsRow , GraphicsColumn, GraphicsGrid

Take[gr,-3]

GraphicsGrid[  { {gr[[1]],gr[[2]], gr[[3]] }, Take[gr,-3]} ,Spacings->{0,20},ImageSize->Full]

GraphicsRow[   {gr[[1]],gr[[2]]}    ]

Take[gr,2]//GraphicsRow

GraphicsGrid[{{gr[[1]],gr[[2]]},
                            {gr[[2]], gr[[3]],gr[[4]]}}]

Exercise:
1. Write a function
            gtorus(R,r)
that plots a torus (donught) of
          equatorial radius R 
          meridian radius r
2. Assemble a number of them, with the same R and various r between 0 and R (or even >R,why not?) in a grid.

E.g., use R=3 , r = 0,...,5 and assemble them in two rows

Might be useful to define two functions:
     torus(R,r,u,t)  :    the parametrization   {  (R+r cos(u) cos(t) ,   (R+r cos(u) sin(t) , r sin(t)  }
      gtorus(R,r)  :   plots the torus, calling torus[R,r]

torus[R_,r_,u_,t_]:=   {(R+r Cos[u])Cos[t],    (R+r Cos[u])Sin[t], r  Sin[u]}

gtorus[R_,r_]:= ParametricPlot3D[ torus[R,r,u,t] ,    {u,0,2Pi} , {t,0,2Pi}  , PlotStyle->Opacity[.3], Mesh->None ,Axes->False,Boxed->False, PlotPoints->30]

gtorus[3,5]

GraphicsGrid[ {  { gtorus[3,0], gtorus[3,1], gtorus[3,2]},
                               { gtorus[3,3], gtorus[3,4], gtorus[3,5]}}  ]

Too small?
Option  ImageSize   ->  Large, Full, 800, 1000,....

(Need not recreating the graphics)

GraphicsGrid[ {  { gtorus[3,0], gtorus[3,1], gtorus[3,2]},
                               { gtorus[3,3], gtorus[3,4], gtorus[3,5]}}  ,
                                    ImageSize->Full]

This can also be done without recreating the graphics

Show[%%, ImageSize->Full]

Don’t like the mesh? The axes? The frame (in 3D, “box”)?
Remove them with Options

gtorus3[R_,r_]:=ParametricPlot3D[   torus[R,r,u,t],  {u,0,2Pi},{t,0,2Pi},
                                     PlotStyle->{Opacity[.5]},
                                     Mesh->False, Axes->False , Boxed ->False]

gtorus3[3,5]

Exercise: Could we have changed all these three options with Show? Why?

The image is not smooth enough?
Option PlotPoints -> n
(try not too large values of n first)

Or make new function
     gtorus(R,r,n)
that includes option PlotPoints -> n

gtorus[R_,r_,n_]:=
ParametricPlot3D[   torus[R,r,u,t],  {u,0,2Pi},{t,0,2Pi},
                                       PlotStyle->{Opacity[.5]},
                                       Mesh->False, Axes->False , Boxed ->False,
                                       PlotPoints-> n]

GraphicsRow[  {gtorus[3,5,30] , gtorus[3,5,50] , gtorus[3,5,100] } ]

Exercise: Automatize the creation of the list
         {  { gtorus[3,0], gtorus[3,1], gtorus[3,2]  },
             { gtorus[3,3], gtorus[3,4], gtorus[3,5]  }  }

As usual, can be done in different ways.
E.g.
- A list of two table (each with one iterator)
- A single table (with two iterators)

GraphicsGrid[  {   Table[   gtorus3[3,r], {r,0,2}],
                                 Table[   gtorus3[3,r], {r,3,5}]   }    ]

GraphicsGrid[    Table[    gtorus3[ 3, r+s ], {s,0,3,3},{r,0,2} ]  ]

Note:
The most external cycle (here, r=0,1,2) is executed first.
In the second cycle, s takes the values 0,3

4.  Animation etc (Manipulate, Animate, ListAnimate)

Manipulate[ expression which contains parameters ,  {parameters} ]
creates an interface that 
- shows the expression, an
- gives the possibility of interactively modify it changing the valus of the parameters (sliders)

Manipulate[  (((1+x))^r)//Expand , {r,0,5,1}]

Manipulate[    (((1+x))^r)//Expand ,   {r,0,5}]

??
The syntax for the parameters in Manipulate look similar to that of iterators in  Table, but there is an important difference:
unless otherwise stated, parameters are incremented contnuously (as real numbers, not as integers)

Manipulate[    (((1+x))^r)//Expand ,   {r,0,5,1}]

gtorus3=gtorus

Manipulate[    gtorus3[3,r] ,   {r,0,5}]

Can change more parameters

Manipulate[    gtorus3[R,r] ,   {r,0,5,1},  {R,0,5,1}]

It is possible to “animate” the expression
- inside the Manipulate, through the slider’s menu
- with the command 
         Animate[  expression which contains parameters ,  {parameters} ]
   which directly shows the animation (in a Manipulate window)

Animate[    gtorus3[3,r] ,   {r,0,5}]

It is possible to animate a list of existing images:
       ListAnimate[ list ]

Example
(First try with few photograms!)

listimm=  Table[  gtorus3[3,r] ,   {r,0,5,1}]  ;

ListAnimate[     listimm ]

listimm2=    Table[  gtorus3[3,r] ,   {r,0,5,.1}]  ;

ListAnimate[     listimm2]

Careful: 
Animations and manipulations cam be very resource intensive.

Moreover, Mathematica updates them automatically if the values of symbols that appear in them change (even outside the Manipulation).

It may be wise to stop or even cancel them if not in use.

Can we create a movie and export it? 
First, how to set directory where to write the file etc ?

5. Interacting with the operating system

Current “working” directory used by Mathematica
(it reads and writes from it)

Directory[ ]

SetDirectory[  NotebookDirectory[]]

Notebook directory (where the current notebook is)

NotebookDirectory[]

NotebookDirectory[    ]

Change the curent directory

SetDirectory["D:/"]

Directory[ ]

Change the current directory to the notebook directory

SetDirectory[ NotebookDirectory[]  ]

Directory[ ]

List of files in the current directory :

FileNames[]

Dimension of a file  
       FileSize[“nomefile”]

FileSize[  "SoftSkills Pd 2023-3.nb"]

Etc.
You may read and write files, start programs, ...