MATLAB WORKSHOP Part 3 Graphs and Runtime Content

MATLAB WORKSHOP Part 3: Graphs and Runtime

Content • • • 2 D plots (advanced) 3 D plots Runtime analysis tic toc MATLAB profiler

2 D plots Plot commands: figure(1); theta= linspace(0, pi*5, 100); phi= pi : pi/10 : 5. 5*pi; plot(theta, sin(theta), 'r-o') hold on; plot(phi, cos (phi), 'k-o') Another method is to give Y-value matrix corresponding to several different curves. Each row vector in the matrix will be shown as a separate curve. For example: figure(2); X= theta; Y= [cos(X) ; 0. 5*sin(X)]; plot(X, Y)

2 D plots More than one plot in the window Sometimes we want to divide our window to a few panels. We do this using the subplot command: subplot(num_rows , num_columns , current_index) The current_index fits the numbering convetion starting from the left up corner, running over the row, and goes to the next line… For example, if num_rows=3, num_columns=4 current_index= 1 2 3 4 5 6 7 8 9 10 11 12

2 D plots Subplot example: figure(3); subplot(2, 1, 1) plot(theta, sin(theta), 'r-o') title('Sine function') subplot(2, 1, 2) plot(phi, cos(phi), 'k-o') title('Cosine function') or a= subplot(2, 1, 1); b= subplot(2, 1, 2); plot(a, theta, sin(theta), 'r-o'); plot(b, phi, cos(phi), 'k-o');

2 D plots Other 2 D plots: Function Action of the function hist Histogram of vector values bar Bar chart stem Plot of discrete points plotyy Two plots with different y axis in the same window

2 D plots x=randn(1, 100000); figure; hist(x) hist(x, -4: 0. 1: 4);

2 D plots Bar x= 1900: 10: 2010; y= [75 91 105 123. 5 131 150 179 203 226 244 281 307]; bar(x, y) xlabel('year') ylabel('Number of citizens in millions') title ('US population')

2 D plots stem x= linspace(-10, 200); y= sinc(x); stem(x, y) title('sinc function')

2 D plots plotyy x= 0: 0. 01: 20; y 1= 200*exp(-0. 05*x). *sin(x); y 2= 0. 8*exp(-0. 5*x). *sin(10*x) figure; plotyy(x, y 1, x, y 2) legend('function 1', 'function 2')

2 D plots Additional functions • Add grid lines grid on; • Limit range of both X and Y axes axis([x_min , x_max , y_min , y_max]) • Add text anywhere on the plot text(x_val , y_val , 'some text') • You can change font size by Fontsize [y_max, ind] = max(y); text(x(ind) , y_max, 'Max' , 'Fontsize' , 15) All these can be added also using the menus in the figure window.

2 D plots Additional functions • Change the axis scale to a logarithmic scale: you can use semilogx or semilogy or loglog functions instead of plot. • Any specific color can be set by using the Color field and entering a vector of RGB color (3 values in the range[0, 1]); x=1: 0. 1: 20; plot(x, besselj(1, x), ’Color’, 0. 5*[0 1 0]) hold on plot(x, besselj(2, x), ’Color’, [128 0 256]/256)

3 D plots Different types of 3 D plots Function Action of the function plot 3 Curve (line) in 3 D mesh 3 D surface, the surface will be semi-transparent surf 3 D surface, the surface will be opaque contour Contour plot (z axis is the color) imagesc Normalized color map

3 D plots plot 3: Plotting the 3 D points created using the vectors (x(t), y(t), z(t)) and connecting them with a curve t = linspace(0 , 10*pi , 100); x = cos(t); y = sin(t); z = t / (2*pi); figure(99); plot 3(x , y , z); xlabel('x'); ylabel('y'); zlabel('z'); title('Spiral'); grid on

3 D plots Draw grid • Use meshgrid function to draw mesh X(i, j)=x(j) Y(i, j)=y(i) [X , Y]= meshgrid(x , y) ; x= 1 y= 10 2 3 20 4 30 X= 1 2 3 4 10 10 Y= 20 20 30 30

3 D plots mesh, surf, contour Let’s draw 2 D Gaussian distribution z(x, y) x= -10: 0. 5: 10; y= -10: 0. 5: 10; [X , Y]= meshgrid(x , y) ; Z= 1/sqrt(2*pi)*exp(-0. 5*(X. ^2 + Y. ^2)) ; figure; mesh(X , Y , Z) ; title('mesh' , 'Font. Size' , 15) ; figure; surf(X , Y , Z) ; title('surf' , 'Font. Size' , 15) ; figure; contour(X , Y , Z) ; title('contour' , 'Font. Size' , 15) ; colorbar % presenting a color bar next to the graph

3 D plots mesh, surf, contour

3 D plots imagesc Receives vectors of x and y values and matrix Z and plots Z(x, y) as a color map x= -10: 0. 01: 10; y= -10: 0. 01: 10; [X , Y]= meshgrid(x , y) ; Z= X. * Y. * (X. ^2 - Y. ^2 -10). / (Y. ^2 +X. ^2+1); figure; imagesc(x , y , Z) ; title('imagesc' , 'Font. Size' , 15) ; colorbar

Runtime analysis tic toc We can measure code runtime by writing a tic command before the code and toc at the end of the code. MATLAB will display the calculation time. tic SOME CODE toc Example Comparing the runtime of summing two very long vectors: 1 By using for loop 2 By using vector connection

Runtime analysis Solution clear all x= rand(1, 1 e 7); y= rand(1, 1 e 7); tic % start time measuring z = x + y; toc % end time measuring tic % start time measuring z= zeros(size(x)); for ii=1: length(x) z(ii)= x(ii)+y(ii); end toc % end time measuring Conclusion: vector/matrix calculation is more efficient than for and while loops.

Runtime analysis profiler We can analyze runtime of a function/code by writing the profile on command before calling a function and profile off at the end of the function. profile on SOME FUNCTION profile off profile viewer

Tips Information about variables and functions • The command whos gives information about variable: size and type whos x % Name %x Size 1 x 10000000 Bytes 80000000 Class double Attributes • The command which gives the address on the comuter, where a script is stored: which my_script % C: tempmy_script. m • isempty check whether an array is empty (1 means empty) • Keyboard commands • F 5 runs program • F 9 runs several lines of code that are currently selected.
- Slides: 22