VIEWS: 23 PAGES: 6 POSTED ON: 3/5/2010
1 MAS1001 – Maple Sheet 2 Graphics and Plotting This session is
1 MAS1001 – Maple Sheet 2 Graphics and Plotting This session is devoted to learning more about using Maple to produce plots (in two and three dimensions, and in different formats). It is very often the case that a figure, perhaps that associated with a function, is of great help in understanding some property of the function or, perhaps, in seeing how it might be relevant to a particular application. As part of our work on Sheet 1, we briefly introduced the plot instruction; we will start from here. [If you want to print the picture that is produced, you have a number of options. You could simply print the whole Maple worksheet – just use the print command from ‘File’, but that will also print all the instructions that preceded the picture, which you may not want. Usually a more convenient route is to right-click over the figure; a menu of instructions will then appear and you may choose to save the picture in any appropriate format (and then print outside Maple). Probably a neater method is to copy the final line (e.g. the one containing the plot command), open a new Maple window, paste and then hit enter; Maple remembers everything from the original window, runs and plots. Now you may print this new page.] 1. Reminder First produce these plots: > plot(exp(-x)*sin(x),x=0..5*Pi); > plot(x^3-5*x^2,x=-2..6); > plot(ln(x),x=0..6,y=-5..2,color=black); but now you may want to change the look of the figure. 2. Changing the look of the plot You might wish to add a title, so enter > plot(sin(x),x=0..2*Pi,title=`Sinx with Maple`); and note that we must use the ‘back-apostrophe’ (called the ‘grave’) here – it is on the top-most key to the left (in the main block of keys). Perhaps you would prefer axes all around (usually called ‘boxed’), then write > plot(sin(x),x=0..2*Pi,axes=boxed); (and remember that you do not need to type all this again – simply make changes to the previous line, and then enter the command by moving the cursor to the end of the line and hitting return). Now try > plot(sin(x),x=0..2*Pi,axes=frame); > plot(sin(x),x=0..2*Pi,axes=none); 2 but then perhaps you are dissatisfied with the tick marks on the axes that are placed (automatically) by Maple, so try > plot(sin(x),x=0..2*Pi,xtickmarks=4,ytickmarks=8); and all these instructions can appear on one command line, inside plot( ), provided that they are separated by commas, so finally try something like > plot(sin(x),x=0..2*Pi,title=`Sin x`,axes=boxed,xtickmarks=6, ytickmarks=3,color=black); (and it does not matter if your command line wraps onto the next line, because it turns out to be so lengthy). 3. Multiple plots If you wish to plot a number of (simple) functions on the same graph, then you might try this > plot([x,x^2,x^3,x^4],x=0..2); and note the use of square brackets, and also observe that Maple automatically chooses to use different colours for each curve, and these can be changed e.g. > plot([x,x^2,x^3,x^4],x=0..2,color=[black,orange,blue,yellow]); or as you wish. Another method that produces a set of curves is e.g. > f:=(x,n)->sin(n*x); > plot([seq(f(x,n),n=1..4)],x=0..2*Pi); and take careful note of the shape of the brackets, the way that these are nested and the use of the seq (sequence) command. If the functions have been plotted separately, they may still be represented in one figure; first let us define two new functions: > f1:=x->sin(x)/x; > df1:=diff(f1(x),x); which defines a function ( y = x −1 sin x ) and its derivative. Now we will STORE the plots of these two (which we will call p1 and p2), by writing > p1:=plot(f1(x),x=0..5): > p2:=plot(df1(x),x=0..5): where the use of the colon ensures that nothing is seen – yet. In order to display these, we must first load some higher-level commands; to do this enter > with(plots): 3 where the colon means that we do not see the names of all the new commands that are now available. (If you do want to see them, replace ‘:’ by ‘;’.) Now use the command > display(p1,p2); and the two graphs appear in the same figure. 4. Parametric and polar plots A function that is defined in parametric form can be plotted very simply in Maple; for example > plot([t*cos(t),t+sin(t),t=-2*Pi..2*Pi],color=black); produces the curve x = t cos t , y = t + sin t , −2π ≤ t ≤ 2π . Also, plotting in polar coordinates rather than Cartesian (which is the default), is also very straightforward: > plot(sin(t),t=0..Pi,coords=polar); were you able to predict this shape? Now try these two > plot(1+cos(t),t=0..2*Pi,coords=polar); > plot(sqrt(cos(2*t)),t=-Pi..Pi,coords=polar); which are the cardioid and Bernoulli’s lemniscate, respectively. (You may find that the lemniscate is not well-generated near the origin; Maple uses only a relatively few points when plotting in the default mode, but this can be improved by asking that more points are used, so try > plot(sqrt(cos(2*t)),t=-Pi..Pi,coords=polar,numpoints=1000) ; – a considerable improvement.) 5. Curves defined implicitly Maple can also plot curves that are not expressed in the explicit form y = f ( x ) ; for example, plotting the curve defined by y[1-x/(x^2+y^2)]-1=0 becomes > implicitplot(y*(1-x/(x^2+y^2))-1,x=-2..2,y=-3..3); and note that this requires that with(plots) has been loaded; see above. (Once this has been loaded during a session, you do not need to do it again – but you will need to do it in the future, whenever you wish to use implicitplot.) You are likely to be dissatisfied with the quality of the plot: Maple selects a relatively small number of points for plotting curves (see comment above) with this command. However, the accuracy can be improved: change the above to read > implicitplot(y*(1-x/(x^2+y^2))-1,x=-2..2,y=-3..3,grid=[50,60]); 4 which uses 50 points in the x-direction and 60 in the y-direction. Comment: implicitplot requires that you specify the domain and the range, defining the region to be plotted. 6. 3D plots Maple can plot functions of two variables, thereby generating surfaces in three-space. For example, try > plot3d(1-x^2-y^2,x=-1..1,y=-1..1); now left click over the figure, and then hold down the left button and move – the orientation of the surface changes! (This is the surface z = 1 − x 2 − y 2 , −1 ≤ x ≤ 1 , −1 ≤ y ≤ 1 .) Your figure has no axes; these may be added: > plot3d(1-x^2-y^2,x=-1..1,y=-1..1,axes=framed); and you may still change the orientation, as above. Perhaps you would prefer to see the surface in a box: > plot3d(1-x^2-y^2,x=-1..1,y=-1..1,axes=boxed); and this representation is quite useful because it makes a little clearer what is happening as the orientation is changed. The surface has appeared as a coloured wire frame; other formats are possible e.g. > plot3d(1-x^2-y^2,x=-1..1,y=-1..1,axes=boxed,style=point); you might like to experiment by replacing point by wireframe, or hidden, or patch, or patchnogrid (and you can also change the colour e.g. include color = red). 7. Contour plots A contour plot draws lines of constant height i.e. constant values of z (in our usual notation). For example, the 3D plot of z = sin x sin y ( −π ≤ x ≤ π , −π ≤ y ≤ π ) is > plot3d(sin(x)*sin(y),x=-Pi..Pi,y=-Pi..Pi,axes=boxed); whereas the contour version is > contourplot3d(sin(x)*sin(y),x=-Pi..Pi,y=-Pi..Pi, axes=boxed,contours=10); and you might experiment with different numbers of contours. (Note that this command needs with(plots) to have been loaded; a 2D version of contour plots is available by using contourplot rather than contourplot3d.) This orientation can also be changed, as described above. 5 8. Animation It is possible to animate figures (so that they change in time); enter > animate3d([sin(t+s)*cos(p),sin(t+s)*sin(p),cos(s)],s=0..Pi,p=0..2*Pi, t=0..Pi,axes=boxed,scaling=constrained,frames=16); (which also requires with(plots) to have been loaded); when the animation has been constructed – the cursor has reappeared – click over the figure and then select the run button (the large arrow) from the new section of the title bar that has now become available. (You may like to examine, as a mathematical exercise, the nature of this surface, defined by x = sin(t + s ) cos p , y = sin(t + s ) sin p , z = cos s , as t varies.) 9. Numerical data sets There will be times when you will want to plot data which are obtained from a mathematical calculation (via a numerical routine) or from a statistical analysis. The main method for handling this is a list and an array. A list consists of two (or more) numbers in square brackets e.g. [1,2]; further, we may construct a list of lists: [[1,1],[2,4],[3,9]] and these can be plotted directly, in the form > plot([[1,1],[2,4],[3,9],[4,16]]); and the result is a set of straight lines joining consecutive points. We can dispense with the lines, and simply produce points: > plot([[1,1],[2,4],[3,9],[4,16]],style=point,color=black); although the default is style=line. It is also possible to define a single quantity which is the list of lists, and then plot this e.g. > data1:=[[1,1],[2,1/2],[3,1/3],[4,1/4]]: > plot(data1); but the real use of these structures is when we have generated a set of data values within Maple; this is most efficiently accomplished using a do loop. (You will make much use of this in a few specific modules, so we will write fairly little here.) 10. Do loops Suppose that we wish to compute (and eventually plot) the values of y = xe − x at a number of points (a calculation which mimics that associated with the numerical integration a differential equation, for example). We shall calculate the values of y on a uniform mesh 0 ≤ x ≤ 1 of N + 1 equally-spaced points x = nh ( n = 0, 1, ..., N ), where h = 1/ N . We accomplish this by using a do loop, and then storing the values in two arrays. Let us choose to use N = 10, then h = 0 ⋅ 1; first we must define the arrays that we need, of the appropriate size, so we enter 6 > y:=array[0..10]: > x:=array[0..10]: then we must define h > h:=0.1: and now the do loop is constructed (which will appear all within one bracket): > for i from 0 to 10 do > x[i]:=i*h: > y[i]:=x[i]*exp(-x[i]): > od: but do not worry about the warning after you enter the ‘do’ line – Maple does not yet know that you will end this with ‘od’; the final colon above suppresses the output (which can be seen if you use ‘;’ instead); finally we define the list of lists and then we plot: > data2:=[[x[n],y[n]]$n=0..10]: > plot(data2); which completes the task. Be careful to enter each command EXACTLY as written here, including the ‘dollar’ symbol. Finally, if you have generated two (or more) data sets, these may be represented in the same figure by using the display command (in §3 above). So we may write > pp1:=plot(data1): > pp2:=plot(data2): > with(plots): > display(pp1,pp2); where we have remembered to call up with(plots) (because it may not have been done earlier). Here, we have combined the new data2 set with the previous data1 set obtained in §9. When you have finished, you might want to save all this to a file (so that you can refer to it in the future). You might also want to keep a hard copy. You are encouraged to try any number of variants of these examples, so that you become skilled at these basic operations. REMEMBER TO EXIT (AND LOGOUT). RSJ Sept 2006