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

Similar documents
INTRODUCTION TO MATLAB PLOTTING WITH MATLAB

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

Basic Graphs. Dmitry Adamskiy 16 November 2011

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

Mechanical Engineering Department Second Year (2015)

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

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

PROGRAMMING WITH MATLAB WEEK 6

Dr. Iyad Jafar. Adapted from the publisher slides

Introduction to MATLAB

Prof. Manoochehr Shirzaei. RaTlab.asu.edu

Lecture 6: Plotting in MATLAB

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

Computing Fundamentals Plotting

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

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

FF505/FY505 Computational Science. MATLAB Graphics. Marco Chiarandini

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

Plotting - Practice session

INTRODUCTION TO MATLAB, SIMULINK, AND THE COMMUNICATION TOOLBOX

Introduction to graphics

Introduction to Matlab

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

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

UNIVERSITI TEKNIKAL MALAYSIA MELAKA FAKULTI KEJURUTERAAN ELEKTRONIK DAN KEJURUTERAAN KOMPUTER

Introduction to MATLAB Practical 1

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

INTRODUCTION TO MATLAB

Introduction to MatLab. Introduction to MatLab K. Craig 1

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

Plotting using Matlab. Vytautas Astromskas

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

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

Dr Richard Greenaway

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

MATLAB Guide to Fibonacci Numbers

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

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

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

Introduction to. The Help System. Variable and Memory Management. Matrices Generation. Interactive Calculations. Vectors and Matrices

MATH 2221A Mathematics Laboratory II

Introduction to MATLAB

Matlab Tutorial. The value assigned to a variable can be checked by simply typing in the variable name:

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

Programming in Mathematics. Mili I. Shah

AMS 27L LAB #2 Winter 2009

MATH (CRN 13695) Lab 1: Basics for Linear Algebra and Matlab

MATLAB Tutorial. Mohammad Motamed 1. August 28, generates a 3 3 matrix.

Introduction to MATLAB

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

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

Chapter 1 Introduction to MATLAB

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

Lecturer: Keyvan Dehmamy

Introduction to Unix and Matlab

The College of Staten Island

Lecture 3 for Math 398 Section 952: Graphics in Matlab

Table of Contents. Basis CEMTool 7 Tutorial

CSE 123. Plots in MATLAB

Getting Started with Matlab

A Guide to Using Some Basic MATLAB Functions

A very brief Matlab introduction

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

Part #6. A0B17MTB Matlab. Miloslav Čapek Filip Kozák, Viktor Adler, Pavel Valtr

FDP on Electronic Design Tools - Computing with MATLAB 13/12/2017. A hands-on training session on. Computing with MATLAB

LabVIEW MathScript Quick Reference

Some elements for Matlab programming

Introduction to the MATLAB SIMULINK Program

! The MATLAB language

GRAPHICS AND VISUALISATION WITH MATLAB

What is MATLAB? What is MATLAB? Programming Environment MATLAB PROGRAMMING. Stands for MATrix LABoratory. A programming environment

Graphics Example a final product:

3 An Introductory Demonstration Execute the following command to view a quick introduction to Matlab. >> intro (Use your mouse to position windows on

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

Chapter 2. MATLAB Fundamentals

Introduction to MATLAB programming: Fundamentals

Introduction to Engineering gii

Matlab Lecture 1 - Introduction to MATLAB. Five Parts of Matlab. Entering Matrices (2) - Method 1:Direct entry. Entering Matrices (1) - Magic Square

MATLAB Tutorial. 1. The MATLAB Windows. 2. The Command Windows. 3. Simple scalar or number operations

Getting Started with MATLAB

Eric W. Hansen. The basic data type is a matrix This is the basic paradigm for computation with MATLAB, and the key to its power. Here s an example:

Experiment 1: Introduction to MATLAB I. Introduction. 1.1 Objectives and Expectations: 1.2 What is MATLAB?

MATLAB Tutorial. Primary Author: Shoumik Chatterjee Secondary Author: Dr. Chuan Li

3.1 Sources of Data Function Data

Introduction to Matlab

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

Introduction to MATLAB

1 Introduction to Matlab

Finding, Starting and Using Matlab

Constraint-based Metabolic Reconstructions & Analysis H. Scott Hinton. Matlab Tutorial. Lesson: Matlab Tutorial

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

dgesv reference implementation of LAPACK MATLAB (\) CPU time (s) Matrix dimension

A Very Brief Introduction to Matlab

Introduction to MATLAB

To start using Matlab, you only need be concerned with the command window for now.

Laboratory 1 Octave Tutorial

PowerPoints organized by Dr. Michael R. Gustafson II, Duke University

Quick MATLAB Syntax Guide

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

3D plot of a surface in Matlab

Transcription:

Introduction to Plotting with Matlab Math Sciences Computing Center University ofwashington September, 1996 Contents Fundamentals........................................... 1 Making Plots........................................... 2 Printing and Saving Graphs................................... Loading Data Files........................................ Title and Labels... 6 Other Interesting Features of Matlab Plotting......................... 6 Matlab is a program for solving engineering and mathematical problems. The basic Matlab objects are vectors and matrices, so you must be familiar with these before making extensive use of this program. To start Matlab type matlab; to quit, type quit or exit. Fundamentals Matlab works with essentially one kind of object, a rectangular numerical matrix. Vectors and scalars are referred to as n-by-1 and 1-by-1 matrices respectively. Here is some basic information on using Matlab matrix commands. Entering Matrices The matrix A = 2 64 1 3 2 2 4 1 6 6 8 3 7 can be entered into Matlab by typing the following three lines. Each line ends by pressing the Return key. A = [1 3 2 2 4 1 6 6 8] 1

Generating Vectors With Even Space To plot a function, you must rst specify the data points at which the function will be evaluated. It is common to choose evenly spaced points and put then in a vector. Here is how you generate a row vector X containing the values from to 1 in increments of.2. X = :.2 : 1 Array Operations This term is used to refer to element-by-element arithmetic operations on vectors, instead of the usual linear algebra operations denoted by the symbols *, /, or^ (exponentiation). Preceding an operator with a period. indicates an array or element-by-element operation. For example, if X = [1 2 3] and Y = [4 6]; then X: Y = [4 1 18]. Notice that the usual vector product X Y is undened. The Matlab object ones(m,n) is useful if you want to add or subtract a constant from each element in a vector. ones(m,n) is an m-by-n matrix of ones. Using the vector X from the last example, you write the expression X + 2 as follows in Matlab notation. X + 2 * ones(1,3) The dimension of ones vector must match the other vectors in the computation. The command size(a) returns the dimension of a vector or matrix A. On-line Help Matlab has on-line help for all its commands. For example, try any of these commands: help print help help help general Making Plots Matlab provides a variety of functions for displaying data as 2-D or 3-D graphics. For 2-D graphics, the basic command is: plot(x1, y1, 'line style', x2, y2, 'line style'...) 2

This command plots vector x1 versus vector y1, vector x2 versus vector y2, etc. on the same graph. Other commands for 2-D graphics are: polar, bar, stairs, loglog, semilogx, and semilogy. For 3-D graphics, the most commonly used commands are: plot3(x1, y1, z1, 'line style', x2, y2, z2, 'line style'...) contour(x,y,z) mesh(x,y,z), surf(x,y,z) The rst statement is a three-dimensional analogue of plot() and plots lines and points in 3-D. The second statement produces contour plots of the matrix Z using vectors x and y to control the scaling on the x- and y- axes. For surface or mesh plots, you use the third statement where x, y are vectors or matrices and Z is a matrix. Other commands available for 3-D graphics are: pcolor, image, contour3, ll3, cylinder, and sphere. Example 1: Plot y 1 = sin(x) and y 2 = cos(x) with x in [; 2] on the same graph. Use a solid line for sin(x) and the symbol + for cos(x). The rst step is to dene a set of values for x at which the functions will be dened. x = :.1 : 2*pi; y1 = sin(x); y2 = cos(x); plot(x, y1, '-', x, y2, '+') Note: Ordinarily Matlab prints the results of each calculation right away. Placing ; at the end of each line directs Matlab to not print the values of each vector. 1.8.6.4.2 -.2 -.4 -.6 -.8-1 1 2 3 4 6 7 Example 1 Another way to get multiple plots on the same graph is to use the hold command to keep the current graph, while adding new plots. Another hold command releases the previous one. For example, the following statements generate the same graph as in Example 1. Matlab remembers that the vector x is already dened. 3

12 9 2 2 6 1 1 1 3 18 21 33 24 27 Example 2 3 plot(x, sin(x), '-') hold plot(x, cos(x), '+') The next example shows how Matlab generates a spiral using the polar coordinate system. Example 2: Plot = 2 with in polar coordinates. theta = :.2: *pi; rho = theta.^2; polar(theta, rho, '*') The following example illustrates how to generate a mesh surface in Matlab. Example 3: Plot z = sin(r)=r with r = p x 2 + y 2, 8 x 8, 8 y 8. The rst step in displaying a function of two variables, z = f(x; y), is to use the meshgrid function to generate X and Y matrices consisting of repeated rows and columns, respectively, over the domain of the function. The function can then be evaluated and graphed. x = -8:.: 8; y = x; [X,Y] = meshgrid(x,y); R = sqrt(x.^2 + Y.^2) + eps; % add eps to prevent R= Z = sin(r)./r; mesh(x, y, Z) % or mesh(x,y,z) Anything following % on a line is treated as a comment. We added eps (the machine ) torto prevent overow. 4

1.8.6.4.2 -.2 -.4 1 - - -1-1 Example 3 1 Printing and Saving Graphs There are two ways to print your plots. The rst one sends a copy ofyour graph directly to the default printer in the Thomson Hall lab. The second lets you save your graph in a le so you can use Unix printing commands to direct it to the printer of your choice. Type print in the Matlab environment to send your current plot to the pre-dened printer. On Math Sciences Matlab, the default printer is a laser printer in Thomson Hall. The print command generates a full page plot. If you want tosave graphs in a le, use another printer, change the plot orientation, or use other features of the print command, look at the on-line help text within Matlab. For example, to save your graph in a PostScript le, use the command: print -dps name-of-file Loading Data Files Matlab reads in values from ASCII les using the load command. Once the datale has been read in, you can use any ofthematlab graphing commands. Here are some of the things you need to consider when reading in data. The name of the ASCII datale must have two parts, separated by a period. The command load filename.extension reads the le lename.extension, which can be an ASCII le with a rectangular array of numeric data, arranged in m lines with n values in each line. The result is an m-by-n matrix with the same name as the le with the extension (including the period) stripped. Here are some examples:

1 Y = Sin(X).8.6.4.2 Y Critical point -.2 -.4 -.6 -.8-1 1 2 3 4 6 7 X Example 4 load f.m load y1 load func -ascii creates a Matlab variable named f loads from a le named y1.mat loads from a le named func Title and Labels You can add a title and labels for the axes with the commands; title, xlabel, ylabel and zlabel. You can also add contour labels to a contour plot by the command clabel. Other text can be added to the graph by using the text or gtext commands. With text, you specify a location where left edge of a text string is placed. With gtext, you position the text string with the mouse. Here is an example which adds titles and labels to the graph of f(x) =sin(x). Example 4: Plot y = sin(x); x 2, with appropriate labels. x = :.1: 2*pi; plot(x,sin(x)) title('y = Sin(X)') xlabel('x'); ylabel('y') hold plot(pi,,'*') text(pi +.1,, 'Critical point') hold % or gtext('critical point') Other Interesting Features of Matlab Plotting Matlab has a lot more capability for graphing or plotting than what has been mentioned here. What follows is a very brief description of three options (multiple graphs in one window, changing the viewpoint for 3-D plots, and controlling axes). Matlab also oers ways to turn a sequence of graphs into a movie, control almost every aspect of graphics objects, and create image plots. You 6

should read the Matlab User's Guide (or some other commercial documentation) for more information. Multiple Plots The command subplot(m,n,p) breaks the graph (or gure) window into an m-by-n matrix of small rectangular panes. The value of p is the pane for the next plot. Panes are numbered from left to right, top to bottom. To return to the default single graph per window, use either subplot(1,1,1) or clf. You can have more than one graphics window on an X display. The Matlab command, gure opens a new window, numbering each new window. You can then use commands such as clf, gure(h), or close to manipulate the gure windows. Viewpoint You can set the angle of view of a 3-D plot with the command: view(az,el) az is the azimuth and el is the elevation of the viewpoint, both in degrees. See the viewpoint gure for an illustration of azimuth and elevation relative to the Cartesian coordiate system. Viewpoint Z Y Elevation =3 X Azimuth =-37. Default Viewpoint Example : View the internal Matlab peaks matrix from 4 dierent viewpoints. The rst one, (view(-37.,3), is the default viewpoint. subplot(2,2,1); mesh(peaks(2)); view(-37.,3) subplot(2,2,2); mesh(peaks(2)); view(-7,8) subplot(2,2,3); mesh(peaks(2)); view(-9,) subplot(2,2,4); mesh(peaks(2)); view(-7,-1) 7

1 1-1 2 1-1 2 1 1 2 1 1 1 2 1 1 - -1 2 1 1 - Example -1 1 1 2 1 2 Controlling Axes You can control the scaling and appearance of plot axis with the axis function. To set scaling for the x- and y- axes on the current 2-D plot, use this command: axis([xmin xmax ymin ymax]) To scale the axes on 3-D plot, use this: axis([xmin xmax ymin ymax zmin zmax]) In addition, axis('auto') axis('square') axis('o') axis('on') returns the axis scaling to its default where the best axis limits are computed automatically; makes the current axis box square in size, otherwise a circle will look like anoval; turns o the axes turns on axis labeling and tic marks. 8