You are on page 1of 6

MatLab Programming Lesson 3

1) Log into your computer and open MatLab 2) If you dont have the previous M-scripts saved, you can find them at http://www.physics.arizona.edu/~physreu/dox/matlab_lesson_1.pdf, and http://www.physics.arizona.edu/~physreu/dox/matlab_lesson_2.pdf. More 3-D Graphing: isosurface Sometimes you have a scalar three-dimensional function. That is for every point in space (x,y,z) you have a value at that point, f(x,y,z). There is no complete way to graph this in 3-D since there are four variables: x, y, z, and f. However, if f is a continuous function, then you can restrict the graph to plot only where the values are equal. Copy the following script into a new M-file.

close all; clear all; clc; format long e; fun_function = inline('cos(x)+cos(y)+cos(z)','x','y','z'); % Don't really need to use an inline function here, % but just to review. points=linspace(-pi,pi,51); [x,y,z]=meshgrid(points,points,points); % Make a 3-D mesh grid or else graphing f won't work. % Notice this is a grid of 51^3 = 132,651 spatial points. f = fun_function(x,y,z); % Make the 132,651 data points at each spatial point. isosurface(x,y,z,f,1.1); grid on; title(['My Fun Function, Contour Value = ',num2str(1.1)]); xlabel('X');ylabel('Y');zlabel('Z'); axis([-pi,pi,-pi,pi,-pi,pi]);

Practice: Try this plot for contour values of 2, 1, .5, and .1.

Multiple Graphs Separate Figures You will often want to create more than one graph inside a program. Copy the following script into a new M-file.

close all; clear all; clc; format long e; x = linspace(0,10,100); y = linspace(-pi,pi,100); z = linspace(0,5,100); [X,Y,Z] = meshgrid(x,y,z); f_a = x.^3 - 2*x; f_b = (sin(X).^2).*cos(Y).*exp(-Z.^2); figure(1); % Make the first graph. isosurface(x,y,z,f_b,.2); figure(2); plot(x,f_a); % Make the second graph.

Practice: A particle is set to move in in the x-y plane via the parameterization: x=cos(2t) and y=sin(4t) for t = [0,5]. Create three graphs using one program that show x vs. t, y vs. t, and x vs. y.

Multiple Graphs Separate Figures (More) (SKIP THIS SECTION, GO TO NEXT PAGE.) Sometimes you have a scalar three-dimensional function. That is for every point in space (x,y,z) you have a value at that point, f(x,y,z). There is no complete way to graph this in 3-D since there are four variables: x, y, z, and f. However, if f is a continuous function, then you can restrict the graph to plot only where the values are equal. Copy the following script into a new M-file. close all; clear all; clc; format long e; fun_function = inline('cos(x)+cos(y)+cos(z)','x','y','z'); points=linspace(-pi,pi,51); [x,y,z]=meshgrid(points,points,points); f = fun_function(x,y,z); % Here is a trick for making multiple graphs. contour_values = [.1,.5,1,1.1,2]; for i_loop = 1:size(contour_values,2), figure(i_loop); isosurface(x,y,z,f,contour_values(i_loop)); grid on; title(['My Fun Function, Contour Value = ',... num2str(contour_values(i_loop))]) xlabel('X');ylabel('Y');zlabel('Z'); axis([-pi,pi,-pi,pi,-pi,pi]); end; Practice: No practice , but note how cool it is to automatically generate graphs.

Multiple Graphs Subplots (SKIP THIS SECTION FOR NOW, GO TO NEXT PAGE.) It is sometimes useful to make several graphs in the same figure, usually for easy comparison. This is done using subplot. Copy the following script into a new M-file.

close all; clear all; clc; format long e; x = linspace(0,10,100); figure(1); subplot(3,1,1); % '3' means set up 3 graphs verticaly, % '1' means 1 graph wide, and the last % '1' means the first plot. plot(x,cos(x)); title('Examine sin(x) times cos(x)'); subplot(3,1,2); % '2' for the second graph. plot(x,sin(x)); subplot(3,1,3); plot(x,sin(x).*cos(x));

Practice: Make three subplots horizontally arranged of a cycloid. Go to wikipedia, http://en.wikipedia.org/wiki/Cycloid, and find the correct parameterization of the cycloid.

Running Another M-file You may find that both your data analysis and graphing code getting very large. You can always separate them into two different programs and have one program run the other. 1) Copy the following script into a new M-file called one.m user_response = ... input('Are you happy? (''1'' for yes, ''2'' for no.) \n'); if user_response==1, disp('Oh brother!'); elseif user_response==2, disp('Boo hoo.'); else, disp('Wrong answer!'); end; 2) Now copy the following script into a new M-file called. close all; clear all; clc; format long e; user_response = input('Should I run one.m? (Y/n) \n','s'); if user_response=='Y', run one; % Notice that you don't type '.m' to run one.m. else, pause(); % 'pause()' waits for the user to type any key. fprintf('\n Whatever.\n\n'); pause(2); % 'pause(2)' waits for 2 seconds. fprintf('\n Goodbye.\n\n'); end; Practice: Make the above program also run another of your old M-files.

M-file Functions (SKIP THIS SECTION FOR NOW, GO TO NEXT PAGE.) The use of subroutine functions is common to all modern programming languages. Below is the simplest such example. There is a M-file-function called matt that takes two arrays and concatenates them together. The second M-file merely needs to use matt by giving it two input arrays. 1) Copy the following script into a new M-file called matt.m function m = matt(x_in, y_in) % Note that this function-line statement has % precise syntax you must follow. 'matt' here % must match the name of the M-file, 'matt.m', % and 'm' must be calculated below. m = [x_in,y_in]; % This statement determines the return value of the % function, 'm'. However, you can return more than % one variable by using '[m_1, m_2] = matt(x_in, y_in)' % as your function-line statement. 2) Now copy the following script into a new M-file called. close all; clear all; clc; format long e; x = [1,2,3]; y = [4,5]; matt(x,y)

Practice: Make an M-file function using your name that is cooler than mine.

(END OF LESSON 3)