MA 223 Lab #1 Name _____________________ Graphing With MATLAB This lab is intended to introduce you to some of the graphics features in Matlab. The focus will be on drawing surfaces, contours, surface intersections, normal lines and planes. Open Matlab with Start|Programs|Matlab. We will create 2 m-files that you can save on your flash drive or other device for later use. As the m-file is being created, you can leave an entry to use later by placing a % symbol at the beginning of the line. This is Matlab's comment character. Any line with a % at the beginning will be ignored when you run the program. 1. Plotting Surfaces In the Matlab window, click on the New M-file button at the top left corner of the menu. The button appears as a blank sheet. This will open up a blank window in the Editor where you will enter your program. Type in the following: (The lines in italics are what you will type in. Also, myfilename is the name of your program, so enter something original??? Give it a path if you want to save to a flash drive or some other device. ) function []=myfilename; [x,y] = meshgrid(-4:0.1:4,-4:0.1:4); z1=x.^2+y.^2; z2=16-x.^2; surf(x,y,z1); hold on; surf(x,y,z2); xlabel('x'); ylabel('y'); zlabel('z'); Click on File|Save as, and save your m-file to whatever path you want to use. Now return to the command window of Matlab and type >> myfilename You should see a new Figure window appear, two intersecting surfaces, and the x,y,z axes labeled x,y, and z. You can change these labels to something else if you want, like 'x-axis', etc. Click on the Rotate 3D button on the Figure menu, place your cursor on the figure and move it around. Return to the Editor, and replace the line surf(x,y,z1); with surfc(x,y,z1); Click on File|Save and run the program again. Hint: to run the program again at the command line, just hit the up-arrow key and your file name will re-appear, then hit the Enter key. Now the contours of the first surface also appear. You can also change the colors of your surfaces with colormap: Add the following line: colormap('autumn'); Save and run the program again. Now comment out this last line and add this line: shading interp; To see more about coloring, or any of the commands used here, type help colormap or help shading etc. at the command line. Also, for example, if you type help colormap, you can then click on colormap doc for more information and examples. 2. Plot the curve of intersection: When we compute volumes of solids bounded by 2 or more surfaces later in the course, we will need to find the curve in 3-space where 2 surfaces intersect. We will draw this curve now, using the 2 surfaces we've been plotting. These 2 surfaces intersect when z1=z2, i.e., when x.^2+y.^2=16-x.^2. Solve for y to get 2 curves, y = sqrt(16-2*x.^2) and y = -sqrt(16-2*x.^2) Add the following lines to your program: display('Press a key to continue'); pause; % Plot the curve of intersection. % the first 2 lines create the curve of points (x1,y1) where surfaces intersect. x1=-3:0.1:3; y1=sqrt(16-2*x1.^2); % Evaluate one of the surfaces (either one) along this path at (x1,y1). z4=x1.^2+y1.^2; % Now plot the yellow curve of intersection. plot3(x1,y1,z4,'y-','LineWidth',2); hold on; plot3(x1,-y1,z4,'y-','LineWidth',2); Save and Run. The pause statement above let's you see what has been drawn to that point in the code. After drawing the surfaces, press any key to add the yellow curve of intersection. Now let's project this curve onto the xy plane. Enter the following: display('Press a key to continue'); pause; z5=zeros(length(x1)); % here, z5 is a column of 0's. plot3(x1,y1,z5); plot3(x1,-y1,z5); Save and Run. You should see an ellipse in the xy plane just below the previous yellow curve of intersection. 3. Plotting a surface, a normal vector and a plane. It may be easier to just start a new M-file by clicking on the New M-file button again. Enter the following lines, some of which you might copy and paste from your previous program. Function [] = myfilename2; Click on File|Save as, and save this new program. Then enter the following: [x,y] = meshgrid(-3:0.1:3,-3:0.1:3); z6=3*cos(2*y)./(abs(y)+sin(3*x)+5); surf(x,y,z6); hold on; % (0,0,3/5) is a point on the surface. Quiver draws an arrow from (0,0,3/5) to % 0.36,0.12,1.6), which will be normal to the surface. (I did the math for you!!) quiver3(0,0,3/5,0.36,0.12,1.6); % Draw a small tangent plane, center at (0,0,3/5). The tangent plane is just % another surface, but we don't want it to cover everything under it. [x2,y2]=meshgrid(-1:0.1:1,-1:0.1:1); z7=(15-9*x2-3*y2)/25; surf(x2,y2,z7); Save and Run. You should see a wavy surface, a tangent plane and normal line. Now comment out the lines for the tangent plane above so they look like this: % [x2,y2]=meshgrid(-1:0.1:1,-1:0.1:1); % z7=(15-9*x2-3*y2)/25; % surf(x2,y2,z7); Then add the following lines to plot normal vectors all over the surface: [u,v,w]=surfnorm(x,y,z6); quiver3(x,y,z6,u,v,w); Save and Run. Zoom in and rotate to see normal vectors to the surface. 4. Plotting a surface, contours and gradient vectors. Start a new m-file and enter the following code. Function [] = myfilename3; % Plot contours of a surface and gradient vectors minx=-3; maxx=3; dx=0.2; miny=-3; maxy=3; dy=0.2; [x,y]=meshgrid(minx:dx:maxx,miny:dy:maxy); % Plot a surface z=3*sin(y)./(x.^2+1); %z=3*cos(2*y)./(abs(y)+sin(3*x)+5); surfc(x,y,z); xlabel('x'); ylabel('y'); zlabel('z'); display('Press a key to continue'); pause; figure; [px,py]=gradient(z,.2); contour(x,y,z,10); hold on; quiver(x,y,px,py); xlabel('x'); ylabel('y'); Save and Run. Gradient vectors are perpendicular to contours and pointing in the direction of increasing z. There are many commands in Matlab for graphics, which makes it such a versatile graphics package for plotting up all kinds of data. Matlab works with matrices as the fundamental unit. The meshgrid command constructs two column vectors of x and y values. You can select any intervals and increments you like. For example, meshgrid(minx:dx:maxx, miny:dy:maxy) creates an x and y column vector. The x column vector starts at minx and ends at maxx with an x value every dx units. Finally, the following m-file is useful if you want to save some data you have created and format it in a convenient format of columns of numbers, etc., which can then be opened in a spreadsheet or word document,or loaded back into Matlab. This is a very handy little m-file. There are some examples of how to format your file. %d5 means to save an integer with 5 places, %8.6f means to save a real number with floating point, 6 decimal places. The \n means to start a new line. function []=saveascii(file,data,format); % SAVEASCII save an array in a formatted form % Usage: saveascii(file,data,format); % file = file name, give your new file a name, like 'myfile'. % data = array to save, the data you want to save. % format = format to be used for each line (eg '%3d %3d %5.6f\n') fid=fopen(file,'w'); %format='%5d %5d %5d %5d %5d\n'; ...this is 5 columns of integers %format='%6d %10.5f\n'; ....this is 1 column of integers, 1 column of reals. %format='%8.6f %8.6f\n' fprintf(fid,format,data'); fclose(fid); disp([file ' created!']);