PSY8219 : Week 6. Homework 5 Due Today. Homework 6 Due October 8. Readings for Today Attaway Chapter 6, 10, and 12

Similar documents
Mechanical Engineering Department Second Year (2015)

INTRODUCTION TO MATLAB PLOTTING WITH MATLAB

Lecture 6: Plotting in MATLAB

PROGRAMMING WITH MATLAB WEEK 6

Classes 7-8 (4 hours). Graphics in Matlab.

Graphics Example a final product:

Basic Graphs. Dmitry Adamskiy 16 November 2011

Introduction to MATLAB

Overview. Lecture 13: Graphics and Visualisation. Graphics & Visualisation 2D plotting. Graphics and visualisation of data in Matlab

FF505/FY505 Computational Science. MATLAB Graphics. Marco Chiarandini

INTRODUCTION TO MATLAB

Introduction to MATLAB

Lab of COMP 406 Introduction of Matlab (II) Graphics and Visualization

Prof. Manoochehr Shirzaei. RaTlab.asu.edu

Introduction to Matlab

Introduction to Matlab

2D LINE PLOTS... 1 The plot() Command... 1 Labeling and Annotating Figures... 5 The subplot() Command... 7 The polarplot() Command...

12 whereas if I terminate the expression with a semicolon, the printed output is suppressed.

Math Sciences Computing Center. University ofwashington. September, Fundamentals Making Plots Printing and Saving Graphs...

The Department of Engineering Science The University of Auckland Welcome to ENGGEN 131 Engineering Computation and Software Development

W1005 Intro to CS and Programming in MATLAB. Plo9ng & Visualiza?on. Fall 2014 Instructor: Ilia Vovsha. hgp://

Introduction to PartSim and Matlab

Scientific Functions Complex Numbers

GRAPHICS AND VISUALISATION WITH MATLAB

Spring 2010 Instructor: Michele Merler.

More on Plots. Dmitry Adamskiy 30 Nov 2011

Objectives. 1 Basic Calculations. 2 Matrix Algebra. Physical Sciences 12a Lab 0 Spring 2016

Dr Richard Greenaway

Basic plotting commands Types of plots Customizing plots graphically Specifying color Customizing plots programmatically Exporting figures

EOSC 473/573 Matlab Tutorial R. Pawlowicz with changes by M. Halverson

MATH 2221A Mathematics Laboratory II

APPM 2460 PLOTTING IN MATLAB

Logical Subscripting: This kind of subscripting can be done in one step by specifying the logical operation as the subscripting expression.

DATA PLOTTING WITH MATLAB

1 >> Lecture 4 2 >> 3 >> -- Graphics 4 >> Zheng-Liang Lu 184 / 243

Fondamenti di Informatica Examples: Plotting 2013/06/13

Introductory Scientific Computing with Python

Plotting x-y (2D) and x, y, z (3D) graphs

Objectives. 1 Running, and Interface Layout. 2 Toolboxes, Documentation and Tutorials. 3 Basic Calculations. PS 12a Laboratory 1 Spring 2014

Introduction to Programming in MATLAB

Introduction to MATLAB LAB 1

CSE 123. Plots in MATLAB

SGN Introduction to Matlab

Graphics and plotting techniques

Computing Fundamentals Plotting

Math 7 Elementary Linear Algebra PLOTS and ROTATIONS

Plotting - Practice session

PyPlot. The plotting library must be imported, and we will assume in these examples an import statement similar to those for numpy and math as

Basic MATLAB Intro III

CCNY. BME 2200: BME Biostatistics and Research Methods. Lecture 4: Graphing data with MATLAB

ENGG1811 Computing for Engineers Week 11 Part C Matlab: 2D and 3D plots

Introduction to MATLAB

What is MATLAB? It is a high-level programming language. for numerical computations for symbolic computations for scientific visualizations

TOPIC 6 Computer application for drawing 2D Graph

Finding, Starting and Using Matlab

Graphics in MATLAB. Responsible teacher: Anatoliy Malyarenko. November 10, Abstract. Basic Plotting Commands

MATLAB Guide to Fibonacci Numbers

Department of Chemical Engineering ChE-101: Approaches to Chemical Engineering Problem Solving MATLAB Tutorial Vb

Name: Math Analytic Geometry and Calculus III - Spring Matlab Project - due on Wednesday, March 30

MATLAB Tutorial. Digital Signal Processing. Course Details. Topics. MATLAB Environment. Introduction. Digital Signal Processing (DSP)

Matlab Examples. (v.01, Fall 2011, Ex prepared by HP Huang; Ex prepared by Noel Baker)

This module aims to introduce Precalculus high school students to the basic capabilities of Matlab by using functions. Matlab will be used in

PyPlot. The plotting library must be imported, and we will assume in these examples an import statement similar to those for numpy and math as

Dr. Iyad Jafar. Adapted from the publisher slides

Solving Simultaneous Nonlinear Equations. SELİS ÖNEL, PhD

Lab 6: Graphical Methods

Interactive Computing with Matlab. Gerald W. Recktenwald Department of Mechanical Engineering Portland State University

Computer Programming in MATLAB

PSY8219 : Week 2. Homework 1 Due Today. Homework 2 Due September 12. Readings for Today Attaway Chapters 2, 7, and 8

MATLAB Laboratory 09/23/10 Lecture. Chapters 5 and 9: Plotting

Lecturer: Keyvan Dehmamy

AMS 27L LAB #2 Winter 2009

Matlab programming, plotting and data handling

1 Introduction to Matlab

Introduction to MATLAB. Computational Probability and Statistics CIS 2033 Section 003

INTRODUCTORY NOTES ON MATLAB

Matlab Practice Sessions

QUICK INTRODUCTION TO MATLAB PART I

Lecture 3 for Math 398 Section 952: Graphics in Matlab

Introduction to MATLAB Step by Step Exercise

MATLAB Quick Reference

A very brief Matlab introduction

( you can also use "contourf" instead of "contour" for filled colors )

MatLab Programming Lesson 3

CDA6530: Performance Models of Computers and Networks. Chapter 4: Using Matlab for Performance Analysis and Simulation

Getting Started with MATLAB

Eng Marine Production Management. Introduction to Matlab

MATLAB PROGRAMMING LECTURES. By Sarah Hussein

Page 1 of 7 E7 Spring 2009 Midterm I SID: UNIVERSITY OF CALIFORNIA, BERKELEY Department of Civil and Environmental Engineering. Practice Midterm 01

Introduction in MATLAB (TSRT04)

Lab 5: Matlab Tutorial Due Sunday, May 8 at midnight

PHS3XXX Computational Physics

CDA5530: Performance Models of Computers and Networks. Chapter 8: Using Matlab for Performance Analysis and Simulation

Lab #1 Revision to MATLAB

MATLAB Functions and Graphics

ECE 201 Matlab Lesson #2 Basic Arithmetic and Plotting. Element-by-Element Arithmetic for Vectors and Matrices

GUI Alternatives. Syntax. Description. MATLAB Function Reference plot. 2-D line plot

A Quick Guide to MATLAB

Programming in Mathematics. Mili I. Shah

Getting Started. Chapter 1. How to Get Matlab. 1.1 Before We Begin Matlab to Accompany Lay s Linear Algebra Text

Transcription:

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);