computational methods in mathematicsmath.uni.lodz.pl/~maczar/cagir/diaz2014.pdfcomputational methods...

16
Computational methods in Mathematics Jos´ e Carlos D´ ıaz Ramos Cristina Vidal Casti˜ neira June 13, 2014 1 Graphics Mathematica represents all graphics in terms of a collection of graphics prim- itives. The primitives are objects like Point, Line and Polygon, that repre- sent elements of a graphical image, as well as directives such as RGBColor or Thickness. The appearance of these can be modified using options. Each com- plete piece of graphics in Mathematica is represented as a graphics object. There are several different kinds of graphics objects, corresponding to different types of graphics. Each kind of graphics object has a definite head which identifies its type. We will essentially use the following two: Graphics[list ] general two-dimensional graphics Graphics3D[list ] general three-dimensional graphics The most basic way of producing a graphic is writing a command like: graphic type[{{directives 1 , primitives 1 }, {directives 2 , primitives 2 }, ...}, options]. Here we have a list of the some basic primitives: Point[{x, y}] point at position {x, y} Line[{{x 1 ,y 1 }, {x 2 ,y 2 }, ···}] line through the points {x 1 ,y 1 }, {x 2 ,y 2 }, ··· Polygon[{{x 1 ,y 1 }, {x 2 ,y 2 }, ···}] filled polygon with the specified list of corners Circle[{x, y},r] circle with radius r centered at {x, y} Tube[{{x 1 ,y 1 ,z 1 },{x 2 ,y 2 ,z 2 },... }, r] represents a tube of radius r Sphere[{x, y, z}, r] represents a sphere of radius r centred at {x, y, z} Some of the basic directives are: RGBColor[r,g,b] color with specified red, green and blue components, each between 0 and 1 Hue[h] color with hue h between 0 and 1 PointSize[d] give all points a diameter d as a fraction of the width of the whole plot Thickness[w] give all lines a thickness w as a fraction of the width of the whole plot 1

Upload: others

Post on 27-Jul-2020

0 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Computational methods in Mathematicsmath.uni.lodz.pl/~maczar/cagir/Diaz2014.pdfComputational methods in Mathematics Jos e Carlos D az Ramos Cristina Vidal Castineira~ June 13, 2014

Computational methods in Mathematics

Jose Carlos Dıaz Ramos Cristina Vidal Castineira

June 13, 2014

1 Graphics

Mathematica represents all graphics in terms of a collection of graphics prim-itives. The primitives are objects like Point, Line and Polygon, that repre-sent elements of a graphical image, as well as directives such as RGBColor orThickness. The appearance of these can be modified using options. Each com-plete piece of graphics in Mathematica is represented as a graphics object. Thereare several different kinds of graphics objects, corresponding to different typesof graphics. Each kind of graphics object has a definite head which identifies itstype. We will essentially use the following two:

Graphics[list ] general two-dimensional graphicsGraphics3D[list ] general three-dimensional graphics

The most basic way of producing a graphic is writing a command like:graphic type[{{directives1, primitives1}, {directives2, primitives2}, ...}, options].

Here we have a list of the some basic primitives:

Point[{x, y}] point at position {x, y}Line[{{x1, y1}, {x2, y2}, · · · }] line through the points {x1, y1},

{x2, y2}, · · ·Polygon[{{x1, y1}, {x2, y2}, · · · }] filled polygon with the specified

list of cornersCircle[{x, y}, r] circle with radius r centered at

{x, y}Tube[{{x1, y1, z1},{x2, y2, z2},. . . }, r] represents a tube of radius r

Sphere[{x, y, z}, r] represents a sphere of radius rcentred at {x, y, z}

Some of the basic directives are:

RGBColor[r,g,b] color with specified red, green and blue components,each between 0 and 1

Hue[h] color with hue h between 0 and 1PointSize[d] give all points a diameter d as a fraction of the width of

the whole plotThickness[w] give all lines a thickness w as a fraction of the width of

the whole plot

1

Page 2: Computational methods in Mathematicsmath.uni.lodz.pl/~maczar/cagir/Diaz2014.pdfComputational methods in Mathematics Jos e Carlos D az Ramos Cristina Vidal Castineira~ June 13, 2014

In the latest versions of Mathematica one can use named colours (like Red,

Blue, Green, etc.), sizes (like Large, Small, Tiny), or thickness (like Thin orThick).

After you have produced a graphics object of some kind, the next thing youhave to do is to display it. This is accomplished by Show.

Show[g1, g2, . . . , opt1 → val1, opt2 → val2, . . . ] display several graphicsobjects combined withcertain options

Here are a few examples of two dimensional graphics.

In[1]:= pt = Table[Point[{Cos[ 2πn4 ], Sin[ 2πn4 ]}], {n, 0, 4}];lin = Line[Table[{Cos[ 2πn6 ], Sin[ 2πn6 ]}, {n, 0, 6}]];pol = Polygon[Table[{Cos[ 2πn3 ], Sin[ 2πn3 ]}, {n, 0, 3}]];cir = Circle[{0, 0}, 1];

This produces the corresponding graphics object and displays it.

In[2]:= Graphics[

{{PointSize[Large], Red, pt}, {Thick, Green, lin},{Blue, pol}, cir}]

Out[2]=

To produce 3-dimensional graphics, the procedure is analogous. One has toreplace Graphics by Graphics3D and use three coordinates instead of two.

You should keep in mind that all the graphics produced with Mathematicafollow this approach regardless of whether you see it or not. For example, thisshows you how a two dimensional plot is saved internally by Mathematica. Weskip the long output.

In[3]:= InputForm[Plot[Sin[x],{x, 0, 2π}]]

1.1 2D graphics

We start with commands for plotting two dimensional data. All the commandslisted here will produce graphic objects of type Graphics.

These are a few options that any of the functions described below will ad-mit (as well as Show, when you combine two dimensional graphics). We willdemonstrate them in what follows.

2

Page 3: Computational methods in Mathematicsmath.uni.lodz.pl/~maczar/cagir/Diaz2014.pdfComputational methods in Mathematics Jos e Carlos D az Ramos Cristina Vidal Castineira~ June 13, 2014

option name default value

AspectRatio 1/GoldenRatio the height-to-width ratio for the plot; Automaticsets it from the absolute x and y coordinates

Axes Automatic whether to include axesAxesLabel None labels to be put on the axes; ylabel specifies a

label for the y axis, {xlabel, ylabel } for bothaxes

AxesOrigin Automatic the point at which axes crossPlotLabel None an expression to be printed as a label for the plotPlotRange Automatic the range of coordinates to include in the plot;

All includes all points;Ticks Automatic which tick marks to draw if there are axes; None

gives no tick marks

1.2 Plot

This commands plots a function f : [a, b]→ R. The syntax is Plot[f, {x, a, b}],where f is an expression depending on x that evaluates to a real number forall x ∈ [a, b]. To plot several functions at a time in the same interval usePlot[{f1, f2, · · · }, {x, a, b}]. You can provide options that change the appear-ance of the graphic.

The following table gives a few options available for Plot which are notpresent for Graphics objects in general.

option name default value

PlotStyle Automatic a list of lists of graphics primitives to use for eachcurve (as explained in the previous section)

PlotPoints Automatic the minimum number of points at which to samplethe function

An example:

In[4]:= Plot[{Tan[x], Sin[x], Cos[3x]}, {x,0,2 Pi},PlotStyle → {Red, Green, Blue}]

Out[4]=

The function Plot, like other Mathematica functions such as Table, eval-uates the first of its arguments for each of the different nodes of the intervalwhere the function is to be plotted. If the first argument is an expression that

3

Page 4: Computational methods in Mathematicsmath.uni.lodz.pl/~maczar/cagir/Diaz2014.pdfComputational methods in Mathematics Jos e Carlos D az Ramos Cristina Vidal Castineira~ June 13, 2014

produces a list of graphics, it is important to force the evaluation of this argu-ment before plotting the function to avoid this evaluation at each iteration ofPlot. In this case we use the syntax Plot[Evaluate[f],{x, xmin, xmax}].

This makes a plot of the functions n cosx with n running from 1 to 6. TheEvaluate command tells Mathematica first to make the table of functions, andonly then to evaluate them for particular values of x.

In[5]:= Plot[Evaluate[Table[n Cos[x], {n, 6}]],{x, 0, 10}]

Out[5]=

1.3 3D graphics

In this section we deal with 3-dimensional graphics which are essentially objectsof type Graphics3D. Most of the comments given for 2-dimensional graphics gothrough now. We will present the basic plotting functions and forget about themore complicated low level commands.

option name default value

AspectRatio Automatic the height-to-width ratio for the plotAxes False whether to include axesAxesLabel None labels to be put on the axes; ylabel specifies

a label for the y axis, {xlabel, ylabel } forboth axes

Boxed True whether to include a box for the graphicLighting True whether to use ambient light to colour the

graphicPlotLabel None an expression to be printed as a label for the

plotPlotRange Automatic the range of coordinates to include in the

plot; All includes all points;SphericalRegion False whether the final image should be scaled

so that a sphere drawn around the three-dimensional bounding box would fit in thedisplay area specified

ViewPoint {1.3,-2.4,2.} point in space from which the objects plot-ted are to be viewed

This is an example of how to use graphics primitives and directives for 3-dimensional graphics.

4

Page 5: Computational methods in Mathematicsmath.uni.lodz.pl/~maczar/cagir/Diaz2014.pdfComputational methods in Mathematics Jos e Carlos D az Ramos Cristina Vidal Castineira~ June 13, 2014

In[6]:= Graphics3D[Green, Sphere[2,3,5], Red, Sphere[3,3,5,1/3],

Brown, Sphere[2,2,5.5,1/4], Sphere[2,1.75,5.5,1/4],

Sphere[2.3,1.75,5.5,1/4], Sphere[2,2,4.5,1/4],

Sphere[2,1.75,4.5,1/4], Sphere[2.3,1.75,4.5,1/4],

Blue, Sphere[3,3.5,5.5,1/4], Sphere[3,3.5,4.5,1/4],

Boxed → False]

Out[6]=

1.4 ParametricPlot3D

We use ParametricPlot3D for 3-dimensional parametric plots. Note that thesyntax ParametricPlot3D[{fx, fy, fz}, {t, tmin, tmax}] is the direct analog inthree dimensions of ParametricPlot[{fx, fy}, {t, tmin, tmax}] in two dimen-sions, which we have not discussed.

This makes a parametric plot of a helical curve.

In[7]:= ParametricPlot3D[{Cos[t],Sin[t],t/3}, {t,0,4π}]

Out[7]=

The function ParametricPlot3D can also be used to plot surfaces. It isyour duty to make sure that the parametric plot you produce does not haveself intersections or any strange behaviour. The syntax to plot a parametrisedsurface is ParametricPlot3D[{fx, fy, fz}, {u, umin, umax}, {v, vmin, vmax}].

In[8]:= ParametricPlot3D[{x, y, x3 - 3 x y2}, {x,-1,1}, {y,-1,1}]

5

Page 6: Computational methods in Mathematicsmath.uni.lodz.pl/~maczar/cagir/Diaz2014.pdfComputational methods in Mathematics Jos e Carlos D az Ramos Cristina Vidal Castineira~ June 13, 2014

Out[8]=

There are other possibilities to represent surfaces instead of ParametricPlot3Dsuch as Plot3D, ContourPlot3D and others.

1.5 Manipulate and DynamicModule

Sometimes it is useful to represent a graphic that depends on a parameterand dynamically manipulate this parameter. This is achieved in Mathematicausing the function Manipulate. Manipulate[expr,{u, umin, umax}] generatesa version of expr with controls added to allow interactive manipulation of thevalue of u.

The following input allows us to manipulate a plot of two functions thatdepends on a parameter called a.

In[9]:= Manipulate[Plot[{(5− a)Sin[x], a Cos[x]},{x, 0, 10}], {a, 0, 5}]

Out[9]=

If the plot we want to manipulate is obtained as a procedure, as we will seelater, then it is convenient to localize the variables used in this procedure. Fordynamic manipulation purposes this is achieved using DynamicModule instead ofModule. The syntax of these two functions is identical: DynamicModule[{x, y, · · · },expr].The symbols specified in a DynamicModule will by default have their valuesmaintained even across Mathematica sessions. One can also specify initial val-ues for x, y, . . . , as DynamicModule[{x = x0, y = y0, · · · },expr]

6

Page 7: Computational methods in Mathematicsmath.uni.lodz.pl/~maczar/cagir/Diaz2014.pdfComputational methods in Mathematics Jos e Carlos D az Ramos Cristina Vidal Castineira~ June 13, 2014

2 Curves

In this section, we start using Mathematica for understanding several conceptsof differential geometry. We start plotting curves of R3. Latter, we will alsoinvestigate some other geometric properties such as curvature.

We illustrate some of the basic geometric concepts associated with curves.We use some of the programming techniques described in previous sections.Many of the concepts defined here are well known and there is a vast bibliog-raphy to explore curves using software like Mathematica. We do not aim to bethorough: we just want to show a few examples. These will give us a clue ofhow to proceed in more general situations.

In[1]:= helix[t ] = {Cos[t], Sin[t], t/3};

In[2]:= ParametricPlot3D[helix[t], {t, 0, 4π}]

Out[2]=

The basic geometric objects associated with regular curves in R3 are curva-ture, torsion and the vectors of the Frenet-Serret frame: the tangent vector, thenormal vector and the binormal vector. We quickly remind the basic formulasand definitions.

Let α : t ∈ I → α(t) ∈ R3 be a regular curve, that is, α′(t) 6= 0 for all t. Such

a regular curve can be parametrised by arc length s(t) =∫ tt0||α′(t)||dt. With

respect to the arc length parameter (whose derivatives are denoted by a dot), wedefine the tangent vector as T (s) = α(s), the curvature as κ(s) = ||T (s)||, thenormal vector as N(s) = T (s)/κ(s), the binormal vector as B(s) = T (s)×N(s),and the torsion as τ(s) = 〈B(s), N(s)〉 whenever α(s) 6= 0.

It is clear that the arc length parameter always exists but it could be verydifficult (or even impossible) to calculate. Hence, for practical purposes, it issometimes better to work with the parameter t. Thus, one has to adapt theabove formulas by doing the right change of variable:

T (t) =α′(t)

‖α′(t)‖, N(t) =

T ′(t)

κ(t)‖α′(t)‖, B(t) = T (t)×N(t),

κ(t) =‖α′(t)× α′′(t)‖‖α′(t)‖3

, τ(t) =det(α′(t), α′′(t), α′′′(t))

‖α′(t)× α′′(t)‖2.

7

Page 8: Computational methods in Mathematicsmath.uni.lodz.pl/~maczar/cagir/Diaz2014.pdfComputational methods in Mathematics Jos e Carlos D az Ramos Cristina Vidal Castineira~ June 13, 2014

The following commands implement the above formulas.

In[3]:= frenetT[α ][t ] := α′[t]√α′[t]·α′[t]

;

In[4]:= frenetN[α ][t ] := frenetT[α]′[t]

curvature[α][t]√α′[t]·α′[t]

;

In[5]:= frenetB[α ][t ] := frenetT[α][t]× frenetN[α][t]

In[6]:= curvature[α ][t] :=

√(α′[t]×α′′[t])·(α′[t]×α′′[t])

(α′[t]·α′[t])3/2

In[7]:= torsion[α ][t ] := Det[{α′[t],α′′[t],α′′′[t]}](α′[t]×α′′[t])·(α′[t]×α′′[t])

As an example, we calculate these geometric objects for the helix above.Note the use of Simplify.

In[8]:= Simplify[{curvature[helix][t], torsion[helix][t]}]Out[8]= { 9

10 ,310}

In[9]:= Simplify[frenetT[helix][t]]

Out[9]= {− 3Sin[t]√10

, 3Cos[t]√10

, 1√10}

In[10]:= Simplify[frenetN[helix][t]]Out[10]= {-Cos[t], -Sin[t],0}

In[11]:= Simplify[frenetB[helix][t]]

Out[11]= { Sin[t]√10,− Cos[t]√

10, 3√

10}

Sometimes, a plot helps more than the exact formulas we get using thesymbolic capability of Mathematica. For example, we write here a function toplot the Frenet-Serret frame at a point of a curve. This uses the formulas forthe Frenet-Serret frames above. We use the commands Arrow and Tube to plotthe corresponding vectors of the Frenet-Serret frame. Once we have a vector,we wrap it using Graphics3D to produce the actual graphic. We plot normaland binormal vector modifying their lenght by multiplication by curvature andtorsion. Finally, we plot the osculating circle. Later, we will use Show to renderthe result on screen.

In[12]:= disk3D[center , r , t1 , t2 ]:=Module[{s, t},ParametricPlot3D[Evaluate[center + t Cos[s] t1 + t Sin[s] t2],{s, 0, 2π},{t, 0, r},Mesh→False, PerformanceGoal→"Quality"]]

In[13]:= plotOsculating[curve ][t ] := Module[

{c = curve[t],v1 = frenetT[curve][t],v2 = frenetN[curve][t],v3 = frenetB[curve][t],curv = curvature[curve][t]},Show[

8

Page 9: Computational methods in Mathematicsmath.uni.lodz.pl/~maczar/cagir/Diaz2014.pdfComputational methods in Mathematics Jos e Carlos D az Ramos Cristina Vidal Castineira~ June 13, 2014

Graphics3D[

{{Blue, Arrow[Tube[{c, c+v1}]], Arrow[Tube[{c, c+v2}]],Arrow[Tube[{c, c+ v3}]]},{Red, Arrow[Tube[{c, c+ curv v2},0.05]]},{Green, Arrow[Tube[{c, c+torsion[curve][t] v3}, 0.05]]}{Yellow, Line[{c, c+ 1/curv v2}]}}],

disk3D[c+ v2/curv, 1/curv, v1, v2]]];In[14]:= curveObjects[curve , {t0 , t1 }] :=

Module[{s}, DynamicModule[{t},Manipulate[

Show[

ParametricPlot3D[curve[s], {s, t0, t1}],plotOsculating[curve][t],Boxed→False, Axes→False,

PlotRangePadding→1, SphericalRegion→True],

{t, t0, t1}, ControlPlacement→Top]]];

Here is an example with the helix.

In[15]:= curveObjects[helix, {0, 4 π}]

Out[15]=

3 Surfaces

We will consider parametrised surfaces and will not be interested in singular-ities or self-intersections. It will be the task of the user to check whether theparametrisation produces an embedded surface or not (that is, one has to checkwhether there are self intersections, singular points and so on). We will illus-trate some of the geometric objects that can be constructed on a surface andgive the code to calculate them.

In order to try our code we start with a well-known example. Here it is thedefinition of a torus.

9

Page 10: Computational methods in Mathematicsmath.uni.lodz.pl/~maczar/cagir/Diaz2014.pdfComputational methods in Mathematics Jos e Carlos D az Ramos Cristina Vidal Castineira~ June 13, 2014

In[1]:= torus[R , r ][u , v ]=

{(R+ rCos[v])Cos[u], (R+ rCos[v])Sin[u], rSin[v]};

The first obvious thing we can do with a surface is to plot it.

In[2]:= ParametricPlot3D[torus[2, 1][u, v], {u, 0, 2π}, {v, 0, 2π}]

Out[2]=

The most basic object of a surface is the metric or first fundamental form. Aswe are given a surface by means of a parametrization, the reasonable thing to dois to calculate the metric with respect to the basis of tangent vectors. The codeis straightforward. The only interesting point here is the way the derivatives

are calculated: Derivate[i1, i2, · · · ][f][x1, x2, · · · ] represents ∂i1+i2+···f

∂xi11 ∂x

i22 ···

. For

example ∂3f∂x2∂y (x, y) is represented by Derivate[2, 1][f][x, y].

In[3]:= CoordinateVector[1][sup ][u , v ]:=Derivative[1,0][sup][u, v];CoordinateVector[2][sup ][u , v ]:=Derivative[0,1][sup][u, v];

In[4]:= Metric[sup ][u , v ]:=Module[

{dx = {CoordinateVector[1][sup][u, v],CoordinateVector[2][sup][u, v]}},

{{dx[[1]]·dx[[1]], dx[[1]]·dx[[2]]},{dx[[2]]·dx [[1]], dx[[2]]·dx[[2]]}}];

For example, this is the metric of the torus

In[5]:= MatrixForm[Simplify[Metric[torus[R, r]][u, v]]]Out[5]//MatrixForm= (

r2 00 (R+ rCos[u])2

)Most of the geometry of a surface in R3 is encoded in the Gauss map. This

is defined by x3 = (x1 × x2)/||x1 × x2|| where xi = ∂x/∂ui and x(u1, u2) is theparametrization.

In[6]:= GaussMap[surface ][u , v ]:=

Module[{x3 = Cross[CoordinateVector[1][surface][u, v],

10

Page 11: Computational methods in Mathematicsmath.uni.lodz.pl/~maczar/cagir/Diaz2014.pdfComputational methods in Mathematics Jos e Carlos D az Ramos Cristina Vidal Castineira~ June 13, 2014

CoordinateVector[2][surface][u, v]]},x3/Sqrt[x3 · x3]];

This calculates the Gauss map of the torus. In this example we use thefunction PowerExpand to cancel square roots and squares. Mathematica assumesthat variables are complex numbers, so this simplification cannot be performedby default.

In[7]:= PowerExpand[Simplify[GaussMap[torus[R, r]][u, v]]]Out[7]= {-Cos[u] Cos[v], -Cos[u] Sin[v], -Sin[u]}

The differential of the Gauss map contains very important geometric infor-mation of a surface. It essentially encodes the second fundamental form. Itstrace is the mean curvature and its determinant the Gaussian curvature.

In[8]:= GaussianCurvature[surface ][u , v ] := Module[

{x11 = Derivative[2, 0][surface][u, v],x12 = Derivative[1, 1][surface][u, v],x22 = Derivative[0, 2][surface][u, v],x3 = GaussMap[surface][u, v]},Det[{{x11·x3, x12·x3}, {x12·x3, x22·x3}}]/Det[Metric[surface][u, v]]];

In[9]:= MeanCurvature[surface ][u , v ] := Module[

{x11 = Derivative[2, 0][surface][u, v],x12 = Derivative[1, 1][surface][u, v],x22 = Derivative[0, 2][surface][u, v],x3 = GaussMap[surface][u, v],g = Metric[surface][u, v]},(x11·x3 g[[2, 2]]−2x12·x3 g[[1, 2]]+x22·x3 g[[1, 1]])/Det[g]/2];

For example, these are the Gaussian and mean curvature of the torus.

In[10]:= Simplify[GaussianCurvature[torus[R, r]][u, v]]

Out[10]=Cos[u]

rR+r2Cos[u]

In[11]:= PowerExpand[Simplify[MeanCurvature[torus[R, r]][u, v]]]

Out[11]=R+2rCos[u]

2r(R+rCos[u])

The Christoffel symbols are used later for the geodesic equation. Here, theyare programmed in a more sophisticated way.

In[12]:= ChristoffelSymbols[surface ][u , v ] :=Module[{g, dg},g = Evaluate[Metric[surface][#1,#2]] &;

dg = {Derivative[1, 0][g][u, v],Derivative[0,1][g][u, v]};((Transpose[dg, {2, 1, 3}]+dg−Transpose[dg, {3, 2, 1}])·Inverse[g[u, v]])/2];

In[13]:= Simplify[ChristoffelSymbols[torus[R, r]][u, v]]

11

Page 12: Computational methods in Mathematicsmath.uni.lodz.pl/~maczar/cagir/Diaz2014.pdfComputational methods in Mathematics Jos e Carlos D az Ramos Cristina Vidal Castineira~ June 13, 2014

Out[13]= {{{0,0}, {0,− rSin[u]R+rCos[u]}}, {{0,− rSin[u]

R+rCos[u]}, {(R+rCos[u])Sin[u]

r ,0}}}

The way the geodesic equation is written here is even more complicated. Thepurpose of this code is to introduce you to functional programming. It is veryeasy to modify this code so that it works for objects of arbitrary dimensions.Check the Mathematica help for further details.

In[14]:= GeodesicEquation[surface ][u ][t ]:= Module[{i, j, k},Table[{u}[[k]]’’[t]+Sum[(ChristoffelSymbols[surface]@@(#[t]&/@{u}))[[i, j, k]]{u}[[i]]’[t] {u}[[j]]’[t], {i, 2}, {j, 2}] == 0,

{k, 2}]];

In[15]:= Simplify[GeodesicEquation[torus[R, r]][u, v][t]]

Out[15]= { (R+rCos[u[t]])Sin[u[t]]v′[t]2+ru′′[t]r = 0,

−2rSin[u[t]]u′[t]v′[t]+(R+rCos[u[t]])v′[t]R+rCos[u[t]] = 0}

3.1 Ploting surfaces

The following code generates a random surface that is the graph of a functiondefined on the rectangle [−1, 1]× [−1, 1].

In[16]:= surface[x , y ]= Module[

{f=ListInterpolation[Array[RandomReal[{0, 1}]&,{6, 6}],{{−1.2, 1.2}, {−1.2, 1.2}}]},

{x, y, f [x, y]}];In[17]:= ParametricPlot3D[surface[x, y], {x,−1, 1}, {y,−1, 1}]

Out[17]=

The following function takes a surface and its domain and plots it with itsnormal vector and tangent plane. It basically uses CoordinateVector to drawthe tangent plane and GaussMap to calculate the normal vector. These valuesare saved so that they do not have to be calculated everytime the point is moved.The dynamic interactivity is performed with the function Manipulate.

In[18]:= PlotTangentNormal[surface , {u0 , u1 }, {v0 , v1 }]:=Module[{u, v,g =ParametricPlot3D[surface[u, v], {u, u0, u1},{v, v0, v1},

12

Page 13: Computational methods in Mathematicsmath.uni.lodz.pl/~maczar/cagir/Diaz2014.pdfComputational methods in Mathematics Jos e Carlos D az Ramos Cristina Vidal Castineira~ June 13, 2014

SphericalRegion→True, Axes→False],

x1 =Evaluate[CoordinateVector[1][surface][#1,#2]]&,x2 =Evaluate[CoordinateVector[2][surface][#1,#2]]&,gm =Evaluate[GaussMap[surface][#1,#2]] &},Manipulate[

Show[g, Graphics3D[

{Arrow[Tube[{surface@@p, surface@@p+ x1@@p}]],Arrow[Tube[{surface@@p, surface@@p+ x2@@p}]],Red, Arrow[Tube[{N[surface@@p], N[surface@@p+gm@@p]}]],EdgeForm[], Green, Polygon[{N[surface@@p+ (x1@@p) + (x2@@p)],N[surface@@p+ (x1@@p)− (x2@@p)],N[surface@@p− (x1@@p)− (x2@@p)],N[surface@@p− (x1@@p) + (x2@@p)],N[surface@@p+ (x1@@p) + (x2@@p)]}]}],

PlotRangePadding→1],

{{p, {u0, (v0 + v1)/2}, "Point"},{u0, v0}, {u1, v1}},ControlPlacement→{Left}]];

In[19]:= PlotTangentNormal[surface, {−1, 1}, {−1, 1}]

Out[19]=

In[20]:= PlotTangentNormal[torus[2, 1], {0, 2π}, {0, 2π}]

13

Page 14: Computational methods in Mathematicsmath.uni.lodz.pl/~maczar/cagir/Diaz2014.pdfComputational methods in Mathematics Jos e Carlos D az Ramos Cristina Vidal Castineira~ June 13, 2014

Out[20]=

The following function plots a surface painting it according to curvature. Ittakes the surface and the domain as parameters. Then it calculates the Gaus-sian curvature and saves the result in a variable. It uses a "TemparatureMap"

function to colour: hot colours correspond to positive curvature and cold coloursto negative curvature. The curvature is rescaled so that it fits in the interval[0,1]. Several other options are given. The function uses the command Quiet

to prevent error messages from appearing when there are singular points.

In[21]:= PlotGaussianCurvature[surface , {u0 , u1 }, {v0 , v1 }]:=Module[

{sc =Evaluate[GaussianCurvature[surface][#1,#2]]&,u, v},Quiet[ParametricPlot3D[surface[u, v], {u, u0, u1}, {v, v0, v1},ColorFunction→(Glow[ColorData["TemperatureMap"]

[0.5 + ArcTan[2.0sc[#4,#5]]/π]]]&),ColorFunctionScaling→False, SphericalRegion→True,

PerformanceGoal→"Quality",Axes→False]]];

In[22]:= PlotGaussianCurvature[surface, {−1, 1}, {−1, 1}]

Out[22]=

In[23]:= PlotGaussianCurvature[torus[2, 1], {0, 2π}, {0, 2π}]

14

Page 15: Computational methods in Mathematicsmath.uni.lodz.pl/~maczar/cagir/Diaz2014.pdfComputational methods in Mathematics Jos e Carlos D az Ramos Cristina Vidal Castineira~ June 13, 2014

Out[23]=

3.2 Plotting geodesics

Geodesics are very important objects of surfaces, but they are very difficult(usually impossible) to calculate in general. Here we show a piece of code thatsolves the geodesic equation numerically and plots a geodesic on a surface.

In[24]:= PlotGeodesic[sup , {u0 , u1 }, {v0 , v1 }, opacity : 1]:=Module[

{u, v, x, sol, gEq,max = 1.566,g = Evaluate[Metric[sup][#1,#2]]&,

graphSup =ParametricPlot3D[sup[u, v], {u, u0, u1}, {v, v0, v1},SphericalRegion→True, Axes→False, Mesh→False,

PerformanceGoal→"Quality", PlotStyle→Opacity[opacity]]},gEq =GeodesicEquation[sup][u[1], u[2]][v];x =Evaluate[

{{1, 0},{−g[#1,#2][[1, 2]], g[#1,#2][[1, 1]]}/Sqrt[g[#1,#2][[1, 1]]g[#1,#2][[2, 2]]− g[#1,#2][[1, 2]]2]}/Sqrt[g[#1,#2][[1, 1]]]]&;

Manipulate[Quiet[

sol =({u[1], u[2]}/.NDSolve[Join[gEq, Thread[{u[1][0], u[2][0]} == pt],Thread[{u[1]′[0], u[2]′[0]} == (x@@pt).{Cos[dir], Sin[dir]}]],

{u[1], u[2]}, {v, 0.0, Tan[r]}][[1]]);Show[

graphSup, Graphics3D[{Sphere[N[sup@@pt], 0.05]}],ParametricPlot3D[

Evaluate[sup@@(#[u]&/@sol)],{u, 0, Tan[r]}, PlotStyle→Thick,

PlotPoints→500],

PlotRangePadding→0.1]],

{{pt, {(u0+u1)/2, (v0+v1)/2}, "Point" }, {u0, v0}, {u1, v1}},{{dir, 0, "Direction"}, 0, 2π},{{r, Pi /4, "Length"}, 0.01, max},Style[Dynamic["Real length: " <> ToString[N[Tan[r]]]]],

15

Page 16: Computational methods in Mathematicsmath.uni.lodz.pl/~maczar/cagir/Diaz2014.pdfComputational methods in Mathematics Jos e Carlos D az Ramos Cristina Vidal Castineira~ June 13, 2014

ControlPlacement→Left, TrackedSymbols:>{pt, dir, r}]];

In[25]:= PlotGeodesic[surface, {−1, 1}, {−1, 1}]

Out[25]=

In[26]:= PlotGeodesic[torus[2, 1], {0, 2π}, {0, 2π}, 0.5]

Out[26]=

16