Dr. Iyad Jafar. Adapted from the publisher slides

Similar documents
FF505/FY505 Computational Science. MATLAB Graphics. Marco Chiarandini

Lecture 6: Plotting in MATLAB

INTRODUCTION TO MATLAB PLOTTING WITH MATLAB

PROGRAMMING WITH MATLAB WEEK 6

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

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

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

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

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

INC151 Electrical Engineering Software Practice. MATLAB Graphics. Dr.Wanchak Lenwari :Control System and Instrumentation Engineering, KMUTT 1

Mechanical Engineering Department Second Year (2015)

Prof. Manoochehr Shirzaei. RaTlab.asu.edu

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

Introduction to MATLAB: Graphics

Basic Graphs. Dmitry Adamskiy 16 November 2011

Dr Richard Greenaway

Graphics Example a final product:

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

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

INTERNATIONAL EDITION. MATLAB for Engineers. Third Edition. Holly Moore

EGR 102 Introduction to Engineering Modeling. Lab 05B Plotting

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

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

UNIVERSITI TEKNIKAL MALAYSIA MELAKA FAKULTI KEJURUTERAAN ELEKTRONIK DAN KEJURUTERAAN KOMPUTER

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

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

Matlab Tutorial 1: Working with variables, arrays, and plotting

CSE 123. Plots in MATLAB

Plotting - Practice session

Graphics and plotting techniques

Introduction to Matlab

Computing Fundamentals Plotting

Introduction to MATLAB

Introduction to MATLAB

Introduction to MATLAB

ECE 202 LAB 3 ADVANCED MATLAB

INTRODUCTION TO MATLAB

PC-MATLAB PRIMER. This is intended as a guided tour through PCMATLAB. Type as you go and watch what happens.

Armstrong Atlantic State University Engineering Studies MATLAB Marina 2D Plotting Primer

Additional Plot Types and Plot Formatting

fplot Syntax Description Examples Plot Symbolic Expression Plot symbolic expression or function fplot(f) fplot(f,[xmin xmax])

PART 1 PROGRAMMING WITH MATHLAB

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

DATA PLOTTING WITH MATLAB

INTRODUCTION TO MATLAB, SIMULINK, AND THE COMMUNICATION TOOLBOX

MATH 2221A Mathematics Laboratory II

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

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

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

BioFuel Graphing instructions using Microsoft Excel 2003 (Microsoft Excel 2007 instructions start on page mei-7)

Total Number of Students in US (millions)

Introduction to the MATLAB SIMULINK Program

Lecture 3 for Math 398 Section 952: Graphics in Matlab

QUICK INTRODUCTION TO MATLAB PART I

GRAPHICS AND VISUALISATION WITH MATLAB

Lab 6: Graphical Methods

DSP Laboratory (EELE 4110) Lab#1 Introduction to Matlab

STAT 391 Handout 1 Making Plots with Matlab Mar 26, 2006

What is Matlab? A software environment for interactive numerical computations

Computer Programming in MATLAB

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

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

1 Introduction to Matlab

Introduction to Simulink

Lecturer: Keyvan Dehmamy

TOPIC 6 Computer application for drawing 2D Graph

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

Introduction to MATLAB LAB 1

Contents. Implementing the QR factorization The algebraic eigenvalue problem. Applied Linear Algebra in Geoscience Using MATLAB

MAT 275 Laboratory 1 Introduction to MATLAB

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

LabVIEW MathScript Quick Reference

Introduction to Matlab

UNIVERSITI TEKNIKAL MALAYSIA MELAKA FAKULTI KEJURUTERAAN ELEKTRONIK DAN KEJURUTERAAN KOMPUTER

MATLAB Guide to Fibonacci Numbers

MATLAB Fundamentals. Berlin Chen Department of Computer Science & Information Engineering National Taiwan Normal University

Getting Started with MATLAB

Purpose of the lecture MATLAB MATLAB

Chapter 8 Complex Numbers & 3-D Plots

ENGR Fall Exam 1 PRACTICE EXAM

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

Introduction to Matlab to Accompany Linear Algebra. Douglas Hundley Department of Mathematics and Statistics Whitman College

DataPlotClasses for REALBasic

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

Technology Assignment: Scatter Plots

Chapter 2 Surfer Tutorial

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

A Summary of the Types of Graphs Compiled by Mr. A. Caruso

Appendix A. Introduction to MATLAB. A.1 What Is MATLAB?

The College of Staten Island

Chemistry Excel. Microsoft 2007

Introduction to Programming in MATLAB

Creating a Basic Chart in Excel 2007

KaleidaGraph Quick Start Guide

ENV Laboratory 2: Graphing

Chapter 2. MATLAB Fundamentals

Points Lines Connected points X-Y Scatter. X-Y Matrix Star Plot Histogram Box Plot. Bar Group Bar Stacked H-Bar Grouped H-Bar Stacked

A very brief Matlab introduction

MATLAB Functions and Graphics

Introduction to Matlab. By: Dr. Maher O. EL-Ghossain

Transcription:

Computer Applications Lab Lab 6 Plotting Chapter 5 Sections 1,2,3,8 Dr. Iyad Jafar Adapted from the publisher slides

Outline xy Plotting Functions Subplots Special Plot Types Three-Dimensional Plotting 2

The most common types of plots. Plots the function y = f(x). (other symbols can be used also) Anatomy of a plot 3

The command that is used for xy plots in MATLAB is plot(x,y) where both x and y are vectors of the same length. Example The following MATLAB session plots y = 0.4 1.8x for 0 x 52, where y represents the height of a rocket after launch, in miles, and x is the horizontal (downrange) distance in miles. >>x = [0:0.1:52]; >>y = 0.4*sqrt(1.8*x); >>plot(x,y) >>xlable( Distance(miles) ) % Adds label to x-axis >>ylable( Height(miles) ) % Adds label to y-axis >>title( Rocket Height as a Function of Downrange Distance ) % Adds title to the plot. 4

5

Requirements for a Correct Plot 1. Each axis must be labeled with the name of the quantity being plotted and its units! 2. Eachaxis should have regularly spaced tick marks at convenient intervals not too sparse, but not too dense with a spacing that is easy to interpret and interpolate. For example, use 0.1, 0.2, and so on, rather than 0.13, 0.26, and so on. 3. If you are plotting more than one curve or data set, label each on its plot or use a legendto distinguish them. 4. If you are preparing multiple plots of a similar type or if the axes labels cannot convey enough information, use a title. 6

Requirements for a Correct Plot 5. If you are plotting measured data, plot each data point with a symbol such as a circle, square, or cross (use the same symbol for every point in the same data set). If there are many data points, plot them using the dot symbol. 6. Sometimes data symbols are connected by lines to help the viewer visualize the data, especially if there are few data points. However, connecting the data points, especially with a solid line, might be interpreted to imply knowledge of what occurs between the data points. Thus you should be careful to prevent such misinterpretation. 7. If you are plotting points generated by evaluating a function (as opposed to measured data), do not use a symbol to plot the points. Instead, be sure to generate many points, and connect the points with solid lines. 7

The Figure Window It has different menus to save, export, and edit generated figures. For example, we can save the figure to be used later on (it has.figextension). print the plot we can insert text boxes, arrows, shapes, and legends. zoom in and out and rotate plot clear the figure copy the plot to be inserted in other documents export the figure in other data formats using File>Export Setup 8

xy Plotting grid command More Commands The grid command displays gridlines at the tick marks corresponding to the tick labels. Type grid on to add gridlines; type grid off to stop plotting gridlines. When used by itself, grid toggles this feature on or off, but you might want to use grid on and grid off to be sure. axis command Is used to override the MATLAB selections for the axis limits. The basic syntax is axis([xmin xmax ymin ymax]). This command sets the scaling for the x-and y-axes to the minimum and maximum values indicated. Note that, unlike an array, this command does not use commas to separate the values. figure command Use this command to open a new figure window to be used for new plots. Using the plot command more than once update the same figure window that was used by the first plot command, unless the hold command is used 9

10

Some Variations of the plot Command If y is a vector and we use plot(y), MATLAB plots the elements of y versus their indices. If y is a vector of complex numbers, the use of plot(y)plots the imaginary part of the elements versus the real part. In other words, plot(y) in case y contains complex values is equivalent to plot(real(y),imag(x)). Example >> z = 0.1 + 0.9i; >> n = [0:0.01:10]; >> plot(z.^n), xlabel( Real ),ylabel( imaginary ); 11

The plot command with complex numbers 12

The function plot command fplot It is a smart command that takes the function to be plotted and determine the required number of points to show most of the features of the function Syntax fplot( string,[xmin xmax ymin ymax]) [x,y] = fplot( string,[xmin xmax ymin ymax]) Example compare the result of using fplot( cos(tan(x)) tan(sin(x)),[1 2]) with >> x = [1:0.01:2] ; >> y = cos(tan(x)) tan(sin(x)); >> plot(x,y) 13

Using fplot command 14

Using plot command 15

Plotting Polynomials The polyval(coeff,x)function in MATLAB can be used to evaluate a polynomial whose coefficients are given in coeff evaluated at values specified by x Example >> x = [-6:0.01:6]; >> y = [3,2,-100,2,-7,90]; >> plot(x,polyval(y,x)) >> xlable( x) >> ylable( p ) p 5000 4000 3000 2000 1000 0-1000 -2000-3000 -6-4 -2 0 2 4 6 x 16

Subplots You can use the subplotcommand to obtain several smaller subplots in the same figure. Syntax subplot(m,n,p) This command divides the Figure window into an array of rectangular panes with mrows and ncolumns. The variable ptells MATLAB to place the output of the plot command following the subplot command into the p th pane. For example, subplot(3,2,5)creates an array of six panes, three panes deep and two panes across, and directs the next plot to appear in the fifth pane (in the bottom-left corner). 17

Example Subplots x = [0:0.01:5]; y = exp(-1.2*x).*sin(10*x+5); subplot(1,2,1) plot(x,y),axis([0 5-1 1]) x = [-6:0.01:6]; y = abs(x.^3-100); subplot(1,2,2) plot(x,y),axis([-6 6 0 350]) 18

- Example 19

Data Markers and Line Types We can change the data markers, line styles, and colors for our plots by using a string in a plot command For example, plot(x,y, b-.o ) plots y versus x using a blue dash-dotted line with circles as data markers. More data markers, line styles, and colors are listed below 20

Controlling Tick-Mark Spacing and Labels The set command is a powerful command for controlling objects One of these objects is the axes in the plot. We can change the tick mark spacing and use labels instead of numbers as tick marks. Syntax Set(gca, XTick,[xmin:dx:xmax], YTick,[ymin:dy:ymax]) gca is a function that returns a handle for the current axes Example set(gca, XTick,[0:0.1:2], YTick,[0:0,1:1]) 21

Controlling Tick-Mark Spacing and Labels We can assign labels to tick marks instead of numbers. Example >> x = [1:6]; >> y = [13,5,7,14,10,12] >> set(gca, Xticklabel,[ Jan ; Feb ; Mar ;Apr ; May ; Jun ]) >> set(gca, XTick,[1:6]), axis([1 6 0 15]), xlabel( Month ),ylabel( Monthly Sales $1000 ), title( Printer Sales for January to June, 1997 ) 22

Labeling Curves and Data When we plot more than one curve on the same plot, we have to distinguish them with Legends legend( string1, string2, ) By a label next to each curve gtext( string ) (it requires user interaction to specify the location of the text by the mouse) or use text(x,y, string ) where x and y specify the location of the text Example 23

24

The hold Command We can plot yversus xand v versus uon the same figure using the plot command by plot(x,y,u,v) If we use the plot command once more, the figure gets updated with the new plot and the old plot is erased. To preserve earlier plots, use the hold on command. This command will allow the new plot to be superimposed over the old plot. Use hold off to disable the hold command. 25

Annotating Plots We already saw the title( string ) that is used to insert a title for the plot. We can insert mathematical symbols in the title, axes labels, and text labels by using the backslash character \ followed by the symbol name. Superscripts and subscripts are denoted by ^and _, respectively. To set multiple characters as superscripts or subscripts, enclose them by {}. Example : the command >> title( Ae^{-t/\tau} sin(\omega t) ) produces in the figure title. t / τ A e sin( ω t ) 26

Hints for Improving Plots 1. Start scales from zero whenever possible. This technique prevents a false impression of the magnitudes of any variations shown on the plot. 2. Use sensible tick-mark spacing. If the quantities are months, choose a spacing of 12 because 1/10 of a year is not a convenient division. Space tick marks as close as is useful, but no closer. If the data is given monthly over a range of 24 months, 48 tick marks might be too dense, and also unnecessary. 3. Minimize the number of zeros in the data being plotted. For example, use a scale in millions of dollars when appropriate, instead of a scale in dollars with six zeros after every number 4. Determine the minimum and maximum data values for each axis before plotting the data. Then set the axis limits to cover the entire data range plus an additional amount to allow convenient tick-mark spacing to be selected. 27

Hints for Improving Plots 5. Use a different line type for each curve when several are plotted on a single plot and they cross each other; for example, use a solid line, a dashed line, and combinations of lines and symbols. Beware of using colors to distinguish plots if you are going to make black-and-white printouts and photocopies. 6. Do not put many curves on one plot, particularly if they will be close to each other or cross one another at several points. 7. Use the same scale limits and tick spacing on each plot if you need to compare information on more than one plot 28

Special Types of Plots 10 10 9 9 8 8 7 7 6 6 y 5 y 5 4 4 3 3 2 2 1 1 0 0 1 2 3 4 5 6 7 8 9 10 x Stem Plots: use stem(x,y) 1 4 5 8 9 x Bar Plots: use bar(x,y) Exercise: use the help browser to check the stairs command. 0 29

3-D Plotting Plotting of Curves Use the command plot3(x,y,z) to plot threedimensional curves. We can use the grid command and the attributes for selecting the line style, color and data markers. Example 30

3-D Plotting Surface Mesh Plots The function z = f(x,y) represents a surface when plotted on xyz axes, and the mesh function provides the means to generate a surface plot. Before you can use this function, you must generate a grid of points on the xyplane, and then evaluate the function f(x,y) at these points. The meshgridfunction generates the grid. 31

3-D Plotting Syntax Surface Mesh Plots [X,Y] = meshgrid(x,y) Where x = [xmin:xspacing:xmax] and y = [ymin:yspacing:ymax], This computes the Cartesian product between x and y. The function [X,Y] = meshgrid(x) is the equivalent to [X,Y] = meshgrid(x,x) and can be used if x and y have the same minimum values, the same maximum values, and the same spacing. Using this form, you can type [X,Y] = meshgrid(min:spacing:max), where min and max specify the minimum and maximum values of both x and y and spacing is the desired spacing of the x and y values. Once the grid is obtained, we can plot the three dimensional surface using the mesh(x,y,z) function. As always, the grid, label, and text functions can be used with the mesh function. 32

3-D Plotting Example Surface Mesh Plots 33

3-D Plotting Contour Plots Contour plot contain lines that indicate constant elevation (values). Contour plots can be generated in MATLAB by the contour(x,y,z) command. We need first to use the meshgrid. 34

3-D Plotting 35

3-D Plotting Examples mesh(x,y,z) meshc(x,y,z) meshz(x,y,z) waterfall(x,y,z) 36