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

Similar documents
INTRODUCTION TO MATLAB PLOTTING WITH MATLAB

CSE 123. Plots in MATLAB

Mechanical Engineering Department Second Year (2015)

GRAPHICS AND VISUALISATION WITH MATLAB

An Introduction to MATLAB II

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

Introduction to Matlab

Lecture 6: Plotting in MATLAB

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

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

Introduction to Matlab

Graphics Example a final product:

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

UNIVERSITI TEKNIKAL MALAYSIA MELAKA FAKULTI KEJURUTERAAN ELEKTRONIK DAN KEJURUTERAAN KOMPUTER

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

FF505/FY505 Computational Science. MATLAB Graphics. Marco Chiarandini

PROGRAMMING WITH MATLAB WEEK 6

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

Basic Graphs. Dmitry Adamskiy 16 November 2011

3D plot of a surface in Matlab

Fondamenti di Informatica Examples: Plotting 2013/06/13

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

Lab #1 Revision to MATLAB

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

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

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://

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

Dr. Iyad Jafar. Adapted from the publisher slides

PERI INSTITUTE OF TECHNOLOGY DEPARTMENT OF ECE TWO DAYS NATIONAL LEVEL WORKSHOP ON COMMUNICATIONS & IMAGE PROCESSING "CIPM 2017" Matlab Fun - 2

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

Introduction to MATLAB

A very brief Matlab introduction

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

Introduction to MATLAB Step by Step Exercise

Eng Marine Production Management. Introduction to Matlab

MATH 2221A Mathematics Laboratory II

Introduction to MATLAB: Graphics

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

Introduction to MATLAB Practical 1

Matlab Practice Sessions

Introduction to MATLAB

Assignment :1. 1 Arithmetic Operations : Compute the following quantities ) -1. and compare with (1-

1 Introduction to Matlab

Computer Programming in MATLAB

MATLAB Functions and Graphics

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

The College of Staten Island

Introduction to Programming in MATLAB

Introduction to Matlab for Engineers

Introduction to Matlab

Introduction to Matlab

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

1. Register an account on: using your Oxford address

More on Plots. Dmitry Adamskiy 30 Nov 2011

Lab of COMP 406 Introduction of Matlab (III) Programming and Scripts

MATLAB Tutorial EE351M DSP. Created: Thursday Jan 25, 2007 Rayyan Jaber. Modified by: Kitaek Bae. Outline

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

TOPIC 6 Computer application for drawing 2D Graph

What is Matlab? A software environment for interactive numerical computations

Introduction to Matlab

EEE161 Applied Electromagnetics Laboratory 1

Introduction to GNU-Octave

PART 1 PROGRAMMING WITH MATHLAB

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

Introduction to MATLAB LAB 1

EE 301 Signals & Systems I MATLAB Tutorial with Questions

Plotting - Practice session

Matlab programming, plotting and data handling

MATLAB Guide to Fibonacci Numbers

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

Introduction to MATLAB programming: Fundamentals

INTRODUCTION TO NUMERICAL ANALYSIS

INTRODUCTION TO MATLAB, SIMULINK, AND THE COMMUNICATION TOOLBOX

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

ELEN E3084: Signals and Systems Lab Lab II: Introduction to Matlab (Part II) and Elementary Signals

Scientific Functions Complex Numbers

Computing Fundamentals Plotting

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

NatSciLab - Numerical Software Introduction to MATLAB

workspace list the variables and describe their matrix sizes 4x1 matrix (4 rows, 1 column) x=[3.4, 7, 2.2] 1x3 matrix (1 row, 3 columns)

! The MATLAB language

Introduction to Matlab

Lecture 3 for Math 398 Section 952: Graphics in Matlab

Spring 2010 Instructor: Michele Merler.

MATLAB PROGRAMMING LECTURES. By Sarah Hussein

Table of Contents. Basis CEMTool 7 Tutorial

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

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

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

STAT/MATH 395 A - PROBABILITY II UW Winter Quarter Matlab Tutorial

Lab of COMP 406. MATLAB: Quick Start. Lab tutor : Gene Yu Zhao Mailbox: or Lab 1: 11th Sep, 2013

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

Basic MATLAB Intro III

Finding, Starting and Using Matlab

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

Introduc)on to Matlab

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

Introduction to Octave/Matlab. Deployment of Telecommunication Infrastructures

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

Transcription:

Lab of COMP 406 Introduction of Matlab (II) Graphics and Visualization Teaching Assistant: Pei-Yuan Zhou Contact: cspyzhou@comp.polyu.edu.hk Lab 2: 19 Sep., 2014 1

Review Find the Matlab under the folder 1. Y:\Win32\Matlab\R2012a 2. Double click it and open Matlab Or open Matlab on your computer 1. Click 'Start' 2. Click 'Run' 3. Input 'nalwin32' 4. Find the Matlab under the folder /Network Application Packages/Statistical & Mathematical/Matlab 2

Review Matrix and Array >>a = [1 2 3 4] / a=[1,2,3,4] >> a = [1 2 3; 4 5 6; 7 8 10] >>z = zeros(5,1) >>a / inv(a) / a*a / a.*a / a.^3 / a^3 Calling functions >>c=sin(a) >>max(a) % a is a vector Save workspace variables >>save myfile.mat >>load myfile.mat 3

Outline -Graphics and Visualization 2-D Plots 3-D Plots Sub-Plots Line Plots of a Chirp, Bar Plot of a Bell Shaped Curve Stairstep Plot of a Sine Wave, Errorbar Plot Stem Plot Scatter Plot Mesh, Surface (Surfacel), Contour, Quiverm Slice 4

Outline 2-D Plots 3-D Plots Sub-Plots Line Plots of a Chirp, Bar Plot of a Bell Shaped Curve Stairstep Plot of a Sine Wave, Errorbar Plot Stem Plot Scatter Plot Mesh, Surface (Surfacel), Contour, Quiverm Slice 5

Line Plots To create two-dimensional line plots, use the plot function. For example, plot the value of the sine function from 0 to 2π: x = linspace(0,2*pi); % 100 points from 0 to 2pi y = sin (x); % calculate sin(x) plot(x,y); 6

Line Plots X=linspace(0,2*pi); % 100 points from 0 to 2pi plot(x,sin(x),x,cos(x),x,xin(x)+cos(x)); % multiple lines 7

Line Plots x = 0:pi/100:2*pi; To add plots to an existing figure, use hold on. Until you use hold off or close the window, all plots appear in the current figure window. % from 0 to 2pi, the interval between two points is pi/100 y = sin(x); plot(x,y); hold on y2 = cos(x); plot(x,y2,'r--') legend('sin','cos') xlabel('x') title('plot of the Sine and Cosine Function') 8

Line Plots You can label the axes and add a title. title legend label 9

Line Plots By adding a third input argument to the plot function, you can plot the same variables using a green star line. x = 0:0.05:7; y=log(x); plot(x,y,'g*') xlabel('x') ylabel( log(x)') title('plot of the log Function') 10

Line Plots of a Chirp x = 0:0.05:5; y = sin(x.^2); plot(x,y); xlabel('time') ylabel('amplitude') plot(sin((0:0.05:5).^2)). 11

Bar Plot of a Bell Shaped Curve x = -3:0.2:3; bar(x,exp(-x.*x)); 12

Stairstep Plot of a Sine Wave x = 0:0.25:10; stairs(x,sin(x)); 13

Errorbar Plot x = -2:0.1:2; y = erf(x); e = rand(size(x))/10; errorbar(x,y,e); The errorbar function draws a line plot of x and y values and superimposes on each observation a vertical error bar 14

x = 0:0.1:4; y = sin(x.^2).*exp(-x); stem(x,y) Stem Plot 15

Scatter Plot x = -10:0.5:10; y = x.^3; scatter(x,y,'bx'); ylabel('x-axis'); xlabel('y-axis'); doc line, doc scatter, doc line_props 16

Exercise Plot two functions, cos(x)+sin(x) and exp(-x) for variable x in one figure. The value of x is from 0 to pi, and the interval between two points is 0.1. Use line format --* for function 1, and --o for function 2. Provide the figure a title, legends of two functions, and Labels for x-axis and y-axis 17

Exercise - answer x=0:0.1:2*pi; y1=sin(x)+cos(x); y2=exp(-x); plot(x,y1,'--*',x,y2,'--o'); xlabel('time'); ylabel('value of sin(x)+cos(x) and exp(-x)'); title('function Plots of "sin(x)+cos(x)" and "exp(-x)"'); legend('sin(x)+cos(x)','exp(-x)'); 18

Outline 2-D Plots 3-D Plots Sub-Plots Line Plots of a Chirp, Bar Plot of a Bell Shaped Curve Stairstep Plot of a Sine Wave, Errorbar Plot Stem Plot Scatter Plot Mesh, Surface (Surfacel), Contour, Quiverm Slice 19

3-D Plots Three-dimensional plots typically display a surface defined by a function in two variables, z = f (x,y). To evaluate z, first create a set of (x,y) points over the domain of the function using meshgrid. Then, create a surface plot. [X,Y] = meshgrid(-2:.2:2); Z = X.* exp(-x.^2 - Y.^2); surf(x,y,z) / mesh(x,y,z) Both the surf function and its companion mesh display surfaces in three dimensions. surf displays both the connecting lines and the faces of the surface in color. mesh produces wireframe surfaces that color only the lines connecting the defining points. 20

Mesh Plot of Peaks z = peaks(25); mesh(z); colormap(hsv); peaks is a function to produce a 25-by-25 matrix obtained by translating and scaling Gaussian distributions, which is useful for demonstrating MESH, SURF, PCOLOR, CONTOUR, etc mesh(z) plot the colored parametrix mesh defined by matrix z. colormap is a function that sets the colormap property of a figure. See also hsv, caxis, spinmap, brighten, rgbplot, figure, colormapeditor. 21

Mesh Plot of Peaks z = peaks(25); mesh(z); colormap(hsv); 22

Surface Plot of Peaks z = peaks(25); surf(z); colormap(jet); 23

Surface plot with shading of peaks z = peaks(25); surfl(z); shading interp; colormap(pink); 24

Contour Plot of Peaks z = peaks(25); contour(z,16); colormap(hsv); contour(z) is a contour plot of matrix Z treating the values in Z as heights above a plane. * contour(z,n), draws N contour lines, overriding the automatic value. 25

Quiver x = -2:.2:2; y = -1:.2:1; [xx,yy] = meshgrid(x,y); % meshgrid replicates the grid vectors x and y to produce the coordinates of a rectangular grid (xx,yy) zz = xx.*exp(-xx.^2-yy.^2); [px,py] = gradient(zz); quiver(x,y,px,py,2); Plots velocity vectors as arrows with components (px,py) at the points (x,y). *And automatically scales the arrows to fit within the grid and then stretches them by 2. Use 0 to plot the arrows without the automatic scaling. 26

Outline 2-D Plots 3-D Plots Sub-Plots Line Plots of a Chirp, Bar Plot of a Bell Shaped Curve Stairstep Plot of a Sine Wave, Errorbar Plot Stem Plot Scatter Plot Mesh, Surface (Surfacel), Contour, Quiverm Slice 28

[X,Y] = meshgrid(-2:.2:2); Subplot Z = X.* exp(-x.^2 - Y.^2);%3D function You can display multiple plots in different subregions of the same window using the subplot function. subplot(2,2,1); mesh(x); title('x'); % the plot for x-axis subplot(2,2,2); mesh(y); title('y'); % the plot for y-axis subplot(2,2,3); mesh(z); title('z'); % the plot for z-axis subplot(2,2,4); mesh(x,y,z); title('x,y,z'); % the plot for the 3D function The first two inputs to the subplot function indicate the number of plots in each row and column. The third input specifies which plot is active. 29

Subplot 30

Exercise t = 0:pi/2:4*pi; [X,Y,Z] = cylinder(5*sin(t)); Plot this 3-D function into four subplots for each axis and the function. 31

Answer t = 0:pi/2:4*pi; [X,Y,Z] = cylinder(5*sin(t)); subplot(2,2,1); mesh(x); title('x'); subplot(2,2,2); mesh(y); title('y'); subplot(2,2,3); mesh(z); title('z'); subplot(2,2,4); mesh(x,y,z); title('x,y,z'); 32

Try this example Plot six functions in one window/figure, and give the title of them x=[0,2pi], y1=sin(x); y2=cos(x); y3=sin(x)+cos(x); y4=exp(x); y5=log(x); y6=x.^3 33

Answer x=0:pi/10:2*pi; y1=sin(x); y2=cos(x); y3=sin(x)+cos(x); y4=exp(-x); y5=log(x); y6=x.^(1/3); subplot(2,3,1); plot(y1); title('sin(x)'); subplot(2,3,2); plot(y2); title('cos(x)'); subplot(2,3,3); plot(y3); title('sin(x)+cos(x)'); subplot(2,3,4); plot(y4); title('exp(x)'); subplot(2,3,5); plot(y5); title('log(x)'); subplot(2,3,6); plot(y6); title('x^1^/^3'); 34

Ex 1 Please add variable A and B to Matlab workspace Create A and B. What s the transpose of A? What s the result of (A+B+10)/3? Concatenate A and B horizontally, then vertically. Subtract the 2nd row vector from A. Please clear A and B from Matlab workspace. 35

Ex 1 Please add variable A and B to Matlab workspace A=[1 2 3 4; 5 6 7 8; 11 12 13 14; 15 16 17 18]; B=A+4*j What s the transpose of A? A What s the result of (A+B+10)/3? (A + B + 10)/3 Concatenate A and B horizontally, then vertically. [A, B] [A;B] Subtract the 2nd row vector from A. A(2, :); Please clear A and B from Matlab workspace. clear 36

Ex 2 Make some simple plots: y = cos(x), x [0, 2π] Try to make it smoother by making the step smaller Label X-axis and Y-axis Add a title Add a legend 37

Ex 2 Make some simple plots: y = cos(x), x [0, 2π] x = 0:2*pi; y = cos(x); plot(x,y) Try to make it smoother by making the step smaller x = 0:0.1: 2*pi; y = cos(x); plot(x,y) Add x-axis label x-axis and y-axis label y-axis xlabel( x-axis ) ; ylabel( Y-axis ); Add a title title( The plot for function cos(x) ); Add a legend legend( cos(x) ); 38

Ex 2 39

Ex 3 Make a more complicated plot y = e -0.4x sin x, x [0,2π] Label the axes, create a nifty title and legend. Type help plot and use the information you get to make another plot of just the unconnected data points. 40

Ex 3 Make a more complicated plot: y = e -0.4x sin x, x [0,2π] x = 0:0.01:2*pi; y = exp(-0.4*x).*sin(x); plot(x,y) Label the axes, create a nifty title and legend. xlabel( x-axis ); ylabel( y-axis ) title( y=exp(-0.4*x)sin(x) ); legend( y=exp(-0.4*x)sin(x) ); Type help plot and use the information you get to make another plot of just the unconnected data points. plot(x, y, : ) / plot (x,y, -- ) 41

Ex 3 Make a 3-dimensional plot of the helix x = sin t, y = cos t, z = t. Hint: Create the vectors t, x, y, and z, then use the command plot3(x,y,z). t = 0:0.01:2*pi; x = sin(t); y = cos(t); z = t; plot3(x, y, z); grid on; 42

What We have Learned? 1. 2-D Plot 2. 3-D Plot 3. Sub-Plot 43

Contact: cspyzhou@comp.polyu.edu.hk Lab 2: 19 Sep., 2014 44