chapter 6: plotting in maple 6.1. 2d plots - qmul...

18
O O O O O O Chapter 6: Plotting in Maple Hugo Touchette School of Mathematical Sciences Queen Mary, University of London Last updated: 1 November 2010. Based on Dr Richard Nelson's notes. We will now examine how to plot functions in Maple in two and three space dimensions. 6.1. 2D plots We have already considered two-dimensional plotting of univariate functions in Maple. We will now revise the basics of two dimensional plotting before examining Maple's more advanced plotting capabilities. The simplest way of plotting a function f(x) in Maple is to use the command plot(f(x),x); Here the first argument to the "plot" command is the function name, and the second argument is the independent variable. An example of plotting the sine function as a function of x is: restart: plot(sin(x),x=-2*Pi..2*Pi); x K 6 K 4 K 2 0 2 4 6 K 1 K 0.5 0.5 1 As discussed before, we can plot multiple functions on a single graph, and specify a number of different arguments that will provide a plot title, a legend describing the different functions plotted, different line colour for each function plotted, etc. To find out about these different options please refer to Maple's on-line help system by typing ?plot. An example of a multiple plot with a variety of the options specified is: plot([sin(x),cos(x),tan(x)],x=-2*Pi..2*Pi,y=-2..2,legend= ["sin","cos","tan"],colour=[green,red,blue],linestyle=[1,2,3], title="Graph of Trig Functions",discont=true);

Upload: vuongtu

Post on 02-Jul-2018

218 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Chapter 6: Plotting in Maple 6.1. 2D plots - QMUL Mathswhitty/MTH4105/Weeks/Week6/Lectures/MTH4105...Chapter 6: Plotting in Maple ... The primary advantage of parametric plots is that

O O

O O

O O

Chapter 6: Plotting in Maple

Hugo TouchetteSchool of Mathematical Sciences

Queen Mary, University of London

Last updated: 1 November 2010.Based on Dr Richard Nelson's notes.We will now examine how to plot functions in Maple in two and three space dimensions.

6.1. 2D plotsWe have already considered two-dimensional plotting of univariate functions in Maple. We will now revise the basics of two dimensional plotting before examining Maple's more advanced plotting capabilities.

The simplest way of plotting a function f(x) in Maple is to use the command

plot(f(x),x);

Here the first argument to the "plot" command is the function name, and the second argument is the independent variable. An example of plotting the sine function as a function of x is:

restart:plot(sin(x),x=-2*Pi..2*Pi);

xK6 K4 K2 0 2 4 6

K1

K0.5

0.5

1

As discussed before, we can plot multiple functions on a single graph, and specify a number of different arguments that will provide a plot title, a legend describing the different functions plotted, different line colour for each function plotted, etc. To find out about these different options please referto Maple's on-line help system by typing ?plot. An example of a multiple plot with a variety of the options specified is:

plot([sin(x),cos(x),tan(x)],x=-2*Pi..2*Pi,y=-2..2,legend=["sin","cos","tan"],colour=[green,red,blue],linestyle=[1,2,3],title="Graph of Trig Functions",discont=true);

Page 2: Chapter 6: Plotting in Maple 6.1. 2D plots - QMUL Mathswhitty/MTH4105/Weeks/Week6/Lectures/MTH4105...Chapter 6: Plotting in Maple ... The primary advantage of parametric plots is that

O O

xK6 K4 K2 0 2 4 6

y

K2

K1

1

2Graph of Trig Functions

In the above plot command, we specify each function to be plotted within a list, we tell Maple that a title is to be used, that a legend should be included to identify each graph, that different line colours and styles should be used, and we specify the x and y ranges for the axes. We also tell Maple that it should plot discontinuous functions - in this case the tangent - without joining the discontinuous end points with lines.

6.2. Parametric plotsA plot can be defined parametrically, which means that both the horizontal and vertical coordinates of each point of the plot are specified as functions of a parameter. A parameter is a variable that is not identical to either the vertical or horizontal coordinate, but varies monotonically along the curve. The Maple syntax for a parametric plot is

plot([x(t), y(t),t=a..b],options);

In this expression, t represents the parameter, while x(t) and y(t) represent functions of the parameter that define the horizontal and vertical coordinates of a general point on the curve to be plotted. The firstargument of "plot" is the whole parametric representation of the curve contained in a list.

The primary advantage of parametric plots is that they can be used to plot curves that are not graphs of(single valued) functions. This is the case for any curve that "folds-over" on itself (such as a circle or an ellipse). Mathematically, such a curve can be represented by an equation of the form f x, y = 0 that does not have a unique solution for y in terms of x.

Consider the example of plotting a circle of unit radius parametrically, using polar coordinates to define the x and y cartesian coordinates parametrically:

plot([cos(t),sin(t),t=0..2*Pi],scaling=CONSTRAINED);

Page 3: Chapter 6: Plotting in Maple 6.1. 2D plots - QMUL Mathswhitty/MTH4105/Weeks/Week6/Lectures/MTH4105...Chapter 6: Plotting in Maple ... The primary advantage of parametric plots is that

O O

O O

K1 K0.5 0 0.5 1

K1

K0.5

0.5

1

In the above plot command, the variable t represents the polar angle going from 0 to 2p. We use the option "scaling=CONSTRAINNED" option to ensure that the axes are drawn with a 1:1 aspect ratio (by default Maple scales plot axes independently).Example:The equation representing this circle in Cartesian coordinates is x2 C y2 = 1, and has the following two solutions for y as a function of x:

sols1:=solve(x^2+y^2=1,y);

sols1 := Kx2 C 1 , K Kx2 C 1

plot([sols1],x=-1..1,scaling=CONSTRAINED);

xK1 K0.5 0 0.5 1

K1

K0.5

0.5

1

By default Maple plots the upper and lower semi-circles in different colours, confirming that they are different graphs.Example:An ellipse is a generalisation of a circle, obtained by stretching or squashing it in one direction. The longest and shortest distances from the centre to the perimeter are called the semi-major and semi-minor axes (denoted by a and b), respectively. The equation for an ellipse in cartesian coordinates is

xa

2

Cyb

2

= 1

An ellipse can be plotted using its natural parametrisation as follows :

Page 4: Chapter 6: Plotting in Maple 6.1. 2D plots - QMUL Mathswhitty/MTH4105/Weeks/Week6/Lectures/MTH4105...Chapter 6: Plotting in Maple ... The primary advantage of parametric plots is that

O O

O O O O

O O

a:=2:b:=1: # semi-major and semi-minor axesplot([a*cos(t),b*sin(t),t=0..2*Pi],scaling=CONSTRAINED);

K2 K1 1 2

K1

K0.5

0.5

1

6.3. 3D plotsMaple can plot both surfaces and curves in 3D, although a plot of a 3D structure generated on a computer screen is in reality a projection into two dimensions. The appearance of the plotted structure depends on the selected viewing angle.

Three dimensional graphics arise naturally from functions of two variables. For example, the graph of a function z = f x, y is a surface, where the value of the function f at the point (x, y) gives the height of the surface above the x-y plane located at z = 0. Three dimensional graphics also arise from problems in three dimensional geometry, such as plotting spheres, spheroids etc.

A curve that exists within three dimensions is called a space curve, and can only be plotted parametrically (using two parameters rather than the single parameter used in 2D parametric plotting).

Three dimensional plotting in Maple is a basic extension of 2D plotting, but be aware that there are some highly undesirable syntactical inconsistencies between 2D plotting commands and their 3D counterparts. These will be highlighted whenever possible. Note that it is not possible to plot 2D graphics and 3D graphics on the same plot.

Basic 3D plots are obtained using the "plot3d" command, which is the 3D equivalent of the "plot" command already encountered for 2D plotting. Be aware that 3D plots require more time and memory to render than 2D plots, so make sure you save your worksheets before constructing 3D plots (and use the Classic Maple worksheet).

If f(x,y) is a function that takes two arguments then the basic syntax for plotting f as an expression is

plot3d(f(x,y),x=a..b,y=c..d);

Example:f:=(x,y)->x*sin(y):plot3d(f(x,y),x=-1..1,y=-Pi..Pi);

Page 5: Chapter 6: Plotting in Maple 6.1. 2D plots - QMUL Mathswhitty/MTH4105/Weeks/Week6/Lectures/MTH4105...Chapter 6: Plotting in Maple ... The primary advantage of parametric plots is that

O O

O O

O O

By default, 3D plots are rendered without axes. Different plotting options are available to change the appearance of a 3D plot. We now plot the same function using a different viewing angle, and adding a title and axes:

plot3d(f(x,y),x=-1..1,y=-Pi..Pi,title="3D plotting example",axes=BOXED,orientation=[-60,60]);

3D plotting example

We see that the viewing angle is specified using the orientation=[f, q] option. Here f is the azimuthal angle defined in spherical polar coordinates, and q is the polar angle. These are the angles that define the line-of-sight to the origin of the plot. Maple allows the user to interactively change the viewing angle of a 3D plot by clicking on the left mouse button and dragging the image to the desired orientation.

Multiple 3D plotsMultiple 3D surfaces can be plotted by including the functions to be plotted in a set as the first argument of the "plot3D" command. Maple does not allow multiple 3D plots generated using the "plot3d" command to be contained in a list (unlike for 2D plots using the "plot" command).

f:=(x,y)->x^2+y^2:

Page 6: Chapter 6: Plotting in Maple 6.1. 2D plots - QMUL Mathswhitty/MTH4105/Weeks/Week6/Lectures/MTH4105...Chapter 6: Plotting in Maple ... The primary advantage of parametric plots is that

O O O O

O O

O O

O O

O O

O O

O O g:=(x,y)->x+4:plot3d({g(x,y),f(x,y)},x=0..3,y=0..3,axes=NORMAL,orientation=[-50,70]);

Example:One application of such plot3d is to examine the intersection of three planes, defined by the three equations

xC yC z K 4 = 0 xK yC z = 0 yC z K xC 2 = 0.

The point of intersection represents the solution of these equations.solve({x+y+z-4=0,x-y+z=0,y+z-x+2=0},{x,y,z});

z = K1, y = 2, x = 3

To plot these equations in the form of planes we write each of them such that z is a function of x and y:

f1:=(x,y)->4-x-y:f2:=(x,y)->y-x:f3:=(x,y)->x-y-2:plot3d({f1(x,y),f2(x,y),f3(x,y)},x=-2..4,y=-2..3,axes=FRAMED,orientation=[-60,60]);

Page 7: Chapter 6: Plotting in Maple 6.1. 2D plots - QMUL Mathswhitty/MTH4105/Weeks/Week6/Lectures/MTH4105...Chapter 6: Plotting in Maple ... The primary advantage of parametric plots is that

O O

O O

O O

O O

6.4. Parametric plotting of surfacesSurfaces that are not graphs of (single-valued) functions can be specified parametrically using two parameters that are the cartesian coordinates, x s, t , y s, t , z s, t , of each point on the surface, specified as functions of two variables.

In Maple the three expressions that define x s, t , y s, t and z s, t are specified as the elements of a list, thus:

plot3d([x s, t , y s, t , z s, t ], s=a..b, t= c..d);

Here is an example:Example:Geometrical shapes such as spheres can be plotted parametrically (using spherical polar coordinates to define the functions for x, y, z:

x = r sin q cos f , y = r sin q sin f , z = r cos q

where r is the radius of the sphere. Consider, for example, the plot of the sphere of radius r = 5:r:=5:plot3d([r*sin(theta)*cos(phi),r*sin(theta)*sin(phi),r*cos(theta)],theta=0..Pi,phi=0..2*Pi,scaling=CONSTRAINED);

Page 8: Chapter 6: Plotting in Maple 6.1. 2D plots - QMUL Mathswhitty/MTH4105/Weeks/Week6/Lectures/MTH4105...Chapter 6: Plotting in Maple ... The primary advantage of parametric plots is that

O O

O O

O O

O O

O O

O O

Spheroids can also be plotted by varying the radial size of the object in the x, y, and/or z directions. We define the long axis/axes to be a and the short axis/axes to be b. Consider the plot of the prolate spheroid whose long-axis a is twice that of the short axes b:

a:=2:b:=1:plot3d([b*sin(theta)*cos(phi),b*sin(theta)*sin(phi),a*cos(theta)],theta=0..Pi, phi=0..2*Pi,scaling=CONSTRAINED);

Consider the plot of the oblate spheroid whose long axes are twice that of the short axis:plot3d([a*sin(theta)*cos(phi),a*sin(theta)*sin(phi),b*cos(theta)],theta=0..Pi, phi=0..2*Pi,scaling=CONSTRAINED);

Page 9: Chapter 6: Plotting in Maple 6.1. 2D plots - QMUL Mathswhitty/MTH4105/Weeks/Week6/Lectures/MTH4105...Chapter 6: Plotting in Maple ... The primary advantage of parametric plots is that

O O

O O

O O

O O

A plot of two touching spheres can be obtained by including the coordinates of a lateral shift. Remember that a multiple plot in 3D requires the individual plots to be in set brackets. Here we make the centre of one sphere have cartesian coordinates (+1,0,0), and the other to be at (-1,0,0):

plot3d({[+1+sin(theta)*cos(phi),sin(theta)*sin(phi),cos(theta)],[-1+sin(theta)*cos(phi),sin(theta)*sin(phi),cos(theta)]},theta=0..Pi,phi=0..2*Pi,scaling=CONSTRAINED);

6.5. Space curvesGeneral curves in 3D can be plotted with the "plots" package. In general, a space curve is not the graph of a single valued function of two variables (z = f x, y , unlike a surface plot), since a space curve may be twisted in 3D space such that there are multiple values of f for a given value of (x, y). Such curves must be plotted parameterically, just as spheres and spheroids are, except that a space curve is plotted using a single parameter that changes continuously along the curve. The command for plotting a space curve is "spacecurve". Consider the example below where we plot a circular helix:

restart:

Page 10: Chapter 6: Plotting in Maple 6.1. 2D plots - QMUL Mathswhitty/MTH4105/Weeks/Week6/Lectures/MTH4105...Chapter 6: Plotting in Maple ... The primary advantage of parametric plots is that

O O

O O

O O

O O

O O

O O

with(plots):spacecurve([cos(t),sin(t),t,t=0..2*Pi],axes=NORMAL,colour=BLACK);

Note that Maple uses the same colour scheme to draw a space curve as it does to shade 3D geometrical shapes. We thus ask Maple to plot the space curve in black. We can modify this plot to increase the number of twists:

spacecurve([cos(2*t),sin(2*t),t,t=0..2*Pi],axes=NORMAL,colour=RED);

Optional: AnimationsMany additional plotting facilities, including animations, are provided in the Maple package called "plots". This package is loaded with the function-call

with(plots):Warning, the name changecoords has been redefinedA plot can be animated by displaying several related plots one after the other. If f x is the function of one variable (x) we are plotting, then we can consider each frame of the animation to be represented by f i, x where i is a variable that changes in each of the individual frames. Animation in Maple can

Page 11: Chapter 6: Plotting in Maple 6.1. 2D plots - QMUL Mathswhitty/MTH4105/Weeks/Week6/Lectures/MTH4105...Chapter 6: Plotting in Maple ... The primary advantage of parametric plots is that

O O

O O

O O

O O

O O

be achieved using the command "animate":

animate(f(i,x),x=a..b, i=j..k);

In this expression, x is the plot variable and i is the animation variable. Example:Note: All the examples pertaining to animations can be viewed "in action" in the Maple-file version ofthese notes.

Consider the following example of a travelling sine wave (moving to the right):animate(sin(x-i),x=-Pi..Pi,i=1..2*Pi);

xK3 K2 K1 0 1 2 3

K1

K0.5

0.5

1

Maple works by computing all of the frames of the animation and storing them in memory before theyare displayed. Maple can sometimes run out of memory (and perhaps freeze), so it is always advisableto save your work before running an animation. If this happens then try reducing the number of frames by using the "frames=" option in the "animate" command.

An animation may be viewed by clicking on the figure generated by the "animate" command and usingthe DVD-player style buttons that appear on the context bar. You will see that animation can be started, stopped, paused, speeded-up, slowed-down, etc., using these buttons.

The animation can also be saved as a multi-framed plot by clicking the right-hand mouse button on theanimation frame. If you Export the file as a gif then it can be displayed on a web page as an "animated gif" file.

Consider the following animation of a travelling Gaussian using 32 frames:animate(exp(-(x-i)^2),x=-4..4,i=-2..2,frames=32);

Page 12: Chapter 6: Plotting in Maple 6.1. 2D plots - QMUL Mathswhitty/MTH4105/Weeks/Week6/Lectures/MTH4105...Chapter 6: Plotting in Maple ... The primary advantage of parametric plots is that

O O

O O

O O

O O

O O

xK4 K3 K2 K1 0 1 2 3 4

0.2

0.4

0.6

0.8

1

We can use the variable i to modify the maximum value of the function from frame to frame:animate((abs(i)+1)*exp(-(x-i)^2),x=-4..4,i=-2..2,frames=32,title="Dancing Gaussian");

xK4 K3 K2 K1 0 1 2 3 4

1

2

3Dancing Gaussian

One can also use parametric plots to construct animations. Below is a complex animation generated byusing trigonometric functions to define the x and y coordinates parametrically. The figure thus generated is called a Lissajous figure.

animate([sin(2*t),cos(3*t+k),t=0..2*Pi],k=0..2*Pi,axes=NONE,frames=32);

Optional: Combining plotsMaple treats plots similarly to the way it treats data structures such as sets and lists. A plot can be constructed and stored in memory without being displayed, and can be assigned to a variable name.

Page 13: Chapter 6: Plotting in Maple 6.1. 2D plots - QMUL Mathswhitty/MTH4105/Weeks/Week6/Lectures/MTH4105...Chapter 6: Plotting in Maple ... The primary advantage of parametric plots is that

O O

O O

O O

• •

O O

O O

• •

O O

O O

O O

O O

O O

O O

O O

Example:restart:with(plots):

Warning, the name changecoords has been redefinedcircle1:=plot([cos(t),sin(t),t=0..2*Pi],scaling=CONSTRAINED):circle1;

K1 K0.5 0 0.5 1

K1

K0.5

0.5

1

circle2:=plot([0.5+cos(t),sin(t),t=0..2*Pi],scaling=CONSTRAINED):circle2;

K0.5 0 0.5 1 1.5

K1

K0.5

0.5

1

circle3:=plot([1+cos(t),sin(t),t=0..2*Pi],scaling=CONSTRAINED):circle4:=plot([1.5+cos(t),sin(t),t=0..2*Pi],scaling=CONSTRAINED):circle5:=plot([2+cos(t),sin(t),t=0..2*Pi],scaling=CONSTRAINED):

The "display" command in Maple can be used to combine plots either by superposing them or by animating them. The plots to be combined should be plot structures, produced by calls to plotting functions such as "plot", that have been assigned to variables or collected into sets or lists (we will normally use lists).

To superimpose a number of plots on a single graph, the first argument of display should be a set or list of plot structures.

To animate a sequence of plots (by showing them one after another) the first argument of "display" must be a list of plot structures.

Other options can be specified as arguments in the "display" command, but it cannot change the properties of the individual curves such as their colour or line style, but only the properties of the

Page 14: Chapter 6: Plotting in Maple 6.1. 2D plots - QMUL Mathswhitty/MTH4105/Weeks/Week6/Lectures/MTH4105...Chapter 6: Plotting in Maple ... The primary advantage of parametric plots is that

O O

O O

O O

O O

O O

entire plot such as titles or axes. Probably the best way to set options (such as line colour) is to changethe default using "setoptions" before constructing the plot structures to be combined.Example:First, let us combine the five circle plots that were defined above. First we will superimpose them on asingle curve

display([circle1,circle2,circle3,circle4,circle5],title="Circles");

K1 0 1 2 3

K1

K0.5

0.5

1

Circles

Now we animate them, where the option used in the "display" command to obtained an animation is "insequence=true":

display([circle1,circle2,circle3,circle4,circle5],insequence=true);

Page 15: Chapter 6: Plotting in Maple 6.1. 2D plots - QMUL Mathswhitty/MTH4105/Weeks/Week6/Lectures/MTH4105...Chapter 6: Plotting in Maple ... The primary advantage of parametric plots is that

O O

O O

O O

O O O O

O O O O

O O

K1 0 1 2 3

K1

K0.5

0.5

1

Example:Here we animate a sequence of sine curves. We start by defining a variable that stores a sequence of plot structures, where each plot in this structure is a sine curve displaced along the x-axis:

n_frames:=16: # this will be the number of framesdx:=2*Pi/(n_frames-1): # distance along x between each sine curveplot_seq:=seq(plot(sin(x-i*dx),x=-Pi..Pi),i=0..n_frames-1):display([plot_seq]); # display curves superimposed

xK3 K2 K1 0 1 2 3

K1

K0.5

0.5

1

display([plot_seq],insequence=true);

Page 16: Chapter 6: Plotting in Maple 6.1. 2D plots - QMUL Mathswhitty/MTH4105/Weeks/Week6/Lectures/MTH4105...Chapter 6: Plotting in Maple ... The primary advantage of parametric plots is that

O O

O O

O O

O O

O O O O

xK3 K2 K1 0 1 2 3

K1

K0.5

0.5

1

Optional: Changing default plotting options with "setoptions"The default options are the values used by plotting commands if no options are specified. For examplethe above plots of sine curves plot the curves in red by default, and use the values "scaling=UNCONSTRAINED" and "axes=NORMAL". These default options may be changed using the "setoptions" command. This is particularly useful if a number of different plots are to be constructed which require the same options to be defined, where those options differ from the default ones.Example:

setoptions(axes=BOXED,colour=blue):plot(sin(x),x=-2*Pi..2*Pi);

xK6 K4 K2 0 2 4 6

K1

K0.5

0

0.5

1

Optional: 3D animationAnimation can be performed in 3D using the "animate3d" and "display3d" commands in a way that is analogous to the cases already discussed for 2D animation (using "animate" and "display"). "animate3d" is used to animate surfaces that can be described as simple functions:

animate3d(sin(x-i),x=0..2*Pi,y=-1..1,i=0..2*Pi);

Page 17: Chapter 6: Plotting in Maple 6.1. 2D plots - QMUL Mathswhitty/MTH4105/Weeks/Week6/Lectures/MTH4105...Chapter 6: Plotting in Maple ... The primary advantage of parametric plots is that

O O

O O

O O

O O

O O

O O

O O

O O

O O

O O

"display3d" is used to animate 3D graphics by combining separate plot structures that are displayed frame-by-frame, giving the impression that they are changing with time. We present an example belowwhere we animate a bouncing ball.Example:

with(plots):setoptions3d(scaling=CONSTRAINED):

Create a user defined function "Ball" which plots a sphere at different heights.Ball:=z->plot3d([cos(theta)*sin(phi),sin(theta)*sin(phi),z+cos(phi)],theta=0..2*Pi,phi=0..Pi):

Create a 3D plot structure "Squash" which plots a oblated spheroid. This represents the squashing of the ball as it hits the surface from which it bounces.

Squash:=plot3d([cos(theta)*sin(phi),sin(theta)*sin(phi),-10.5+0.5*cos(phi)],theta=0..2*Pi,phi=0..Pi):

We now create a data structure "Bounce" that stores a sequence of images. The first 11 images plot thesphere at different heights (values of z). The next image is the squashed sphere (i.e., the ball as it hits the surface). The next 11 images are of the sphere at different heights as it bounces upward. Note that the use of "insequence=true" tells Maple to construct an animation.

Bounce:=display3d([seq(Ball(-k^2/10),k=0..10),Squash,seq(Ball(-k^2/10),k=-10..0)],insequence=true):

Construct a plot of a plane surface which represents the surface from which the ball bounces.Plane:=plot3d(-11,-2..2,-2..2):

Display the sequence of images as an animation containing the surface and the sequence of images showing the position of the ball changing as it falls, hits the surface, and bounces upward:

display3d({Plane,Bounce},title="Bouncing Ball");

Page 18: Chapter 6: Plotting in Maple 6.1. 2D plots - QMUL Mathswhitty/MTH4105/Weeks/Week6/Lectures/MTH4105...Chapter 6: Plotting in Maple ... The primary advantage of parametric plots is that

O O

O O

O O

Bouncing Ball