Homework 5 Due Today PSY8219 : Week 6 Homework 6 Due October 8 Readings for Today Attaway Chapter 6, 10, and 12 Readings for Next Week Attaway Chapter 12 and 13
Turning in Homework Assignments Remember to include EVERYTHING needed to run your homework assignment (even if it includes files than I give you as part of the assignment). If you need to submit more than one file, please submit one ZIP file.
function x = testfun1(y, x) y = y + 1; x = x + y; end ---------------------------------------- x = 1; y = 2; z = testfun1(x, y); x + y + z
function x = testfun1(y, x) y = y + 1; x = x + y; end ---------------------------------------- x = 1; y = 2; testfun1(testfun1(x, y), x)
function y = testfun2(x) if x > 1 y = x + testfun2(x-1) else y = x; end ---------------------------------------- a = 3; b = testfun2(a); b
function y = testfun3(x) global zzz; zzz = x-1; y = zzz-x; end ---------------------------------------- global zzz; zzz = 5; x = 2; x = testfun3(x+zzz); x+zzz
function testfun3(a, b) clear all; end ---------------------------------------- clear all; a = 1; b = 2; testfun4(a, b) a+b
function testfun5(fun) x = 0:pi/50:10*pi; plot(x,fun(x)); end ---------------------------------------- fun = @sin; testfun5(fun); testfun5(@cos); testfun5(@testfun6); testfun5(@testfun7);
Create a function that computes x! (factorial)
Graphing in Matlab
A not uncommon data flow in research experiment data file Excel SPSS Excel raw data is organized, summarized, and reformatted in Excel by hand reformatted data is analyzed in SPSS or SAS using menu options summarized data with MSe read into Excel and graphs are created by hand
A not uncommon data flow in research experiment data file Excel SPSS Excel raw data is organized, summarized, and reformatted in Excel by hand reformatted data is analyzed in SPSS or SAS using menu options summarized data with MSe read into Excel and graphs are created by hand Advantages you understand your data Disadvantages possible mistakes, slow, no record of steps
Potentially more efficient approach experiment data file Matlab, Python, or R reorganization, reformatting, summary, analyses, graphs are automated
Potentially more efficient approach experiment data file Matlab, Python, or R reorganization, reformatting, summary, analyses, graphs are automated Advantages quick, avoid careless errors, record of steps Disadvantages can remove yourself from your data if you do not include descriptive analyses
Potentially more efficient approach experiment data file Matlab, Python, or R reorganization, reformatting, summary, analyses, graphs are automated You never want to go from raw data straight to statistical analyses without also doing descriptive analyses (histograms, graphs of data, etc.), but you can automate
we ll only cover a tiny fraction of all the graphing options available in Matlab http://www.mathworks.com/help/pdf_doc/matlab/graphg.pdf http://www.mathworks.com/help/releases/r2018a/matlab/graphics.html
x = 1:.01:5; y = sin(x); plot(x,y); x = [1 2 3 4 5]; y = [2 12 18 6 1]; bar(x,y); What is happening behind the scenes when plot() or bar() is called?
x = [1 2 3 4 5]; y = [2 12 18 6 1]; bar(x,y); is there a current figure window? no yes create a figure window draw graph in current figure window
x = [1 2 3 4 5]; y = [2 12 18 6 1]; bar(x,y); plot(x,y); is there a current figure window? no yes create a figure window overwrite graph in current figure window
x = [1 2 3 4 5]; y = [2 12 18 6 1]; hold on; bar(x,y); plot(x,y);
x = [1 2 3 4 5]; y = [2 12 18 6 1]; hold on; bar(x,y); plot(x,y); is there a current figure window? yes no create a figure window no is hold on? yes overwrite graph in current figure window add to graph in current figure window
x = [1 2 3 4 5]; y = [2 12 18 6 1]; hold on; bar(x,y); plot(x,y); hold off; bar(y,x); is there a current figure window? yes no create a figure window no is hold on? yes overwrite graph in current figure window add to graph in current figure window
figure; create a new figure window make it the current figure window close; close current figure window clf; clear current figure window
figure; create a new figure window make it the current figure window close all; close all figure window clf; clear current figure window
figure(n); does figure n exist? no create figure window n make figure n the current figure window yes make figure n the current figure window close(n); close figure window n clf(n); clear figure window n
What will this do? f1 = figure; f2 = figure; f3 = figure; plot(x,y); figure(f2); plot(x,exp(y)); figure(f3); plot(x,log(y));
What will this do? figure(1); figure(2); figure(3); plot(x,y); figure(2); plot(x,exp(y)); figure(3); plot(x,log(y));
What will this do? figure(1); figure(2); hold on; plot(x,y); figure(1); bar(x,y); figure(2); plot(x,x+y); figure(1); plot(x,2*y);
Formatting You need to realize the internally Matlab has a hierarchical representation of figures. root figure GUI objects axes plot objects other objects
handles fh = figure; x = 1:.01:5; y = sin(x); ph = plot(x,y);
Formatting figure windows figure(1) % create or use existing % figure with handle h=1 h = figure; % creates a brand new figure % returning its handle h h = figure('color', [1 1 0], % background 'Name', 'My Figure', % name window 'DockControls', 'off', % controls 'Menubar', 'none', % menu 'Visible', 'off'); % visible
Formatting figure windows h = figure('name', 'My Figure'); % name window common calling convention in Matlab and other languages calling by property/value pairs internally using the varargin parameter
h1 = figure; Formatting figure windows set(h1, 'Name', 'Test', 'Menubar', 'none'); You can format a figure window after you create it using set(), passing it the figures handle.
Formatting figure windows h = gcf; set(h, 'Name', 'Test', 'Menubar', 'none'); gcf gets the handle for the current figure.
Formatting Graphs Formatting when a graph is created. x = 0:pi/100:4*pi; y1 = sin(x); y2 = cos(x); y3 = tan(x); plot(x,y1,x,y2); plot(x,[y1 ; y2]); equivalent, except for some formatting hold on; plot(x,y1); plot(x,y2); multidimensional arrays too You can put multiple plots on the same graph.
Formatting Graphs Will this work? Why or why not? x = 0:pi/100:4*pi; y1 = sin(x); y2 = cos(x); plot(x,[y1 y2]);
Formatting Graphs Will this work? Why or why not? x = 0:pi/100:4*pi; y1 = sin(x); y2 = cos(x); plot(x,[y1 ; y2]);
Formatting Graphs What will this do? Will it work? What will it look like? x = [1 2 3]; y = [1 2 3 ; 4 4 4 ; 3 2 1]; plot(x,y,'o-');
Formatting Graphs What will this do? Will it work? What will it look like? x = [1 10 100]; y = [10 20 50]; plot(x,y,'loglog');
Formatting Graphs Format graph when created. Shorthand for line type. plot(x,y1,'-or'); Line Style Marker Color
Formatting Graphs Format graph when created. Shorthand for line type. plot(x,y1,'-or'); plot(x,y2,'b--x'); plot(x,-y1, -or',x,-y2,'-*b');
Formatting Graphs Format graph when created. Shorthand for line type. x = [1 2 3 4 5]; y1 = [1 2 2 3 1]; y2 = [5 1 2 3 4]; bar(x,y1,'red'); bar(x,y1,y2,'stacked');
Formatting Graphs Format graph when created. Shorthand for line type. x = [1 2 3 4 5]; y1 = [1 2 2 3 1]; y2 = [5 1 2 3 4]; What do you think this will do? bar(x,[y1 ; y2],'stacked');
Formatting Graphs Format graph when created. Shorthand for line type. x = [1 2 3 4 5]; y1 = [1 2 2 3 1]; y2 = [5 1 2 3 4]; What do you think this will do? bar(x,[y1 ; y2],'stacked'); Why do you need to do this? bar(x',[y1 ; y2]','stacked');
Formatting the Current Axes. x = 0:.1:10; y1 = x.*exp(-x); y2 = 1./x; Formatting Graphs plot(x,y1,'r',x,y2,'b'); axis([xmin xmax ymin ymax]); e.g., axis([0.5 10.5 0.0 1.1]); e.g., axis([min(x) max(x) min([y1 y2]) max([y1 y2])]); what is this doing?
Formatting the Current Axes. x = 0:.1:10; y1 = x.*exp(-x); y2 = 1./x; Formatting Graphs plot(x,y1,'r',x,y2,'b'); xlim([xmin xmax]); ylim([ymin ymax]);
plot(x,y1,'r',x,y2,'b'); axis([0.5 10.5 0.0 1.1]); title('this is a title'); xlabel('x axis label'); ylabel('y axis label'); Formatting Graphs
Getting the axes handle. Formatting Graphs h = plot(x,y1); or plot(x,y1); h = gca;
Getting the axes handle. h = plot(x,y1,'b-o'); get(h) Formatting Graphs w = get(h, 'LineWidth'); set(h, 'LineWidth', 3*w); set(h, 'Marker', 'x'); set(h, 'Color', [.5.3.1]);
Formatting Graphs Many graphics elements have handle that can be set. h = plot(x,y1,'b-o'); t = title('my Title ); get(t); set(t, 'FontName', 'Arial'); set(t, 'FontSize', 24); set(t, 'Color', [1 1 0]);
Some other formatting commands tl = legend('condition 1', 'condition 2', 'Location', 'NorthEast'); th = text(1.0, 1.5, 'This is a text string'); get(tl); % see what properties there are set(tl, Property Name, Property Value); % set properties
Some other formatting commands x = 0:.1:10; y = x.*exp(-x); ph = plot(x,y,'r ); get(ph);
see MoreOnHandle.m (in Week6.zip)
e.g., errorbar(x,y,e); plotyy(x,y1,x,y2); loglog(x,y); semilogx(x,y); semilogy(x,y); barh(x,y); rose(theta,x); polar(theta,r); stem(x,y); pie(x); Some various plots in Matlab
Subplots You can create multiple graphs within the same figure window. x = 1:pi/10:2*pi; subplot(2,2,1); plot(x,sin(x)); subplot(2,2,2); plot(x,tan(x)); subplot(2,2,3); plot(x,log(x)); subplot(2,2,4); plot(x,sin(x)/cos(x));
Saving graphs to a file Remember, you can create a graph without showing the figures on the screen, which takes time and resources. And you might be running it in the background. h = figure('visible', 'off');... print('-dpdf', '-r300', 'myfig'); type dpi fname http://www.mathworks.com/help/matlab/creating_plots/choosing-a-graphics-format.html
Saving graphs to a file Remember, you can create a graph without showing the figures on the screen, which takes time and resources. And you might be running it in the background. h = figure('visible', 'off');... print('-dtiff', '-r300', 'myfig'); type dpi fname http://www.mathworks.com/help/matlab/creating_plots/choosing-a-graphics-format.html
Saving graphs to a file Remember, you can create a graph without showing the figures on the screen, which takes time and resources. And you might be running it in the background. h = figure('visible', 'off');... print('-djpeg', '-r300', 'myfig'); type dpi fname http://www.mathworks.com/help/matlab/creating_plots/choosing-a-graphics-format.html
3D plots e.g., plot3(); bar3(); stem3(); pie3();
3D plots
3D plots
3D plots What is this? 1 $ exp& 2πσ 2 % 2 (x µ) 2σ 2 ' ) (
3D plots What do you think this is? 1 ( ) 2π Σ exp 1 2 (x µ)t Σ 1 (x µ) $ x ' x = & ) %& y () $ µ = µ ' & x ) & µ ) % y ( $ 2 σ x σ x σ y Σ = & & 2 σ x σ y σ %& y ' ) ) ()
3D plots multivariate normal distribution (pdf) x = [1 ; 2]; mu = [1 ; 1]; sig = [1 0; 0 1]; f1 = (1/(2*pi*det(sig))) *... exp(-(1/2)*(x-mu)'*inv(sig)*(x-mu)); - or - f2 = mvnpdf(x, mu, sig); how can we plot this multivariate pdf?
z = f(x,y) z y x
z = f(x,y) z y a grid of (x,y) points x
z = f(x,y) z calculate z=f(x,y) over the grid of (x,y) points y a grid of (x,y) points x
3D plots xinc = 1; yinc = 1; xlims = -3:xinc:4; ylims = -2:yinc:5; [X Y] = meshgrid(xlims, ylims); size(x) size(y) X Y
[X Y] = meshgrid(xlims, ylims); z y x
[X Y] = meshgrid(xlims, ylims); z y let s view this straight on x
[X Y] = meshgrid(xlims, ylims); y x
[X Y] = meshgrid(xlims, ylims); y -2-1 0 1 2 3 4-2 -1 0 1 2 3 4-2 -1 0 1 2 3 4-2 -1 0 1 2 3 4-2 -1 0 1 2 3 4-2 -1 0 1 2 3 4-2 -1 0 1 2 3 4 x
x y [X Y] = meshgrid(xlims, ylims); 2 3 4 5 6 7 8 2 3 4 5 6 7 8 2 3 4 5 6 7 8 2 3 4 5 6 7 8 2 3 4 5 6 7 8 2 3 4 5 6 7 8 2 3 4 5 6 7 8
3D plots [X Y] = meshgrid(xlims, ylims); How would we create Z, which has the same dimensions as X and Y?
3D plots [X Y] = meshgrid(xlims, ylims); How would we create Z, which has the same dimensions as X and Y? for i=1:size(x,1) for j=1:size(x:2) Z(i,j) = mvnpdf([x(i,j) Y(i,j)], mu, sig); end end
3D plots [X Y] = meshgrid(xlims, ylims); Matlab shortcut: Z = mvnpdf([x(:) Y(:)], mu', sig); Z = reshape(z, size(x,1), size(x,2));
3D plots Surface Plot surf(x, Y, Z);
3D plots Change Color Maps colormap(gray); surf(x, Y, Z); colormap(hot); surf(x, Y, Z);
3D plots Mesh Plot mesh(x, Y, Z);
3D plots Contour Plot contour(x, Y, Z);
Contour plus Surface Plot surfc(x, Y, Z); 3D plots Hmmmmm. You can t see the contours. Why?
3D plots Also this: ezsurfc('y/(1 + x^2 + y^2)',[-5,5,-2*pi,2*pi],35);
3D plots Compare pdf with random numbers drawn from distribution mu = [1 ; 1]; sig = [2.8;.8 1]; [X Y] = meshgrid(xlims, ylims); Z = mvnpdf([x(:) Y(:)], mu', sig); Z = reshape(z, size(x,1), size(x,2)); subplot(1,2,1); contour(x, Y, Z); pts = mvnrnd(mu, sig, 1000); subplot(1,2,2); plot(pts(:,1),pts(:,2),'*');
3D plots Some more formatting/graphing possibilities peaks(60) % built-in example function z = 3*(1-x).^2.* exp(-(x.^2) - (y+1).^2)... - 10*(x/5 - x.^3 - y.^5).* exp(-x.^2-y.^2)... - 1/3*exp(-(x+1).^2 - y.^2)
3D plots Some more formatting/graphing possibilities Z = peaks(60); subplot(1,3,1); surf(z); subplot(1,3,2); contour(z); subplot(1,3,3); contourf(z); The X and Y mesh grid are assumed to be the array indices if no X and Y matrices are passed to the plotting function.
3D plots Some more formatting/graphing possibilities contourf(peaks(60)); colormap(jet(8)); hcb = colorbar('yticklabel',... {'Freezing','Cold','Cool','Neutral',... 'Warm','Hot','Burning','Nuclear'}); set(hcb,'ytickmode','manual');
3D plots Some more formatting/graphing possibilities figure; contourf(peaks(60)); colormap(jet(8)); hcb = colorbar(... 'Location','SouthOutside','XTickLabel',... {'Freezing','Cold','Cool','Neutral',... 'Warm','Hot','Burning','Nuclear'}); set(hcb,'xtickmode','manual');
Viewing Arrays/Matrices imagine wanting to visualize an array or matrix A = [1 5 9 4; 2 2 1 2; 1 1 1 0]; A = peaks(60); A = mvnpdf([x(:) Y(:)], mu', sig); A = reshape(a, size(x,1), size(x,2)); In all three cases, A is just a matrix. bar3(a); contour(a); surf(a); imagesc(a);
images are just arrays Viewing Arrays/Matrices load penny.mat subplot(1,3,1); surf(p); subplot(1,3,2); contour(p); subplot(1,3,3); imagesc(p);