You are on page 1of 2

18.02A, 18.02, 18.

02IS at ESG

Fall 2003

Plotting Surfaces with MATLAB


These instructions are admittedly rudimentary. Those who will go on to use MATLAB more formally will learn the needed more advanced techniques. To start, keep in mind that MATLAB is sort of an abbreviation for MATrix LABoratory. What happens with MATLAB is that arrays of numbers are created, and then combined, plotted and otherwise manipulated. Matrix algebra is not identical to scalar algebra, and if we want the proper saclar manipulations, we have to ask nicely. These instructions will use the commands in the le plotdemo.m, linked from the various web pages. The sux .m means that this is a MATLAB le, and any work that you save should be of this form. These commands may be cut and pasted into your own buer, or of course copied. The commands will be given in these notes explicitly. To begin, start MATLAB either from the Athena dash or from athena% add matlab; matlab& and wait a while. If this is the rst time youve run MATLAB in a while, you may get a zephyr informing you about recent version updates. When the MATLAB window appears, with the prompt >>, youre ready to go, almost. (In these notes, the prompt will be given, but should not, of course, be typed.) The rst thing youll want to do is enter >>more on which toggles the more feature, allowing you to view the help menus one at a time. These notes will address the mesh and plot3 features, which you would view by entering >>help mesh or >>help plot3 What both of these features do is to plot the values of a two-dimensional array (which well be calling z or f , for reasons that might be obvious) for each pair of coordinates (x, y ) on a grid that we can dene. So, start with >>xx = -1.01:.05:0.99; >>yy = xx; >>[x,y]=meshgrid(xx,yy); which divides a rectangular region of the x-y plane into a [41 41] set of grid points. You may note immediately that the grid is not centered at the origin; Eventually, 1

we may want to divide by r = x2 + y 2 , and we need to stay away from the origin. To see what weve done so far, enter >>whos and see that while the variables xx and yy are 1 41 arrays, the variables x and y are 41 41 arrays. That is, both x and y have been dened at each grid point. So, lets plot something. Dene a function r by >>r = x.^2+y.^2; (yes, I know this should be r 2 , but its not worth messing with the variable names). The dot after both the x and y is crucial; this tells MATLAB to perform the operations term-by-term, intstead of mutiplying the matrices corresponding to the array. The basic commands to plot are then >>mesh(r) or >>plot3(x,y,r) and you will want to plot both to see which you prefer and why. Now, get fancy. Try >>z = x.*y.*(x.^2-y.^2)./r to dene an interesting function. Note that you need a dot for each operation. Plot this function as before, with >>mesh(z) or >>plot3(x,y,z) to get your own version of the 18.023-ESG Logo.

You might also like