+ All Categories
Home > Documents > Introduction to Matlab 332:202 Discrete Mathematics Spring 2007.

Introduction to Matlab 332:202 Discrete Mathematics Spring 2007.

Date post: 22-Dec-2015
Category:
View: 230 times
Download: 0 times
Share this document with a friend
Popular Tags:
38
Introduction to Matlab 332:202 Discrete Mathematics Spring 2007
Transcript
Page 1: Introduction to Matlab 332:202 Discrete Mathematics Spring 2007.

Introduction to Matlab

332:202 Discrete Mathematics

Spring 2007

Page 2: Introduction to Matlab 332:202 Discrete Mathematics Spring 2007.

Contents

• What is Matlab

• Matlab basic

• Advanced usage of Matlab

- plots

- loops

- working with m files

- advanced functions

Page 3: Introduction to Matlab 332:202 Discrete Mathematics Spring 2007.

Contents

• What is Matlab

• Matlab basic

• Advanced usage of Matlab

Page 4: Introduction to Matlab 332:202 Discrete Mathematics Spring 2007.

What is Matlab

• Matrix laboratory

• easy to learn

• robust

• powerful functions

Page 5: Introduction to Matlab 332:202 Discrete Mathematics Spring 2007.

Contents

• What is Matlab

• Matlab basic

• Advanced usage of Matlab

Page 6: Introduction to Matlab 332:202 Discrete Mathematics Spring 2007.

Get Help

• Help

- help

- help function name (help abs)

Page 7: Introduction to Matlab 332:202 Discrete Mathematics Spring 2007.

Basic Math Operations

• To compute a math expression composed of numbers and mathematics operators

• + addition• - subtraction• * multiplication• / division• ^ power• Compute (2+3-9)*7^2/4• =-49

Page 8: Introduction to Matlab 332:202 Discrete Mathematics Spring 2007.

General Math Expressions

• To compute a math expression composed of numbers, variables and mathematics operators

• Define all variables

• The others are the same

• E.g. x=3;y=6;x+y=? It is equivalent to 3+6

Page 9: Introduction to Matlab 332:202 Discrete Mathematics Spring 2007.

Exercise in Class(1)

• 5 minutes• x=123;y=234;z=345• Write the command expression and get the

result: 1) the sum of x and y 2) subtract y from z 3) the multiplication of x and z• Answer1) x+y=357;2)z-y=111;3)x*z=42435

Page 10: Introduction to Matlab 332:202 Discrete Mathematics Spring 2007.

Vectors and Matrices

• Arrays are represented by some elements within brackets

• The elements of each row is separated by at least one space

• The separation of rows is by semicolons

• E.g. [2 3;1 2]

Page 11: Introduction to Matlab 332:202 Discrete Mathematics Spring 2007.

How to Retrieve Elements of Matrix 1

16151413

1211109

8765

4321

x

x=[1 2 3 4;5 6 7 8;9 10 11 12;13 14 15 16]

What is the difference between x(2),x(1,2),x(:,2),x(2,:),x(1:2,2)?

Page 12: Introduction to Matlab 332:202 Discrete Mathematics Spring 2007.

How to Retrieve Elements of Matrix 2

• Index begins from 1, not 0• For matrix x:• x(i) is the ith element, counted along the

column• x(i,j) is the element at the ith row and jth column• x(:,j) is all the elements at the jth column• x(i,:) is all the elements at the ith row• x(i1:i2,j) is those elements at the jth column, begins

from index i1 to i2

Page 13: Introduction to Matlab 332:202 Discrete Mathematics Spring 2007.

Vector and Matrix Operations

• +,-,*,/ are used in operations between matrices

• .*,./,.^ are used in operations between matrix elements

• Pay attention to the dimensions of vectors and matrices

Page 14: Introduction to Matlab 332:202 Discrete Mathematics Spring 2007.

Exercise in Class(2)

• 10 mins• x=[1 2;3 4];y=[5 6;7 8];• z1=[1 2 3];z2=[4 5 6];1)Sum of x and y2) Product of x and y3)Bitwise product of x and y4) Bitwise division of z1 over z25) Bitwise division of z1 over z2, but for the first two elements

only (both the expression and result)Answer:1) [6 8;10 12]; 2) [19 22;43 50]; 3) [5 12;21 32];4)[0.25 0.4 0.5]; 5) z1(1:2)./z2(1:2)=[0.25 0.4]

Page 15: Introduction to Matlab 332:202 Discrete Mathematics Spring 2007.

Matrix Transpose

• More operators other than +,-,*,/,^

• Matrix transpose:’(apostrophe)

Page 16: Introduction to Matlab 332:202 Discrete Mathematics Spring 2007.

Contents

• What is Matlab

• Matlab basic

• Advanced usage of Matlab

Page 17: Introduction to Matlab 332:202 Discrete Mathematics Spring 2007.

Plot

• plot(y) plots the columns of y versus their index• plot(x,y) plots vector y versus vector x• plot(x,y,s) plots vector y versus vector x, and s is

a character string which states the line properties

• plot(X1,Y1,S1,X2,Y2,S2,X3,Y3,S3,...) combines the plots defined by the (X,Y,S) triples, where the X's and Y's are vectors or matrices and the S's are strings.

Page 18: Introduction to Matlab 332:202 Discrete Mathematics Spring 2007.

Color Options

- Yellow - ‘y’– Magenta - ‘m’– Cyan - ‘c’– Red - ‘r’– Green - ‘g’– Blue - ‘b’– Black - ‘k’

Page 19: Introduction to Matlab 332:202 Discrete Mathematics Spring 2007.

Line Styles

- solid line (default)

-- dashed line

: dotted line

-. dash-dot line

Page 20: Introduction to Matlab 332:202 Discrete Mathematics Spring 2007.

Line Markings

• + - plus sign• o - circle• * - asterisk• . - Point• x - cross• s - square• d - diamond• ^ - upward pointing triangle• v - downward pointing triangle• > - right pointing triangle• < - left pointing triangle• p - five-pointed star (pentagram)• h - six-pointed star (hexagram)

• Combined use of color, line style, and line marking : ‘r+:’

Page 21: Introduction to Matlab 332:202 Discrete Mathematics Spring 2007.

Example

1)t=0:0.1:4*pi; x=sin(t);

plot(x);plot(t,x); plot(t,x,’r’); plot(t,x, 'c+:’);

2)x=1:1:10;y=2*x;

plot(x,y,'r-');

Page 22: Introduction to Matlab 332:202 Discrete Mathematics Spring 2007.

Exercise in Class 3

• 15 mins

• Write a short program which plots 2 lines in a graph. Two cycles of sin wave and cos wave.

• Useful function: plot, sin, cos

• t=0:0.1:4*pi; x=sin(t);y=cos(t);

• plot(t,x,'r+-',t,y,'bo-.');

Page 23: Introduction to Matlab 332:202 Discrete Mathematics Spring 2007.

Grid

• Grid or GRID ON adds major grid lines to the current axes.

• GRID OFF removes major and minor grid lines from the current axes.

Page 24: Introduction to Matlab 332:202 Discrete Mathematics Spring 2007.

Comment Functions

• xlabel('text') adds text beside the x-axis on the current axis

• ylabel('text') adds text beside the y-axis on the current axis

• title('text') adds text at the top of the current axis• legend(string1,string2,string3, ...) puts a legend

on the current plot using the specified strings as labels

• text(x,y,'string') adds the text in the quotes to location (X,Y) on the current axes

Page 25: Introduction to Matlab 332:202 Discrete Mathematics Spring 2007.

Axis

• AXIS Control axis scaling and appearance.

• AXIS([XMIN XMAX YMIN YMAX]) sets scaling for the x- and y-axes on the current plot.

• AXIS OFF turns off all axis labeling, tick marks and background.

• AXIS ON turns axis labeling, tick marks and background back on.

Page 26: Introduction to Matlab 332:202 Discrete Mathematics Spring 2007.

Exercise in Class 4• 15 mins• Write a short program which plots 2 lines in a graph. Two cycles of sin wave and cos

wave.• Use as many functions as you can: plot, sin, cos, grid, xlabel, ylabel, grid, title,

legend, text, axis• t=0:0.1:4*pi; • x=sin(t);• y=cos(t);• plot(t,x,'r+-',t,y,'bo-.');• grid on;• xlabel('The Time');• ylabel('The Amplitude');• title('Sin Wave and Cos Wave');• legend('Sin','Cos','Location','Best');• axis([0,4*pi,-2,2]);• grid on;• text(3/4*pi,sin(3/4*pi),'\leftarrow Sin');• text(9/4*pi,cos(9/4*pi),'\leftarrow Cos');

Page 27: Introduction to Matlab 332:202 Discrete Mathematics Spring 2007.

Plots and Graphs – Con’t

• HOLD ON holds the current plot and all axis properties so that subsequent graphing commands add to the existing graph.

• HOLD OFF returns to the default mode whereby PLOT commands erase the previous plots and reset all axis properties before drawing new plots.

• Figure or figure ( );

Page 28: Introduction to Matlab 332:202 Discrete Mathematics Spring 2007.

Save Figures

• Save manually• Use Function Print - print filename directs the output to the

PostScript file designated by filename - print -dformat filename exports the figure to the

specified file using the specified graphics format, (such as TIFF).

e.g. print –dmeta filename print -deps filename

Page 29: Introduction to Matlab 332:202 Discrete Mathematics Spring 2007.

Exercise in Class 5

• 5 mins

• Generate a figure, and save it as ps file and emf file, respectively

Page 30: Introduction to Matlab 332:202 Discrete Mathematics Spring 2007.

Plots and Graphs – Con’t

One example, different views• Plot3() x=-1:0.1:1; y=-1:0.1:1;

z=x.^2+y.^2; plot3(x,y,z);• Mesh(); contour(); meshc(); meshz(); Surf(); [x,y]=meshgrid(-1:0.1:1);

z=x.^2+y.^2;mesh(x,y,z);

• H = SUBPLOT(m,n,p), or SUBPLOT(mnp), breaks the Figure window into an m-by-n matrix of small axes, selects the p-th axes

for the current plot, and returns the axis handle. The axes are counted along the top row of the Figure window, then the second row, etc.

Page 31: Introduction to Matlab 332:202 Discrete Mathematics Spring 2007.

Basic Control Statements-for

• for, end for variable=expression statement; end expression: initial value: increment: end value for i=1:5

for j=1:4A(i,j)=1/(i+j-1);

endend;

Page 32: Introduction to Matlab 332:202 Discrete Mathematics Spring 2007.

Exercise in Class 6

• 10 mins

• Write a for program to compute 0.5+1+1.5+2+…+50

• x=0;

• for i=0.5:0.5:50

• x=x+i;

• end

Page 33: Introduction to Matlab 332:202 Discrete Mathematics Spring 2007.

Basic Control Statements-while

• while, end while expression statement; end expression: a logical statement which is

connected by ==,<,<=,>,>=,~=.x=1;while x<5 x=x+1;end

Page 34: Introduction to Matlab 332:202 Discrete Mathematics Spring 2007.

Exercise in Class 7

• 10 mins• Write a while program to compute

0.5+1+1.5+2+…+50• i=0.5;x=0;• while i<=50• x=x+i;• i=i+0.5;• end

Page 35: Introduction to Matlab 332:202 Discrete Mathematics Spring 2007.

Basic Control Statements-if

• if, elseif, end IF expression statements ELSEIF expression statements ELSE statements END

Page 36: Introduction to Matlab 332:202 Discrete Mathematics Spring 2007.

M Files

• Put a bunch of commands in an .m file.• Run the file under the command window.• The using of ‘;’.• Write a function as an .m file.

- file name is the function name- the first line defines the function

function myresutl=myfunction(a, b);- the comment lines (start with %) follow the first line is shown when asking for help

Page 37: Introduction to Matlab 332:202 Discrete Mathematics Spring 2007.

Exercise in Class

• 10 mins

• Write a m file to compute 0.5+1+1.5+2+…+50

• Run it

Page 38: Introduction to Matlab 332:202 Discrete Mathematics Spring 2007.

Useful Links

• www.mathworks.com

• Google


Recommended