Barak Shenhav Early Evolution Course 11 Feb 2001 MATLAB Visualization, Functions & Debugging.

30
Barak Shenhav Early Evolution Course 11 Feb 2001 MATLAB Visualization, Functions & Debugging

Transcript of Barak Shenhav Early Evolution Course 11 Feb 2001 MATLAB Visualization, Functions & Debugging.

Page 1: Barak Shenhav Early Evolution Course 11 Feb 2001 MATLAB Visualization, Functions & Debugging.

Barak Shenhav

Early Evolution Course

11 Feb 2001

MATLAB Visualization, Functions & Debugging

Page 2: Barak Shenhav Early Evolution Course 11 Feb 2001 MATLAB Visualization, Functions & Debugging.

Visualization

Page 3: Barak Shenhav Early Evolution Course 11 Feb 2001 MATLAB Visualization, Functions & Debugging.

Getting Started

Page 4: Barak Shenhav Early Evolution Course 11 Feb 2001 MATLAB Visualization, Functions & Debugging.

Creating a Plot

x = 0:pi/100:2*pi;

y = sin(x);

plot(x, y)

Page 5: Barak Shenhav Early Evolution Course 11 Feb 2001 MATLAB Visualization, Functions & Debugging.

Multiple Data Sets

y2 = sin(x - .25);

y3 = sin(x - .5);

plot(x, y, x, y2, x, y3)

legend('sin(x)', 'sin(x - .25)', 'sin(x - .5)')

Page 6: Barak Shenhav Early Evolution Course 11 Feb 2001 MATLAB Visualization, Functions & Debugging.

Line Styles, Colors and Markers

plot(x, y, 'r:+')

x1 = 0:pi/100:2*pi;

x2 = 0:pi/10:2*pi;

plot(x1, sin(x1), 'r:', x2, sin(x2), 'r+')

Page 7: Barak Shenhav Early Evolution Course 11 Feb 2001 MATLAB Visualization, Functions & Debugging.

Multiple Plots

plot(x, y, 'r')

figure

plot(x, y, 'b')

figure(1)

plot(x, y, 'g')

close all

subplot(2, 2, 1);

plot(x, y, 'r')

subplot(2, 2, 2);

plot(x, y, 'b')

subplot(2, 2, 3);

plot(x, y, 'g')

subplot(2, 2, 4);

plot(x, y, 'y')

Page 8: Barak Shenhav Early Evolution Course 11 Feb 2001 MATLAB Visualization, Functions & Debugging.

Axes Controlplot(x, y, x, y2, x, y3) axis([0 2*pi –1 1])grid on

axis autoaxis offaxis onaxis equalaxis square

Page 9: Barak Shenhav Early Evolution Course 11 Feb 2001 MATLAB Visualization, Functions & Debugging.

Other Plotsplot - Graph 2-D data with linear scales for both axes

plot3 - Graph 3-D data with linear scales for both axes

loglog - Graph with logarithmic scales for both axes

semilogx - Graph with a logarithmic scale for the x-axis and a linear scale for the y-axis

semilogy - Graph with a logarithmic scale for the y-axis and a linear scale for the x-axis

plotyy - Graph with y-tick labels on the left and right side

Page 10: Barak Shenhav Early Evolution Course 11 Feb 2001 MATLAB Visualization, Functions & Debugging.

3-D Plot

t = 0:pi/50:10*pi;

plot3(sin(t), cos(t), t)

grid on

axis square

Page 11: Barak Shenhav Early Evolution Course 11 Feb 2001 MATLAB Visualization, Functions & Debugging.

Labels and Titles

x = -pi:pi/100:pi;

y = sin(x);

plot(x,y)

axis([-pi pi -1 1])

ylabel('sin(x)')

xlabel('-\pi \leq {\itx} \leq \pi')

title('\fontsize{16}Graph of the sine function')

text(1,-1/3,'\bf{Note the odd symmetry.}')

Page 12: Barak Shenhav Early Evolution Course 11 Feb 2001 MATLAB Visualization, Functions & Debugging.

Get Properties

Finding handles:

• gcf - get current figure handle

• gca - get current axes handle

Getting properties :

• get(h) - all properties of the graphics object identified by the handle h.

• get(h, 'PName') - get the value of the property ‘PName' of the graphics object identified by h.

Page 13: Barak Shenhav Early Evolution Course 11 Feb 2001 MATLAB Visualization, Functions & Debugging.

Set PropertiesSetting properties :

• set(h, ’PName', PValue, ...) - sets the named properties to the specified values on the object(s) identified by the handle(s).

For example :

x = -pi:.1:pi;y = sin(x);plot(x, y)set(gca, 'XTick', -pi:pi/2:pi)set(gca, 'XTickLabel', {'-pi','-pi/2','0','pi/2','pi'})

Page 14: Barak Shenhav Early Evolution Course 11 Feb 2001 MATLAB Visualization, Functions & Debugging.

Bar Chartsy = round(rand(5,3)*10);subplot(2 , 2, 1)bar(Y, 'group')title 'Group'subplot(2, 2, 2)bar(Y, 'stack')title 'Stack'subplot(2, 2, 3)barh(Y, 'stack')title 'Stack'subplot(2, 2, 4)bar(Y,1.5)title 'Width = 1.5'

Page 15: Barak Shenhav Early Evolution Course 11 Feb 2001 MATLAB Visualization, Functions & Debugging.

Pie Chart

val = [12 17 21];explode = [0 0 1];h = pie(val, explode);colormap summertextObjs = findobj(h, 'Type', 'text');newStr = {'X'; 'Y'; 'Z'};set(textObjs, {'String'}, newStr)

Page 16: Barak Shenhav Early Evolution Course 11 Feb 2001 MATLAB Visualization, Functions & Debugging.

Histogram

Y = randn(10000,3);

hist(Y)

Page 17: Barak Shenhav Early Evolution Course 11 Feb 2001 MATLAB Visualization, Functions & Debugging.

Contour and Quiver

n = -2.0:.2:2.0;

[X,Y,Z] = peaks(n);

[C, h] = contour(X,Y,Z,10);

Colormap('default');

clabel(C,h)

[U,V] = gradient(Z,.2);

hold on

quiver(X,Y,U,V)

hold off

Page 18: Barak Shenhav Early Evolution Course 11 Feb 2001 MATLAB Visualization, Functions & Debugging.

Mesh

[U, V] = meshgrid(-8:.5:8);

R = sqrt(U.^2 + V.^2) + eps;

H = sin(R)./R;

mesh(H)

Page 19: Barak Shenhav Early Evolution Course 11 Feb 2001 MATLAB Visualization, Functions & Debugging.

Surf

surf(X, Y, Z, 'FaceColor', 'interp', …

'EdgeColor', 'none', … 'FaceLighting', 'phong')

axis tight

view(-50,30)

camlight left

Page 20: Barak Shenhav Early Evolution Course 11 Feb 2001 MATLAB Visualization, Functions & Debugging.

MoviesZ = peaks;surf(Z); axis tightset(gca, 'nextplot', 'replacechildren');% Record the moviefor j = 1:20

surf(sin(2*pi*j/20)*Z, Z) M(j) = getframe;end% Play the movie twenty timesmovie(M,20)

Page 21: Barak Shenhav Early Evolution Course 11 Feb 2001 MATLAB Visualization, Functions & Debugging.

Saving Figures

From menu:

• File Save – MATLAB format

• File Export – Other graphics formats

From command line:

• Saveas(gcf, filename, extension)

• Print

Page 22: Barak Shenhav Early Evolution Course 11 Feb 2001 MATLAB Visualization, Functions & Debugging.

The Importance of Being Lognormal

c Lognormal

Normal

Page 23: Barak Shenhav Early Evolution Course 11 Feb 2001 MATLAB Visualization, Functions & Debugging.

Where Did Life Start ?

Life Started Here

Q=N/NG

Page 24: Barak Shenhav Early Evolution Course 11 Feb 2001 MATLAB Visualization, Functions & Debugging.

0 20 40 60 80 1000

20

40

60

80

100

time

time

0.15

0.30

0.45

0.60

0.75

0.90

Compositional Carpet

Page 25: Barak Shenhav Early Evolution Course 11 Feb 2001 MATLAB Visualization, Functions & Debugging.

Functionsfunction r = rank(A,tol)% RANK Matrix rank.% RANK(A) provides an estimate of the number of% linearly independent rows or columns of a% matrix A.% RANK(A,tol) is the number of singular values of % A that are larger than tol.% By default tol = max(size(A)) * norm(A) * eps.

s = svd(A);if nargin==1

tol = max(size(A)) * max(s) * eps;endr = sum(s > tol);

Page 26: Barak Shenhav Early Evolution Course 11 Feb 2001 MATLAB Visualization, Functions & Debugging.

MATLAB Components

Page 27: Barak Shenhav Early Evolution Course 11 Feb 2001 MATLAB Visualization, Functions & Debugging.

MATLAB Editor

Page 28: Barak Shenhav Early Evolution Course 11 Feb 2001 MATLAB Visualization, Functions & Debugging.

Editor Toolbar

Step in/over

Breakpoint

Evaluation

Quit

The function call stack

Page 29: Barak Shenhav Early Evolution Course 11 Feb 2001 MATLAB Visualization, Functions & Debugging.

Strings

s1 = 'Hello world 1'

s2 = ['Hello ' 'world' ' 2']

s3 = ['Hello world ' num2str(3)]

s4 = sprintf('Hello world %d', 4)

s5 = ['Hello' ; 'world' ; 'in' ; 'lines'] % ERROR

s5 = char({'Hello' ; 'world' ; 'in' ; 'lines'}) % OK

Page 30: Barak Shenhav Early Evolution Course 11 Feb 2001 MATLAB Visualization, Functions & Debugging.

Symbolic Math Toolbox-Funtool