Mechanical Engineering Department Second Year (2015)

Similar documents
MATLAB PROGRAMMING LECTURES. By Sarah Hussein

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

INTRODUCTION TO MATLAB PLOTTING WITH MATLAB

PROGRAMMING WITH MATLAB WEEK 6

Basic Graphs. Dmitry Adamskiy 16 November 2011

Computer Programming in MATLAB

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

Additional Plot Types and Plot Formatting

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

Introduction to MATLAB: Graphics

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

MATLAB PROGRAMMING LECTURES

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

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

PART 1 PROGRAMMING WITH MATHLAB

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

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

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

MATLAB Introduction to MATLAB Programming

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

1 Introduction to Matlab

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

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

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

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

Chapter 3: Introduction to MATLAB Programming (4 th ed.)

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

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

Graphics Example a final product:

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

CSE 123. Plots in MATLAB

1. Register an account on: using your Oxford address

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

QUICK INTRODUCTION TO MATLAB PART I

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

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

Chapter 2. MATLAB Fundamentals

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

MATLAB Guide to Fibonacci Numbers

Lab #1 Revision to MATLAB

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

Dr. Iyad Jafar. Adapted from the publisher slides

MATLAB SUMMARY FOR MATH2070/2970

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

Graphics and plotting techniques

INTRODUCTION TO NUMERICAL ANALYSIS

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

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

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

MATH 2221A Mathematics Laboratory II

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

UNIVERSITI TEKNIKAL MALAYSIA MELAKA FAKULTI KEJURUTERAAN ELEKTRONIK DAN KEJURUTERAAN KOMPUTER

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

Matlab Tutorial and Exercises for COMP61021

Matlab Tutorial for COMP24111 (includes exercise 1)

TOPIC 6 Computer application for drawing 2D Graph

Lecture 6: Plotting in MATLAB

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

Introduction to Matlab

Introduc)on to Matlab

Computing Fundamentals Plotting

Math 375 Natalia Vladimirova (many ideas, examples, and excersises are borrowed from Profs. Monika Nitsche, Richard Allen, and Stephen Lau)

Dr Richard Greenaway

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

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

Introduction to Matlab

Programming 1. Script files. help cd Example:

Prof. Manoochehr Shirzaei. RaTlab.asu.edu

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

Plotting - Practice session

You can change the line style by adding some information in the plot command within single quotation marks.

Fondamenti di Informatica Examples: Plotting 2013/06/13

Introduction to Scientific Programming in MATLAB

Introduction to Matlab. WIAA Technical Workshop #2 10/20/2015

Introduction to MATLAB Practical 1

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

MATLAB Functions and Graphics

More on Plots. Dmitry Adamskiy 30 Nov 2011

AMS 27L LAB #2 Winter 2009

A very brief Matlab introduction

What is Matlab? A software environment for interactive numerical computations

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

FF505/FY505 Computational Science. MATLAB Graphics. Marco Chiarandini

Introduction to Matlab

Purpose of the lecture MATLAB MATLAB

By Edward Grossman Edited and Updated by Mark Turner

GRAPHICS AND VISUALISATION WITH MATLAB

Introduction to Matlab

Introduction to Matlab

Introduction to MATLAB LAB 1

Chapter 2 (Part 2) MATLAB Basics. dr.dcd.h CS 101 /SJC 5th Edition 1

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

Introduction to MATLAB

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

NatSciLab - Numerical Software Introduction to MATLAB

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

Introduction to GNU-Octave

MATLAB. Input/Output. CS101 lec

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

An Introduction to MATLAB II

Transcription:

Lecture 7: Graphs Basic Plotting MATLAB has extensive facilities for displaying vectors and matrices as graphs, as well as annotating and printing these graphs. This section describes a few of the most important graphics functions and provides examples of some typical applications. Graphs (in 2-D) are drawn with the plot statements: plot(y): plots the columns of Y versus the index of each value. For example, plot 20 random numbers against the integers 1 20. Ex1: >> plot(rand(1, 20)) Ex2: plot(x,y) : plots vector Y versus vector X. x = 0:1:10; [Type text] Page 47

y = x.^2-10.*x + 15; plot(x,y) Titles and axis labels can be added to a plot with the title, xlabel, and ylabel functions. Each function is called with a string containing the title or label to be applied to the plot. Grid lines can be added or removed from the plot with the grid command: grid on turns on grid lines, and grid off turns off grid lines. For example, the following statements generate a plot of the function with titles, labels, and gridlines. Ex3: x = 0:1:10; y = x.^2-10.*x + 15; plot(x,y); title ('Plot of y = x.^2-10.*x + 15'); xlabel ('x'); ylabel ('y'); [Type text] Page 48

grid on; 7.1.1 Line Color, Line Style and Marker Style MATLAB allows an engineer to select the color of a line to be plotted, the style of the line to be plotted, and the type of marker to be used for data points on the line. These traits may be selected using an attribute character string after the x and y vectors in the plot function. Plot(x,y, abc ) when a=line color, b= symbol of data points and c= line type. The possible colors are: b blue c cyan g green k black m magenta r red w white y yellow [Type text] Page 49

Either the single character listed above or the full name of the color can be used in the string to specify the color. The plot symbols, or markers, that can be used are: o d h p circle + plus diamond. point s hexagram pentagram square * star v down triangle < left triangle > right triangle ^ x up triangle x-mark Line types can also be specified by the following: Ex4: -- dashed -. dash dot : dotted - solid If no line type is specified, a solid line is drawn between the points. Plot of the function y=x 2-10x+15, 0<=x<=10 with a dashed red line and show the actual data points as blue circles. x = 0:1:10; [Type text] Page 50

y = x.^2-10.*x + 15; plot(x,y,'r--',x,y,'bo') 7.1.2 Simple Related Plot Functions Other functions that are useful in customizing plots include clf, figure, hold and legend. clf : clears the Figure Window by removing everything from it. figure: creates a new, empty Figure Window when called without any arguments. Calling it as figure(n) where n is an integer is a way of creating and maintaining multiple Figure Windows, and of referring to each individually. legend: displays strings passed to it in a legend box in the Figure Window in order of the plots in the Figure Window. Ex5: Write a script to create 2 different plots, in 2 different Figure Windows where X=1 2 3 4 5 Y1=2 11 6 9 3 Y2= 4 5 8 6 2 [Type text] Page 51

Sol: clf x = 1:5; y1 = [2 11 6 9 3]; y2 = [4 5 8 6 2]; figure(1); bar(x,y1); figure(2); plot(x,y2) grid on legend('y2') Running this script will produce two separate Figure Windows. [Type text] Page 52

7.1.3 Multiple Plots: 7.1.3.1 Multiple Plots on the Same Axes plot(x1,y1,...,xn,yn): plot multiple functions on the same graph by simply including more than one set of (x,y) values in the plot function. For example, suppose that we wanted to plot the function and its derivative on the same plot. The derivative of is To plot both functions on the same axes, we must generate a set of x values and the corresponding y values for each function. Then to plot the functions, we would simply list both sets of (x, y) values in the plot function as follows. Ex6: x = 0:pi/100:2*pi; y1 = sin(2*x); y2 = 2*cos(2*x); plot(x,y1,x,y2); [Type text] Page 53

7.1.3.2 Multiple plots in a figure: subplot subplot(m, n, p), which creates a matrix of plots in the current Figure Window, it divides the figure window into m n small sets of axes, and selects the pth set for the current plot (numbered by row from the left of the top row). Ex7: x = 0:pi/100:2*pi; subplot(2,2,1); plot(x,sin(x),'r-') subplot(2,2,2); plot(x,sin(x),'g+') subplot(2,2,3); plot(x,sin(x),'b-o') subplot(2,2,4); plot(x,sin(x),'k:') [Type text] Page 54

7.2 3D Graph The meshgrid function can specify the x and y coordinates of points in images, or can be used to calculate functions on two variables x and y. It receives as input arguments two vectors, and returns as output arguments two matrices that specify separately x and y values. For example, the x and y coordinates of a 2 x 3 image would be specified by the coordinates: (1,1) (2,1) (3,1) (1,2) (2,2) (3,2) The matrices that separately specify the coordinates are created by the meshgrid function, where x iterates from 1 to 3 and y iterates from 1 to 2: >> [x y] = meshgrid(1:3,1:2) x = 1 2 3 1 2 3 y = 1 1 1 2 2 2 As another example, to evaluate a function f of two variables x and y: f(x,y) = 2*x + y, where x ranges from 1 to 4 and y ranges from 1 to 3. We can accomplish this by creating x and y matrices using meshgrid, and then the expression to calculate f uses scalar multiplication and array addition. >> [x y] = meshgrid(1:4,1:3) x = [Type text] Page 55

Ex8: 1 2 3 4 1 2 3 4 1 2 3 4 y = 1 1 1 1 2 2 2 2 3 3 3 3 >> f = 2*x + y f = 3 5 7 9 4 6 8 10 5 7 9 11 For 3-D graphics, the most commonly used commands are: plot3(x1, y1, z1) mesh(x,y,z) surf(x,y,z) Plot x,y,z where, x=1,2,3,4,5, y=0-2 4 11 3, z= 2 4 6 8 10 x = 1:5; y = [0-2 4 11 3]; z = 2:2:10; plot3(x,y,z); grid; xlabel('x'); ylabel('y'); [Type text] Page 56

zlabel('z'); title('3d Plot') Ex9: Plot st,ct,t where st=sin(t), ct=cos(t), 0>=t <= 10π with π/50 increment t = 0:pi/50:10*pi; st = sin(t); ct = cos(t); plot3(st,ct,t) Ex10: Plot z= sin( r )/ r, where r=, -8>= x>=8, -8 >= y >= 8 with 0.5 increment. By using mesh: x = -8:.5: 8; y = x; [x y]=meshgrid(x,y); R = sqrt(x.^2 + y.^2) ; Z = sin(r)./r; mesh(x, y, Z) [Type text] Page 57

Ex11: By using surf: x = -8:.5: 8; y = x; [x y]=meshgrid(x,y); R = sqrt(x.^2 + y.^2) ; Z = sin(r)./r; surf(x, y, Z) Ex12: t= linspace (0, 10 * pi, 100); plot3(sin(t),cos(t),t); xlabel('sin(t)'); ylabel('cos(t)'); zlabel('t'); text(0,0,0,'origin') grid on [Type text] Page 58