topic 10 introduction to mathematica€¦ · topic 10 introduction to mathematica based on material...

29
CITS2401 Computer Analysis & Visualisation SCHOOL OF COMPUTER SCIENCE AND SOFTWARE ENGINEERING FACULTY OF ENGINEERING, COMPUTING AND MATHEMATICS Topic 10 Introduction to Mathematica Based on material from MATH2200 by Paul Abbott

Upload: others

Post on 05-Jul-2020

11 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Topic 10 Introduction to Mathematica€¦ · Topic 10 Introduction to Mathematica Based on material from MATH2200 by Paul Abbott . The University of Western Australia Mathematica

CITS2401 Computer Analysis & Visualisation

SCHOOL OF COMPUTER SCIENCE AND SOFTWARE ENGINEERING

FACULTY OF ENGINEERING, COMPUTING AND MATHEMATICS

Topic 10 Introduction to Mathematica

Based on material from MATH2200 by Paul Abbott

Page 2: Topic 10 Introduction to Mathematica€¦ · Topic 10 Introduction to Mathematica Based on material from MATH2200 by Paul Abbott . The University of Western Australia Mathematica

The University of Western Australia

Mathematica

◊  In this lecture we will examine the Mathematica notebook environment, and explore basic commands.

◊  We will learn how to solve basic equations with Mathematica, how Mathematica represents data, and how to do basic visualisations with Mathematica.

◊  We will compare the functionality of Matlab to the functionality of Mathematica.

Page 3: Topic 10 Introduction to Mathematica€¦ · Topic 10 Introduction to Mathematica Based on material from MATH2200 by Paul Abbott . The University of Western Australia Mathematica

The University of Western Australia

Videos

◊  http://www.youtube.com/user/WolframResearch?v=L1aWy629vgY&lr=1

•  Stephen Wolfram explains the vision of Mathematica

◊  http://www.youtube.com/user/wolframmathematica

•  Demonstrations of Mathematica for solving problems and visualising data

◊  http://www.wolfram.com/broadcast/screencasts/first-ten-minutes/

•  Essential features of Mathematica

Page 4: Topic 10 Introduction to Mathematica€¦ · Topic 10 Introduction to Mathematica Based on material from MATH2200 by Paul Abbott . The University of Western Australia Mathematica

The University of Western Australia

The Structure of Mathematica

◊  As a computer application, Mathematica has two major components: the front end and the kernel.

◊  The frontend is what loads up when you start mathematica. It allows you to load and manipulate notebooks and acts as interpreter between you and the kernel. The first expression you evaluate will start the math kernel.

◊  The kernel is where (nearly) all of your commands are processed. It is possible to use more than one kernel at a time, which can be located either locally or remotely.

Page 5: Topic 10 Introduction to Mathematica€¦ · Topic 10 Introduction to Mathematica Based on material from MATH2200 by Paul Abbott . The University of Western Australia Mathematica

The University of Western Australia

The welcome screen:

Page 6: Topic 10 Introduction to Mathematica€¦ · Topic 10 Introduction to Mathematica Based on material from MATH2200 by Paul Abbott . The University of Western Australia Mathematica

The University of Western Australia

Notebooks ◊  The user primarily works within a notebook. A notebook

consists of a series of cells which are of the type, eg "Section", "Text", "Input", "Output", etc

◊  The user primarily works within a notebook. A notebook consists of a series of cells which are of the type, eg "Section", "Text", "Input", "Output", etc

◊  If you move the mouse between two cells, the cursor will become horizontal. Clicking here and then typing will yield a new Input cell. Once entered (by pressing shift-return or enter) each input cell is assigned a number, and will return a corresponding output cell.

◊  % refers to the last output, %% the second last, and %i refers to the ith output.

Page 7: Topic 10 Introduction to Mathematica€¦ · Topic 10 Introduction to Mathematica Based on material from MATH2200 by Paul Abbott . The University of Western Australia Mathematica

The University of Western Australia

The Kernal ◊  Everything that Mathematica deals with is an Expression (the

first principle of Mathematica). An expression is either Atomic or normal.

◊  Atomic expressions are elementary objects, such as a Number (2, 5.55, 1+2I), or a Symbol such as x.

◊  A normal expression consists of a Head and a list of arguments, eg f[1,2,g[y,z],x]

◊  When an input cell is entered it's translated into FullForm and passed to the kernel for evaluation.

◊  FullForm and TreeForm functions display Mathematica formulas from the Kernal’s point of view.

Page 8: Topic 10 Introduction to Mathematica€¦ · Topic 10 Introduction to Mathematica Based on material from MATH2200 by Paul Abbott . The University of Western Australia Mathematica

The University of Western Australia

Palettes Palettes contain many of the basic functions you may need to use, and are a good way to learn the Mathematica syntax. Use FullForm functions to learn the Kernal representation of each operation.

Page 9: Topic 10 Introduction to Mathematica€¦ · Topic 10 Introduction to Mathematica Based on material from MATH2200 by Paul Abbott . The University of Western Australia Mathematica

The University of Western Australia

Mathematica Brackets and Conventions ◊  Functions in Mathematica should always start with a capital letter,

and variables should always start with a lower case letter:

•  Sin, Pi

◊  We use single square brackets to enclose the arguments of functions:

•  Sin[x], Exp[Pi]

◊  We use curly brackets to enclose elements in a list:

•  v = {1, 4, 9, 16, 25}

◊  We use double square brackets for indexing arrays:

•  v[[4]] = 16

◊  Round brackets are used to indicate precedence.

Page 10: Topic 10 Introduction to Mathematica€¦ · Topic 10 Introduction to Mathematica Based on material from MATH2200 by Paul Abbott . The University of Western Australia Mathematica

The University of Western Australia

Numerical Data ◊  Mathematica is built on the principal of symbolic computation

which means it can do perfect precision arithmetic.

•  Try the functions Pi, 2^100000, Pi^3000…

◊  Mathematica stores data symbolically to avoid any round off error.

•  Try 100/31,

◊  Use //N or N[…] to force the Kernal to return a numeric value (to specified precision), rather than a symbolic value.

◊  Complex numbers can be specified using I, or the Complex function:

•  4+2I, Complex[4,2]

‡1

5Exp@1êx + Sin@xDD ‚x

Page 11: Topic 10 Introduction to Mathematica€¦ · Topic 10 Introduction to Mathematica Based on material from MATH2200 by Paul Abbott . The University of Western Australia Mathematica

The University of Western Australia

Arithmetic Operators

◊  In Mathematica, everything is an expression, and there is an expression for juts about every arithmetic operation:

•  a+b, a-b, a*b (also a b), a/b, a^b, a!

◊  More complex functions are written in function form:

•  GCD[a,b], Log[a], Sqrt[a]

◊  These operators are overloaded so that they will behave differently depending on whether the arguments are rational numbers, floating point numbers or symbols.

Page 12: Topic 10 Introduction to Mathematica€¦ · Topic 10 Introduction to Mathematica Based on material from MATH2200 by Paul Abbott . The University of Western Australia Mathematica

The University of Western Australia

Other Data Types

◊  Floating point numbers (eg 2.71) are referred to as inexact numbers. We may set the precision as high as we like.

◊  Rational numbers (Rational[1,5]) are exact numbers.

◊  Strings are specified with double quotes:

•  “This is a String”

◊  Characters are just Strings of length 1

◊  Boolean values (True, False) are special types of symbols. The operations !! (not), || (0r) and && (and) can be applied to form complex Boolean expressions.

Page 13: Topic 10 Introduction to Mathematica€¦ · Topic 10 Introduction to Mathematica Based on material from MATH2200 by Paul Abbott . The University of Western Australia Mathematica

The University of Western Australia

Lists, Vectors and Matrices.

◊  The List is the key data structure in Mathematica, just as the array is the key data structure in Matlab.

◊  Lists are specified as follows:

•  ls = {1,2,3}

•  ls = List[1,2,3]

◊  These structures are very similar to arrays in Matlab (at least in the way they are used).

◊  Lists need not contain the same types:

•  ls = {“abc”, 2.9, Pi}

Page 14: Topic 10 Introduction to Mathematica€¦ · Topic 10 Introduction to Mathematica Based on material from MATH2200 by Paul Abbott . The University of Western Australia Mathematica

The University of Western Australia

Lists of numbers ◊  We can apply arithmetic operators to lists:

•  {1,2,3}^2 = {1,4,9}

•  {1,2,4}+5 = {6,7,9}

◊  We do point-wise arithmetic on lists as well:

•  {1,2,3}{2,3,4} = {2,6,12}

•  {1,2,3}^{2,3,4} = {1,8,81}

•  %/{1,2,3} = {1,4,27}

◊  We can apply functions to all elements of a list

•  Exp{1,2,3} = …?

◊  These operations also work for lists of lists.

Page 15: Topic 10 Introduction to Mathematica€¦ · Topic 10 Introduction to Mathematica Based on material from MATH2200 by Paul Abbott . The University of Western Australia Mathematica

The University of Western Australia

More Lists

◊  Lists can also work as arguments:

•  v = 4^{1,2,3} = {4,16,64}

◊  You can access elements inside a list :

•  v[[1]] = 4, v[[2]] = 16

•  v[[-1]] = 64, v[[-2]] = 16

•  v[[0]] = List, v[[4]] gives an error.

◊  You can also access lists of elements:

•  v[[{1,3}]] = {4,64}

Page 16: Topic 10 Introduction to Mathematica€¦ · Topic 10 Introduction to Mathematica Based on material from MATH2200 by Paul Abbott . The University of Western Australia Mathematica

The University of Western Australia

Vectors and Matrices ◊  Vectors are simply lists, and Matrices are lists of lists.

◊  There are specific operations that have been implemented for performing linear algebra:

•  a.b is used to calculate dot products, or perform matrix multiplication

•  Cross[a,b] will calculate the cross product of two vectors

•  Norm[v] will calculate the Euclidean norm of a vector

•  Inverse[m] will invert the matrix

•  Transpose[m] will transpose the matrix

•  MatrixPower[m,n] will calculate the nth power of m.

•  MatrixForm will display a matrix in conventional notation

Page 17: Topic 10 Introduction to Mathematica€¦ · Topic 10 Introduction to Mathematica Based on material from MATH2200 by Paul Abbott . The University of Western Australia Mathematica

The University of Western Australia

Creating Lists and Matrices

◊  The following functions can be used to create a new list:

•  Table[f, {i, n}] – Build a length n vector by evaluating f with values i from 1 to n.

•  Range[n1, n2, dn] – Build the list {n1, n1+dn,…,n2}

•  IdentityMatrix[n] – Build the nxn identity matrix

•  DiagonalMatrix[ls] – Build a square matrix with the elements of ls along the main diagonal.

◊  Length[ls] will return the length of a list

◊  Dimensions[ls] will return the Dimensions of a matrix.

Page 18: Topic 10 Introduction to Mathematica€¦ · Topic 10 Introduction to Mathematica Based on material from MATH2200 by Paul Abbott . The University of Western Australia Mathematica

The University of Western Australia

List Operations

◊  Part ([[]]) – Extract a part of an Expression

•  {{a,b},{c,d}}[[2,1]] = c

◊  Map (/@) – Applies a function to each element of a list

•  f/@{a,b,c,d} = {f[a], f[b], f[c], f[d]}

◊  Apply (@@) – Applies a function to an entire list

•  f@@{a,b,c,d} = f [a,b,c,d]

◊  ReplaceAll (/.) – Applies a substitution to a list

•  {1, 2, 3, 4}/.{2->”two”, 1->”I”} = {“I”, “two”, 3, 4}

Page 19: Topic 10 Introduction to Mathematica€¦ · Topic 10 Introduction to Mathematica Based on material from MATH2200 by Paul Abbott . The University of Western Australia Mathematica

The University of Western Australia

Strings and Characters

◊  Strings are special types of lists. They have special operations such as:

•  StringLength, StringReverse

•  StringSplit is a tokenizer that returns a list of Strings

–  StringSplit[“a:bc:d”, “:”] = {“a”, “bc”, “d”}

•  ToUpperCase, ToLowerCase,

•  DigitQ, LetterQ test whether a String is all Digits

•  Print will display a series of objects:

–  Print[“age:”,18,”.”]

Page 20: Topic 10 Introduction to Mathematica€¦ · Topic 10 Introduction to Mathematica Based on material from MATH2200 by Paul Abbott . The University of Western Australia Mathematica

The University of Western Australia

Polynomials

◊  Mathematica will interpret all unrecoginzed symbols as variables. This makes it easy to define expressions:

•  x^2-3x-x+4 will display x^2-4x+4. Expressions will be reduced (but not always to their simplest form).

◊  There are many algebraic functions: Expand, Factor, Simplify, Collect that allow you to manipulate polynomial expressions

◊  Given a polynomial p, Solve[p==0] will return all roots of the polynomial

Page 21: Topic 10 Introduction to Mathematica€¦ · Topic 10 Introduction to Mathematica Based on material from MATH2200 by Paul Abbott . The University of Western Australia Mathematica

The University of Western Australia

Solving Equations

◊  The Solve function can solve equalities for several equations:

•  Solve[{x^2+y^2==1, y==x+1/2}, {x, y}]

–  It produces a list of all substitutions for x and y that make the equalities true.

•  FindRoot is similar, but it will search for numerical solutions to the systems of equalities.

Page 22: Topic 10 Introduction to Mathematica€¦ · Topic 10 Introduction to Mathematica Based on material from MATH2200 by Paul Abbott . The University of Western Australia Mathematica

The University of Western Australia

Trigonometry

◊  Mathematica will also simplify trigonometric expressions to a degree:

•  x^2+y^2/.{x->r Cos[theta],y -> r Sin[theta]}

•  r^2Sin^2[theta] + r^2 Cos^2[theta]

•  TrigReduce[%] = r^2

◊  TrigExpand will expand trigonometric expressions

•  Try Cos[x+y+z]//TrigExpand

Page 23: Topic 10 Introduction to Mathematica€¦ · Topic 10 Introduction to Mathematica Based on material from MATH2200 by Paul Abbott . The University of Western Australia Mathematica

The University of Western Australia

Calculus

◊  Partial Derivatives can be calculated using D:

•  D[x^2,x] = 2x

◊  We can also use the ‘ (prime operator) to find the derivative of pure functions:

•  Sin’[x] = Cos[x]

◊  We can do symbolic integration using the Integrate function:

•  Integrate[x^n,x] = x^(n+1)/(n+1)

◊  Try Integrate[x^x, {x, 0, 1}]

◊  You can use // N to get the numeric solution.

Page 24: Topic 10 Introduction to Mathematica€¦ · Topic 10 Introduction to Mathematica Based on material from MATH2200 by Paul Abbott . The University of Western Australia Mathematica

The University of Western Australia

2D-Plotting

Graphics and Sound

Basic Plotting

Plot@ f,8x,xmin,xmax<D plot f as a function of x from xmin to xmax

Plot@ f,8x,x0,x1,…,xk<D plot f over a series of segments, potentially breaking the

curve at each of the xi

Plot@8 f1, f2,…<,8x,xmin,xmax<D

plot several functions together

Basic plotting functions.

This plots a graph of sinHxL as a function of x from 0 to 2 p.

In[1]:= Plot@Sin@xD, 8x, 0, 2 Pi<D

Out[1]=1 2 3 4 5 6

-1.0

-0.5

0.5

1.0

You can plot functions that have singularities. Mathematica will try to choose appropriate scales.

In[2]:= Plot@Tan@xD, 8x, -3, 3<D

Out[2]=-3 -2 -1 1 2 3

-6

-4

-2

2

4

6

The singularities can be omitted from the plot by specifying them in the plot's range.

In[3]:= Plot@Tan@xD, 8x, -3, -Pi ê 2, Pi ê 2, 3<D

Out[3]=-3 -2 -1 1 2 3

-6

-4

-2

2

4

6

Graphics and Sound

Basic Plotting

Plot@ f,8x,xmin,xmax<D plot f as a function of x from xmin to xmax

Plot@ f,8x,x0,x1,…,xk<D plot f over a series of segments, potentially breaking the

curve at each of the xi

Plot@8 f1, f2,…<,8x,xmin,xmax<D

plot several functions together

Basic plotting functions.

This plots a graph of sinHxL as a function of x from 0 to 2 p.

In[1]:= Plot@Sin@xD, 8x, 0, 2 Pi<D

Out[1]=1 2 3 4 5 6

-1.0

-0.5

0.5

1.0

You can plot functions that have singularities. Mathematica will try to choose appropriate scales.

In[2]:= Plot@Tan@xD, 8x, -3, 3<D

Out[2]=-3 -2 -1 1 2 3

-6

-4

-2

2

4

6

The singularities can be omitted from the plot by specifying them in the plot's range.

In[3]:= Plot@Tan@xD, 8x, -3, -Pi ê 2, Pi ê 2, 3<D

Out[3]=-3 -2 -1 1 2 3

-6

-4

-2

2

4

6

Graphics and Sound

Basic Plotting

Plot@ f,8x,xmin,xmax<D plot f as a function of x from xmin to xmax

Plot@ f,8x,x0,x1,…,xk<D plot f over a series of segments, potentially breaking the

curve at each of the xi

Plot@8 f1, f2,…<,8x,xmin,xmax<D

plot several functions together

Basic plotting functions.

This plots a graph of sinHxL as a function of x from 0 to 2 p.

In[1]:= Plot@Sin@xD, 8x, 0, 2 Pi<D

Out[1]=1 2 3 4 5 6

-1.0

-0.5

0.5

1.0

You can plot functions that have singularities. Mathematica will try to choose appropriate scales.

In[2]:= Plot@Tan@xD, 8x, -3, 3<D

Out[2]=-3 -2 -1 1 2 3

-6

-4

-2

2

4

6

The singularities can be omitted from the plot by specifying them in the plot's range.

In[3]:= Plot@Tan@xD, 8x, -3, -Pi ê 2, Pi ê 2, 3<D

Out[3]=-3 -2 -1 1 2 3

-6

-4

-2

2

4

6

Graphics and Sound

Basic Plotting

Plot@ f,8x,xmin,xmax<D plot f as a function of x from xmin to xmax

Plot@ f,8x,x0,x1,…,xk<D plot f over a series of segments, potentially breaking the

curve at each of the xi

Plot@8 f1, f2,…<,8x,xmin,xmax<D

plot several functions together

Basic plotting functions.

This plots a graph of sinHxL as a function of x from 0 to 2 p.

In[1]:= Plot@Sin@xD, 8x, 0, 2 Pi<D

Out[1]=1 2 3 4 5 6

-1.0

-0.5

0.5

1.0

You can plot functions that have singularities. Mathematica will try to choose appropriate scales.

In[2]:= Plot@Tan@xD, 8x, -3, 3<D

Out[2]=-3 -2 -1 1 2 3

-6

-4

-2

2

4

6

The singularities can be omitted from the plot by specifying them in the plot's range.

In[3]:= Plot@Tan@xD, 8x, -3, -Pi ê 2, Pi ê 2, 3<D

Out[3]=-3 -2 -1 1 2 3

-6

-4

-2

2

4

6

Page 25: Topic 10 Introduction to Mathematica€¦ · Topic 10 Introduction to Mathematica Based on material from MATH2200 by Paul Abbott . The University of Western Australia Mathematica

The University of Western Australia

2D Plotting ◊  There are many options to explore with 2d plotting

Filling None filling to insert under each curve

FillingStyle Automatic style to use for filling

PlotPoints 50 the initial number of points at which to

sample the function

MaxRecursion Automatic the maximum number of recursive subdivi -sions allowed

More options for Plot. These cannot be used in Show.

This uses PlotStyle to specify a dashed curve.

In[8]:= Plot@Sin@x^2D, 8x, 0, 3<, PlotStyle Ø DashedD

Out[8]=0.5 1.0 1.5 2.0 2.5 3.0

-1.0

-0.5

0.5

1.0

When plotting multiple functions, PlotStyle settings in a list are used sequentially for each

function.

In[9]:= Plot@8Sin@x^2D, Cos@x^2D<, 8x, 0, 3<, PlotStyle Ø 8Red, Blue<D

Out[9]=0.5 1.0 1.5 2.0 2.5 3.0

-1.0

-0.5

0.5

1.0

When a PlotStyle contains a sublist, the settings are combined.

In[10]:= Plot@8Sin@x^2D, Cos@x^2D<, 8x, 0, 3<, PlotStyle Ø 8Red, 8Blue, Thick<<D

Out[10]=0.5 1.0 1.5 2.0 2.5 3.0

-1.0

-0.5

0.5

1.0

Visualization and Graphics 7

By default nothing is indicated when the PlotRange is set, so that it cuts off curves.

In[11]:= Plot@8Sin@x^2D, Cos@x^2D<, 8x, 0, 3<, PlotRange Ø 0.9D

Out[11]=0.5 1.0 1.5 2.0 2.5 3.0

-0.5

0.5

Setting ClippingStyle to Automatic draws a dashed line where a curve is cut off.

In[12]:= Plot@8Sin@x^2D, Cos@x^2D<, 8x, 0, 3<, PlotRange Ø 0.9, ClippingStyle Ø AutomaticD

Out[12]=0.5 1.0 1.5 2.0 2.5 3.0

-0.5

0.5

Setting ClippingStyle to a list defines the style for the parts cut off at the bottom and top.

In[13]:= Plot@8Sin@x^2D, Cos@x^2D<, 8x, 0, 3<,PlotRange Ø 0.9, ClippingStyle Ø 8Green, Red<D

Out[13]=0.5 1.0 1.5 2.0 2.5 3.0

-0.5

0.5

This specifies filling between the curve and the x axis.

In[14]:= Plot@Sin@x^2D, 8x, 0, 3<, Filling Ø AxisD

Out[14]=

8 Visualization and Graphics

Parametric Plots

"Basic Plotting" described how to plot curves in Mathematica in which you give the y coordinate

of each point as a function of the x coordinate. You can also use Mathematica to make paramet-

ric plots. In a parametric plot, you give both the x and y coordinates of each point as a function

of a third parameter, say t.

ParametricPlotA9 fx, fy=,9t,tmin,tmax=E

make a parametric plot

ParametricPlotA99 fx, fy=,9gx,gy=,…=,9t,tmin,tmax=E

plot several parametric curves together

Functions for generating parametric plots.

Here is the curve made by taking the x coordinate of each point to be Sin@tD and the y coordi-

nate to be Sin@2 tD.

In[1]:= ParametricPlot@8Sin@tD, Sin@2 tD<, 8t, 0, 2 Pi<D

Out[1]=-1.0 -0.5 0.5 1.0

-1.0

-0.5

0.5

1.0

ParametricPlot3DA9 fx, fy, fz=,9t,tmin,tmax=E

make a parametric plot of a three-dimensional curve

ParametricPlot3DA9 fx, fy, fz=,9t,tmin,tmax=,9u,umin,umax=E

make a parametric plot of a three-dimensional surface

ParametricPlot3DA99 fx, fy, fz=,9gx,gy,gz=,…=,…E

plot several objects together

Three-dimensional parametric plots.

Visualization and Graphics 25

This plots the values.

In[2]:= ListPlot@tD

Out[2]=

2 4 6 8 10

20

40

60

80

100

This joins the points with lines.

In[3]:= ListLinePlot@tD

Out[3]=

2 4 6 8 10

20

40

60

80

100

When plotting multiple datasets, Mathematica chooses a different color for each dataset

automatically.

In[4]:= ListPlot@8t, 2 t<D

Out[4]=

2 4 6 8 10

50

100

150

200

This gives a list of x, y pairs.

In[5]:= Table@8i^2, 4 i^2 + i^3<, 8i, 10<D

Out[5]= 881, 5<, 84, 24<, 89, 63<, 816, 128<, 825, 225<,836, 360<, 849, 539<, 864, 768<, 881, 1053<, 8100, 1400<<

Visualization and Graphics 23

Page 26: Topic 10 Introduction to Mathematica€¦ · Topic 10 Introduction to Mathematica Based on material from MATH2200 by Paul Abbott . The University of Western Australia Mathematica

The University of Western Australia

Interactive Plots

◊  Manipulate allows you to adjust parameters interactively

•  Manipulate[Plot[Sin[a x]*x, {x, -Pi, Pi}], {a, 0, 100}]

◊  Manipulate isn’t just restricted to plotting either:

•  Manipulate[FactorInteger[Floor[a]], {a, 1, 100000}]

◊  We can also listen to functions:

•  Play[Sin[2 Pi 440 t], {t,0,1}]

•  Play[Sin[300 t Sin[20 t]], {t,-1,1}]

Page 27: Topic 10 Introduction to Mathematica€¦ · Topic 10 Introduction to Mathematica Based on material from MATH2200 by Paul Abbott . The University of Western Australia Mathematica

The University of Western Australia

3D-Plotting ◊  As with Matlab there are a large range of three dimensional

plotting options:

While it is often convenient to use a variable to represent a graphic as in the above examples,

the graphic itself can be evaluated directly. The typical ways to do this in the notebook interface

are to copy and paste the graphic or to simply begin typing in the graphical output cell, at

which point the output cell will be converted into a new input cell.

When a plot created with no explicit ImageSize is placed into an input cell, it will automatically

shrink to more easily accommodate input.

The following input cell was created by copying and pasting the graphical output created in the

previous example.

In[7]:= AbsoluteOptionsB

5 10 15 20

0.5

1.0

1.5

, PlotRangeF

Out[7]= 9PlotRange Ø 994.08163µ10-7, 20.=, 94.08163µ10-7, 1.85194===

Three-Dimensional Surface Plots

Plot3D@ f,8x,xmin,xmax<,8y,ymin,ymax<D

make a three-dimensional plot of f as a function of the

variables x and y

Basic 3D plotting function.

This makes a three-dimensional plot of the function sinHxyL.

In[1]:= Plot3D@Sin@x yD, 8x, 0, 3<, 8y, 0, 3<D

Out[1]=

16 Visualization and Graphics

ParametricPlot3D@8 fx, fy, fz<, 8t, tmin, tmax<D is the direct analog in three dimensions of

ParametricPlotA9 fx, fy=, 8t, tmin, tmax<E in two dimensions. In both cases, Mathematica effec-

tively generates a sequence of points by varying the parameter t, then forms a curve by joining

these points. With ParametricPlot, the curve is in two dimensions; with ParametricPlot3D, it

is in three dimensions.

This makes a parametric plot of a helical curve. Varying t produces circular motion in the x-y

plane, and linear motion in the z direction.

In[2]:= ParametricPlot3D@8Sin@tD, Cos@tD, t ê 3<, 8t, 0, 15<D

Out[2]=

�1.0 �0.5 0.0 0.5 1.0

�1.0�0.5

0.00.51.0

0

2

4

ParametricPlot3D@8 fx, fy, fz<, 8t, tmin, tmax<, 8u, umin, umax<D creates a surface, rather than a

curve. The surface is formed from a collection of quadrilaterals. The corners of the quadrilater-

als have coordinates corresponding to the values of the fi when t and u take on values in a

regular grid.

Here the x and y coordinates for the quadrilaterals are given simply by t and u. The result is a

surface plot of the kind that can be produced by Plot3D.

In[3]:= ParametricPlot3D@8u Sin@tD, u Cos@tD, u<, 8t, 0, 2 Pi<, 8u, -1, 1<D

Out[3]=

26 Visualization and Graphics

This produces a cylinder. Varying the t parameter yields a circle in the x-y plane, while varying

u moves the circles in the z direction.

In[6]:= ParametricPlot3D@8Sin@tD, Cos@tD, u<, 8t, 0, 2 Pi<, 8u, 0, 2<D

Out[6]=

This produces a torus. Varying u yields a circle, while varying t rotates the circle around the z

axis to form the torus.

In[7]:= ParametricPlot3D@8Cos@tD H3 + Cos@uDL, Sin@tD H3 + Cos@uDL, Sin@uD<, 8t, 0, 2 Pi<, 8u, 0, 2 Pi<D

Out[7]=

This produces a sphere.

In[8]:= ParametricPlot3D@8Cos@tD Cos@uD, Sin@tD Cos@uD, Sin@uD<,8t, 0, 2 Pi<, 8u, -Pi ê 2, Pi ê 2<D

Out[8]=

You should realize that when you draw surfaces with ParametricPlot3D, the exact choice of

parametrization is often crucial. You should be careful, for example, to avoid parametrizations

28 Visualization and Graphics

Page 28: Topic 10 Introduction to Mathematica€¦ · Topic 10 Introduction to Mathematica Based on material from MATH2200 by Paul Abbott . The University of Western Australia Mathematica

The University of Western Australia

Functions ◊  It is important to be able to define functions within

Mathematica. This can be done in several ways:

•  f [x_] := Sin[ x^2]/ x

◊  The underscore is called a Blank, and will match whatever value is pass to f as a parameter.

◊  It uses := because the function should only be evaluated when the function is called:

•  Compare R[x_] = Random[] with R[x_] := Random[]

◊  There are other (cryptic) ways to define functions:

•  F = #1^#2 &

Page 29: Topic 10 Introduction to Mathematica€¦ · Topic 10 Introduction to Mathematica Based on material from MATH2200 by Paul Abbott . The University of Western Australia Mathematica

The University of Western Australia

Matlab vs Mathematica

◊  Matlab and Mathematica a similar systems both design for similar purposes. There are some important differences, though:

◊  In Mathematica, Everything is an Expression. This makes it very good at symbolic computation.

◊  In Matlab (almost) Everything is a matrix. This makes it very good at numeric computation

◊  They both mimic each others functionality, but each has its clear area of strength.