+ All Categories
Home > Documents > MATLAB Tutorial – The Basics - Intranet home · ˘ by C. Chuang ˝ INTRODUCTION A Powerful...

MATLAB Tutorial – The Basics - Intranet home · ˘ by C. Chuang ˝ INTRODUCTION A Powerful...

Date post: 09-Jul-2020
Category:
Upload: others
View: 1 times
Download: 0 times
Share this document with a friend
46
by C. Chuang INTRODUCTION A Powerful Calculator MATRIX String FLOW CONTROL GRAPHICS MATLAB Tutorial – The Basics Chienmin Chuang School of Mathematics, University of Birmingham May 24, 2011
Transcript
Page 1: MATLAB Tutorial – The Basics - Intranet home · ˘ by C. Chuang ˝ INTRODUCTION A Powerful Calculator MATRIX String FLOW CONTROL GRAPHICS NUMERICAL VARIABLE Some preset constants

∼ by C. Chuang�

INTRODUCTION A Powerful Calculator MATRIX String FLOW CONTROL GRAPHICS

MATLAB Tutorial – The Basics

Chienmin ChuangSchool of Mathematics, University of Birmingham

May 24, 2011

Page 2: MATLAB Tutorial – The Basics - Intranet home · ˘ by C. Chuang ˝ INTRODUCTION A Powerful Calculator MATRIX String FLOW CONTROL GRAPHICS NUMERICAL VARIABLE Some preset constants

∼ by C. Chuang�

INTRODUCTION A Powerful Calculator MATRIX String FLOW CONTROL GRAPHICS

WHAT IS MATLAB?

MATLAB, coined from MATrix LABoratory, is a mathematicalcomputing software developed by MathWorks. Simply speaking, itcan be thought as a calculator which is powerful in matrix operations.To a further extent, it is a powerful programming language equippedwith various built-in functions and (optional) tool boxes and can beused to build a customized program.

Page 3: MATLAB Tutorial – The Basics - Intranet home · ˘ by C. Chuang ˝ INTRODUCTION A Powerful Calculator MATRIX String FLOW CONTROL GRAPHICS NUMERICAL VARIABLE Some preset constants

∼ by C. Chuang�

INTRODUCTION A Powerful Calculator MATRIX String FLOW CONTROL GRAPHICS

HOW TO START MATLAB?

Simply double click the MATLAB icon on your desktop or execute theprogram from the start menu. You will see the Current Directoryon the top row of the interface where you create, save andconnect all your programs and files. In addition, there are threemain windows:

I Workspace: All the variables currently used are displayedhere.

I Command: Used for executing all commands, built-infunctions and your own programs.

I History: Show what you have done on MATLAB.

Page 4: MATLAB Tutorial – The Basics - Intranet home · ˘ by C. Chuang ˝ INTRODUCTION A Powerful Calculator MATRIX String FLOW CONTROL GRAPHICS NUMERICAL VARIABLE Some preset constants

∼ by C. Chuang�

INTRODUCTION A Powerful Calculator MATRIX String FLOW CONTROL GRAPHICS

HOW TO START MATLAB?

Your best friend when using MatLab – help function.I Simply type Help, you can get instruction of how to use

MATLAB.I To know how to use a certain built-in function, simply

type help <function-name>.I Another choice: Help>Using the Command Window.

Page 5: MATLAB Tutorial – The Basics - Intranet home · ˘ by C. Chuang ˝ INTRODUCTION A Powerful Calculator MATRIX String FLOW CONTROL GRAPHICS NUMERICAL VARIABLE Some preset constants

∼ by C. Chuang�

INTRODUCTION A Powerful Calculator MATRIX String FLOW CONTROL GRAPHICS

NUMERICAL VARIABLE

Type any number in the command window and then pressEnter. MATLAB always echoes back with ans=the typednumber.

I MATLAB generates the ans variable automatically when auser specifies no output argument.

I A user can define a numerical variable using the syntax:variable-name = numerical value.

I An semicolon (;) can be put in the end for no echo.Examples:

Page 6: MATLAB Tutorial – The Basics - Intranet home · ˘ by C. Chuang ˝ INTRODUCTION A Powerful Calculator MATRIX String FLOW CONTROL GRAPHICS NUMERICAL VARIABLE Some preset constants

∼ by C. Chuang�

INTRODUCTION A Powerful Calculator MATRIX String FLOW CONTROL GRAPHICS

NUMERICAL VARIABLE

Some preset constants areI i and j both refer to

√−1.

I pi is the ratio of circle’s circumference to its diameter.I Nan refers to Not a Number and has the form 0/0.I Inf refers to Infinity and has the form 1/0.

Technical points:1. Preset names can be reloaded with new values or data.2. Inf-Inf and Inf/Inf both produce NaN.3. The exponential base is not preset as e. To define the

number, we can use e=exp(1).Examples:

Page 7: MATLAB Tutorial – The Basics - Intranet home · ˘ by C. Chuang ˝ INTRODUCTION A Powerful Calculator MATRIX String FLOW CONTROL GRAPHICS NUMERICAL VARIABLE Some preset constants

∼ by C. Chuang�

INTRODUCTION A Powerful Calculator MATRIX String FLOW CONTROL GRAPHICS

DISPLAY FORMATS

Numerical values or variables can be displayed in differentformats. Below are widely used types:

I format short/long displays values with 4/14-15 digits afterthe decimal point.

I format short/long eng displays values in the engineeringformat with 4/16 digits after the point.

I format rat displays values in terms of an rational numberwhich may be exact or approximate to the original one.

Examples:Short is he default format type. Simply type help format formore details.

Page 8: MATLAB Tutorial – The Basics - Intranet home · ˘ by C. Chuang ˝ INTRODUCTION A Powerful Calculator MATRIX String FLOW CONTROL GRAPHICS NUMERICAL VARIABLE Some preset constants

∼ by C. Chuang�

INTRODUCTION A Powerful Calculator MATRIX String FLOW CONTROL GRAPHICS

ARITHMETIC OPERATIONSFive basic operations:+ (add), -(subtract), * (multiply), � (divide) andˆ(exponentiate or raise to a power).Tips:

1. Priority: ˆ � ∗, / � +,−.2. Use space ” ” and bracket ”()” for clear expression.3. A comma ”, ” is used to separate two expressions.4. If a semicolon ”; ” ends an equation, the result will not be

shown on the screen. It is also used to end a line ofcommand and separate it from the next one.

5. A variable can be defined by simply assigning a value tothe variable name. To clear a variable x from memory,simply type ”clear x”. ”clear all” clears all variables. Notethat MATLAB are case-sensitive to variable names.

6. To check what variables are currently used, type whos.

Page 9: MATLAB Tutorial – The Basics - Intranet home · ˘ by C. Chuang ˝ INTRODUCTION A Powerful Calculator MATRIX String FLOW CONTROL GRAPHICS NUMERICAL VARIABLE Some preset constants

∼ by C. Chuang�

INTRODUCTION A Powerful Calculator MATRIX String FLOW CONTROL GRAPHICS

ARITHMETIC OPERATIONS

Examples:The command clc is used to to clear command window.

Page 10: MATLAB Tutorial – The Basics - Intranet home · ˘ by C. Chuang ˝ INTRODUCTION A Powerful Calculator MATRIX String FLOW CONTROL GRAPHICS NUMERICAL VARIABLE Some preset constants

∼ by C. Chuang�

INTRODUCTION A Powerful Calculator MATRIX String FLOW CONTROL GRAPHICS

ELEMENTARY FUNCTIONS

There are a variety of elementary mathematical functions builtin MATLAB. Type help elfun in the command window for alldetails. We introduce a few here. For any variable (or value) x:

I log(x): Natural logarithm of x.I exp(x): Exponential of x or e = 2.71828... to power x.I sin(x), cos(x), tan(x), cot(x), sec(x), csc(x): Trigonometric

functions of x.I abs(x): Modulus or absolute value of x.I sqrt(x): Square root of X.I conj(x): Complex conjugate of x.I imag(x): Complex imaginary part of x.I real(x): Complex real part of x.

Page 11: MATLAB Tutorial – The Basics - Intranet home · ˘ by C. Chuang ˝ INTRODUCTION A Powerful Calculator MATRIX String FLOW CONTROL GRAPHICS NUMERICAL VARIABLE Some preset constants

∼ by C. Chuang�

INTRODUCTION A Powerful Calculator MATRIX String FLOW CONTROL GRAPHICS

DEFINE A VECTOR

Below are common ways to define a horizontal row vector.a:incerment:b

Defines a row vector with starting entry a and thelast entry (less than or) equal to b in the step ofincrement. In particular, a:b is the shorthand ofa:1:b.

linspace(a,b,n)Defines a row vector with starting entry a and thelast entry equal to b with n equidistant entries intotal.

Examples:

Page 12: MATLAB Tutorial – The Basics - Intranet home · ˘ by C. Chuang ˝ INTRODUCTION A Powerful Calculator MATRIX String FLOW CONTROL GRAPHICS NUMERICAL VARIABLE Some preset constants

∼ by C. Chuang�

INTRODUCTION A Powerful Calculator MATRIX String FLOW CONTROL GRAPHICS

DEFINE A MATRIX

When defining a matrix or vector,I ”, ” separate entries in the same row which can also be

replaced by space ” ”.I ”; ” separate different rows.I ”[ ]” is used to cover all its entry values. Besides, ”[ ]”

can also combine two matrices to a bigger one.I We can also define an empty matrix by ”C=[]”.

For example,

Page 13: MATLAB Tutorial – The Basics - Intranet home · ˘ by C. Chuang ˝ INTRODUCTION A Powerful Calculator MATRIX String FLOW CONTROL GRAPHICS NUMERICAL VARIABLE Some preset constants

∼ by C. Chuang�

INTRODUCTION A Powerful Calculator MATRIX String FLOW CONTROL GRAPHICS

DEFINE A MATRIX

Several functions used to define specific matrices:I zeros(m,n) defines an m× n zero matrix.I ones(m,n) defines an m× n matrix with entries equal to

one.I eye(m,n) defines an m× n matrix with main-diagonal

entries equal to one and others equal to zero. In particular,eye(m,m) can be shortened to eye(m) and defines a m×midentity matrix.

I rand(m,n) generates an m× n random matrix with entriesdrawn from a uniform distribution over [0, 1].

I randn(m,n) generates an m× n random matrix with entriesdrawn from a normal distribution with zero mean and unitvariance.

Page 14: MATLAB Tutorial – The Basics - Intranet home · ˘ by C. Chuang ˝ INTRODUCTION A Powerful Calculator MATRIX String FLOW CONTROL GRAPHICS NUMERICAL VARIABLE Some preset constants

∼ by C. Chuang�

INTRODUCTION A Powerful Calculator MATRIX String FLOW CONTROL GRAPHICS

DEFINE A MATRIX

Provided values x0, x1, . . . , xn, a Vander matrix is a matrix of theform below.

xn0 · · · x2

0 x0 1xn

1 · · · x21 x1 1

xn2 · · · x2

2 x2 1...

. . ....

......

xnn · · · x2

n xn 1

Such a matrix is useful when doing nth-order interpolation andcan be generated by MatLab using vander([x0; x1; . . . ; xn]).Examples:

Page 15: MATLAB Tutorial – The Basics - Intranet home · ˘ by C. Chuang ˝ INTRODUCTION A Powerful Calculator MATRIX String FLOW CONTROL GRAPHICS NUMERICAL VARIABLE Some preset constants

∼ by C. Chuang�

INTRODUCTION A Powerful Calculator MATRIX String FLOW CONTROL GRAPHICS

MATRIX MANIPULATION

If two matrices A and B are compatible in dimensions, we canuse the following matrix operations.

I Basics: A + B A− B A ∗ BI Inverse for a square matrix C: inv(C)I Power m: C ˆ m

In particular, C ˆ (−1) = inv(C).I Transpose: A′

I Entry-by-entry operation: . ∗ ./ . ˆ

I For two compatible matrices A and B:I A*inv(B)=A/BI inv(B)*A=B� A

I To solve a system of equations Ax=b where A is a squarematrix and b is a vector, we can use x=inv(A)*b or x=A�b.In general, � is faster than using inv.

Page 16: MATLAB Tutorial – The Basics - Intranet home · ˘ by C. Chuang ˝ INTRODUCTION A Powerful Calculator MATRIX String FLOW CONTROL GRAPHICS NUMERICAL VARIABLE Some preset constants

∼ by C. Chuang�

INTRODUCTION A Powerful Calculator MATRIX String FLOW CONTROL GRAPHICS

MATRIX MANIPULATION

Define A=[1 2; 3 4; 5 6], B=[7 8 9; 10 11 12] and then computethe examples below:

Page 17: MATLAB Tutorial – The Basics - Intranet home · ˘ by C. Chuang ˝ INTRODUCTION A Powerful Calculator MATRIX String FLOW CONTROL GRAPHICS NUMERICAL VARIABLE Some preset constants

∼ by C. Chuang�

INTRODUCTION A Powerful Calculator MATRIX String FLOW CONTROL GRAPHICS

INDEXING ENTRIES

Suppose M is a n× n matrix, then:I M(i,j) is the (i,j) entry of M for any i,j<=n.I M(i,:) is the ith. row of M for any i<=n.I M(:,j) is the jth. column of M for any j<=n.I M([r1r2...rm], [c1c2...cw]) is a matrix with the (i,j) entry equal

to M(ri, cj);Try the examples:

Page 18: MATLAB Tutorial – The Basics - Intranet home · ˘ by C. Chuang ˝ INTRODUCTION A Powerful Calculator MATRIX String FLOW CONTROL GRAPHICS NUMERICAL VARIABLE Some preset constants

∼ by C. Chuang�

INTRODUCTION A Powerful Calculator MATRIX String FLOW CONTROL GRAPHICS

INDEXING ENTRIES

For every subscript (i,j), there is a unique single index scorresponding to it.Examples:

Note that for a row or column vector, we can simply use such asingle index to specify an entry.Examples:

Page 19: MATLAB Tutorial – The Basics - Intranet home · ˘ by C. Chuang ˝ INTRODUCTION A Powerful Calculator MATRIX String FLOW CONTROL GRAPHICS NUMERICAL VARIABLE Some preset constants

∼ by C. Chuang�

INTRODUCTION A Powerful Calculator MATRIX String FLOW CONTROL GRAPHICS

INDEXING ENTRIES

To convert between these two indexing methods, we can useI sub2ind(sz,i,j): returns a single index using the subscripts

(i,j) for a matrix of size sz.I ind2sub(sz,s): returns the subscripts (i,j) using a single

index s for a matrix of size sz.A matrix M can be easily vectorized in the single order byreloading M=M(:).Examples:

Page 20: MATLAB Tutorial – The Basics - Intranet home · ˘ by C. Chuang ˝ INTRODUCTION A Powerful Calculator MATRIX String FLOW CONTROL GRAPHICS NUMERICAL VARIABLE Some preset constants

∼ by C. Chuang�

INTRODUCTION A Powerful Calculator MATRIX String FLOW CONTROL GRAPHICS

SET OPERATIONS OF TWO VECTORS

Think of two vectors V1 and V2 as two sets. We can do setoperations as below.

I union(V1,V2) generates their union in terms of a rowvector.

I intersect(V1,V2) generates their intersection in terms of arow vector.

I setdiff(V1,V2) generates their difference in terms of a rowvector.

Examples:

Page 21: MATLAB Tutorial – The Basics - Intranet home · ˘ by C. Chuang ˝ INTRODUCTION A Powerful Calculator MATRIX String FLOW CONTROL GRAPHICS NUMERICAL VARIABLE Some preset constants

∼ by C. Chuang�

INTRODUCTION A Powerful Calculator MATRIX String FLOW CONTROL GRAPHICS

FUNCTIONS ASSOCIATED WITH MATRIX

Suppose M is a matrix.I size(M) returns the size of M.I length(M) amounts to max(size(M)).I numel(M) returns the number of entries in M.I sign(M) returns the signs of all entries in M.

Examples:

Most elementary functions can be applied to a Matrix in termsof entries.Examples:

Page 22: MATLAB Tutorial – The Basics - Intranet home · ˘ by C. Chuang ˝ INTRODUCTION A Powerful Calculator MATRIX String FLOW CONTROL GRAPHICS NUMERICAL VARIABLE Some preset constants

∼ by C. Chuang�

INTRODUCTION A Powerful Calculator MATRIX String FLOW CONTROL GRAPHICS

FUNCTIONS ASSOCIATED WITH MATRIX

Suppose M is a matrix. We can permute its entries in thefollowing ways.

I sort(M) sort entries in ascending column by column. Thisis equivalent using sort(M,1). To sort M row by row usingsort(M,2). To sort M in descending order, put ’descend’ asthe last argument.

I fliplr(M) flips columns of M in the left-right direction.I flipud(M) flips columns of M in the up-down direction.

Examples:

Page 23: MATLAB Tutorial – The Basics - Intranet home · ˘ by C. Chuang ˝ INTRODUCTION A Powerful Calculator MATRIX String FLOW CONTROL GRAPHICS NUMERICAL VARIABLE Some preset constants

∼ by C. Chuang�

INTRODUCTION A Powerful Calculator MATRIX String FLOW CONTROL GRAPHICS

FUNCTIONS ASSOCIATED WITH MATRIX

diag is a multipurpose function.I If v is a n× 1 vector, diag(v) defines a diagonal matrix with

the main-diagonal entries equal to those of vector v.Moreover, diag(v,k) defines a (n + |k|)× (n + |k|) matrixwith the kth-diagonal entries equal to those of vector v.

I If M is a n×m matrix, diag(M,k) returns the kth diagonalof M in terms of a column vector. In particular, diag(M)defines the column vector equal to the main diagonal of M.

Examples:

Page 24: MATLAB Tutorial – The Basics - Intranet home · ˘ by C. Chuang ˝ INTRODUCTION A Powerful Calculator MATRIX String FLOW CONTROL GRAPHICS NUMERICAL VARIABLE Some preset constants

∼ by C. Chuang�

INTRODUCTION A Powerful Calculator MATRIX String FLOW CONTROL GRAPHICS

FUNCTIONS ASSOCIATED WITH MATRIX

I spdiags(Ds,d,m,n) returns an m-by-n sparse matrix bytaking the columns of matrix Ds and placing them alongthe diagonals specified by d. (Beware the entries assignedto non-main-diagonals!) This is particularly convenient fordefining tri-diagonal and banded matrices. For moredetails, refer to ite help file. Note that MATLAB onlyshows the nonzeros of a sparse matrix.

I full(M) converts a sparse matrix M to a full one.I In contrast, sparse(M) turns the matrix M into a sparse one

when existing numerous zero entries.

Example:

Page 25: MATLAB Tutorial – The Basics - Intranet home · ˘ by C. Chuang ˝ INTRODUCTION A Powerful Calculator MATRIX String FLOW CONTROL GRAPHICS NUMERICAL VARIABLE Some preset constants

∼ by C. Chuang�

INTRODUCTION A Powerful Calculator MATRIX String FLOW CONTROL GRAPHICS

STRING VARIABLE

String is a combination of characters, including space. Eachentry of a string is a character. Both can be defined using singlequote ’ ’. (Not double quote ” ”!) Similar to a matrix variable,length(), numel() and size() can apply to a string variable.Examples:

A numerical variable can be transformed to a string usingnum2str(). str2num() can do the reverse. However, if the inputstring does not represent a valid number or matrix, str2num()returns the empty matrix in xExamples:

Page 26: MATLAB Tutorial – The Basics - Intranet home · ˘ by C. Chuang ˝ INTRODUCTION A Powerful Calculator MATRIX String FLOW CONTROL GRAPHICS NUMERICAL VARIABLE Some preset constants

∼ by C. Chuang�

INTRODUCTION A Powerful Calculator MATRIX String FLOW CONTROL GRAPHICS

FUNCTIONS ASSOCIATED WITH STRING

I Strings can be combined by using square brackets [].I disp(a) displays the content of variables where a can be a

number or a string.I whos shows information of all variables while whos(’a’)

shows information of the variable a. This command can beshortened to who with less information provided.

I strcmp(s1,s2) compares two string variables s1 and s2,returns 1 if they are identical and 0 otherwise.

I strncmp(s1,s2,n) compares the first n characters of twostring variables s1 and s2 and returns 1 or 0.

I findstr(s1,s2) searches any occurrences of the shorterstring, which returns the starting index of each occurrenceif any and empty array [] otherwise.

I strrep(s,s1,s2) searches the shorter string s1 in a longerstring s and replaces s1 by another string s2.

Page 27: MATLAB Tutorial – The Basics - Intranet home · ˘ by C. Chuang ˝ INTRODUCTION A Powerful Calculator MATRIX String FLOW CONTROL GRAPHICS NUMERICAL VARIABLE Some preset constants

∼ by C. Chuang�

INTRODUCTION A Powerful Calculator MATRIX String FLOW CONTROL GRAPHICS

FUNCTIONS ASSOCIATED WITH STRING

Examples:

Page 28: MATLAB Tutorial – The Basics - Intranet home · ˘ by C. Chuang ˝ INTRODUCTION A Powerful Calculator MATRIX String FLOW CONTROL GRAPHICS NUMERICAL VARIABLE Some preset constants

∼ by C. Chuang�

INTRODUCTION A Powerful Calculator MATRIX String FLOW CONTROL GRAPHICS

RELATIONAL OPERATORS

I The relational operators are <,>,<=, >=,==, and ∼=,which return logical value 1 (true) if the specified relationholds and 0 (false) otherwise. The operators are performedand return values on the elemental basis. Note that ” =(assign or define) ” is different from ” == (equal to)”.

I true and false are preset logical variables with valuesequal to 1 and 0 respectively.

Examples:

Page 29: MATLAB Tutorial – The Basics - Intranet home · ˘ by C. Chuang ˝ INTRODUCTION A Powerful Calculator MATRIX String FLOW CONTROL GRAPHICS NUMERICAL VARIABLE Some preset constants

∼ by C. Chuang�

INTRODUCTION A Powerful Calculator MATRIX String FLOW CONTROL GRAPHICS

REDUCE MULTIPLE RELATIONS TO ONE

Suppose N is a numerical matrix and M is a numerical orlogical Matrix.

I or(expr1, expr2) and expr1 | expr2 returns 1 if at least oneof expr1 and expr2 is equal to logical 1; otherwise, itreturns logical 0.

I and(expr1, expr2) and expr1 & expr2 returns 1 if bothexpr1 and expr2 are equal to logical 1; otherwise, it returnslogical 0.

I logical(N) transforms N to a logical matrices with 0corresponding to zero entry of N and 1 to nonzeros.

I any(M) returns logical value 1 for the column(s) with atleast one nonzero entry and logical value 0 otherwise.

I all(M) returns logical value 1 for the column(s) with allnonzero entries and logical value 0 otherwise.

Page 30: MATLAB Tutorial – The Basics - Intranet home · ˘ by C. Chuang ˝ INTRODUCTION A Powerful Calculator MATRIX String FLOW CONTROL GRAPHICS NUMERICAL VARIABLE Some preset constants

∼ by C. Chuang�

INTRODUCTION A Powerful Calculator MATRIX String FLOW CONTROL GRAPHICS

REDUCE MULTIPLE RELATIONS TO ONE

Examples:

Page 31: MATLAB Tutorial – The Basics - Intranet home · ˘ by C. Chuang ˝ INTRODUCTION A Powerful Calculator MATRIX String FLOW CONTROL GRAPHICS NUMERICAL VARIABLE Some preset constants

∼ by C. Chuang�

INTRODUCTION A Powerful Calculator MATRIX String FLOW CONTROL GRAPHICS

CONDITIONER – IFI The syntax if condition, operations; end or

if conditionoperations;

endstates: the operations will be performed only if the conditionis true.

I A more general form is

if condition1operation1;

elseif condition2operation2;

elseoperation3;

endwhich states: if the condition1 is true, the operation1 will beperformed; else if condition2 is true, the operation2 will beperformed; otherwise, the operation3 will be performed.Note that elseif and else can be omitted if not required.Besides, elseif can be used more than once while else canbe used at most once.

Page 32: MATLAB Tutorial – The Basics - Intranet home · ˘ by C. Chuang ˝ INTRODUCTION A Powerful Calculator MATRIX String FLOW CONTROL GRAPHICS NUMERICAL VARIABLE Some preset constants

∼ by C. Chuang�

INTRODUCTION A Powerful Calculator MATRIX String FLOW CONTROL GRAPHICS

CONDITIONER – IF

Examples:

Page 33: MATLAB Tutorial – The Basics - Intranet home · ˘ by C. Chuang ˝ INTRODUCTION A Powerful Calculator MATRIX String FLOW CONTROL GRAPHICS NUMERICAL VARIABLE Some preset constants

∼ by C. Chuang�

INTRODUCTION A Powerful Calculator MATRIX String FLOW CONTROL GRAPHICS

CONDITIONER – IF

Tips:I In general, a nonzero value can be interpreted as a true

logical value when used as a condition.I ∼ transforms the values of true and false to each other.

Example:

Page 34: MATLAB Tutorial – The Basics - Intranet home · ˘ by C. Chuang ˝ INTRODUCTION A Powerful Calculator MATRIX String FLOW CONTROL GRAPHICS NUMERICAL VARIABLE Some preset constants

∼ by C. Chuang�

INTRODUCTION A Powerful Calculator MATRIX String FLOW CONTROL GRAPHICS

CONDITIONER – SWITCHswitch scenario-variablecase scenario1

operation1;...

case scenarioNoperationN;

otherwiseoperation-for-others;

end

The syntax states: operationK will be executed if thescenario-variable is equal to the scenarioK and operation-for-otherswill be executed if no matched scenario is available. Note thatonly one possibility may happen and only its correspondingoperation will be performed in the syntax. Note that, we canuse curly brackets {} to combines several scenarios as one.

Page 35: MATLAB Tutorial – The Basics - Intranet home · ˘ by C. Chuang ˝ INTRODUCTION A Powerful Calculator MATRIX String FLOW CONTROL GRAPHICS NUMERICAL VARIABLE Some preset constants

∼ by C. Chuang�

INTRODUCTION A Powerful Calculator MATRIX String FLOW CONTROL GRAPHICS

CONDITIONER – SWITCH

Examples:

Page 36: MATLAB Tutorial – The Basics - Intranet home · ˘ by C. Chuang ˝ INTRODUCTION A Powerful Calculator MATRIX String FLOW CONTROL GRAPHICS NUMERICAL VARIABLE Some preset constants

∼ by C. Chuang�

INTRODUCTION A Powerful Calculator MATRIX String FLOW CONTROL GRAPHICS

LOOP – FOR

I

for IndexVariable = a:Increment:boperations;

end

The syntax states the operations will be performediteratively. The iteration starts from IndexVariable equal toa, go to the next iteration by adding Increment toIndexVariable and stops until the IndexVariable equal to(or more than) b.

I If the Increment is equal to 1, then the ′ :Increment ′ can beomitted.

I Note that in the syntax, the value of IndexVariable isassigned as a single scalar, which is different fromIndexVariable=a:Increment:b used out of the syntax.

Page 37: MATLAB Tutorial – The Basics - Intranet home · ˘ by C. Chuang ˝ INTRODUCTION A Powerful Calculator MATRIX String FLOW CONTROL GRAPHICS NUMERICAL VARIABLE Some preset constants

∼ by C. Chuang�

INTRODUCTION A Powerful Calculator MATRIX String FLOW CONTROL GRAPHICS

LOOP – FOR

Examples:

Page 38: MATLAB Tutorial – The Basics - Intranet home · ˘ by C. Chuang ˝ INTRODUCTION A Powerful Calculator MATRIX String FLOW CONTROL GRAPHICS NUMERICAL VARIABLE Some preset constants

∼ by C. Chuang�

INTRODUCTION A Powerful Calculator MATRIX String FLOW CONTROL GRAPHICS

LOOP – WHILE

I

while conditionoperations;

end

The syntax states: the operations will be repeatedly carriedout until the condition does not hold.

I Note that if the procedure falls into infinite, use Ctrl+C tobreak the endless iteration.

Example:

Page 39: MATLAB Tutorial – The Basics - Intranet home · ˘ by C. Chuang ˝ INTRODUCTION A Powerful Calculator MATRIX String FLOW CONTROL GRAPHICS NUMERICAL VARIABLE Some preset constants

∼ by C. Chuang�

INTRODUCTION A Powerful Calculator MATRIX String FLOW CONTROL GRAPHICS

AUXILIARIES FOR LOOPS

Commands below can facilitate the usage of for-loop andwhile-loop which generally follow some specific conditions.

I break stops the current iteration of a loop and passescontrol to the outer scope of the loop.

I continue stops the current iteration of a loop and passescontrol to the next iteration of the same loop.

I return hands over the control to its invoking function orthe keyboard.

I Note both break and continue skip the remainingoperations of the current iteration after they are active.

Page 40: MATLAB Tutorial – The Basics - Intranet home · ˘ by C. Chuang ˝ INTRODUCTION A Powerful Calculator MATRIX String FLOW CONTROL GRAPHICS NUMERICAL VARIABLE Some preset constants

∼ by C. Chuang�

INTRODUCTION A Powerful Calculator MATRIX String FLOW CONTROL GRAPHICS

AUXILIARIES FOR LOOPS

Examples:

Page 41: MATLAB Tutorial – The Basics - Intranet home · ˘ by C. Chuang ˝ INTRODUCTION A Powerful Calculator MATRIX String FLOW CONTROL GRAPHICS NUMERICAL VARIABLE Some preset constants

∼ by C. Chuang�

INTRODUCTION A Powerful Calculator MATRIX String FLOW CONTROL GRAPHICS

2-D PLOT

I plot(data1, data2) creates a line graph with data1 on x-axisand data 2 on y-axis.

I Note that the input data are discrete, but plot(·) draws aline graph by connecting all data points. To show all pointswithout connection, we can use plot(data1, data2, ’.’).

I To specify more prpperties such as the line width, pointshape, color..., etc, use the help file.

I Similarly, scatter(data1, data2) can draw a scatter plot.I bar(data) generates a bar chart for the vector data.

Page 42: MATLAB Tutorial – The Basics - Intranet home · ˘ by C. Chuang ˝ INTRODUCTION A Powerful Calculator MATRIX String FLOW CONTROL GRAPHICS NUMERICAL VARIABLE Some preset constants

∼ by C. Chuang�

INTRODUCTION A Powerful Calculator MATRIX String FLOW CONTROL GRAPHICS

2-D PLOT

Examples:

Page 43: MATLAB Tutorial – The Basics - Intranet home · ˘ by C. Chuang ˝ INTRODUCTION A Powerful Calculator MATRIX String FLOW CONTROL GRAPHICS NUMERICAL VARIABLE Some preset constants

∼ by C. Chuang�

INTRODUCTION A Powerful Calculator MATRIX String FLOW CONTROL GRAPHICS

3-D PLOT

I The three-dimensional versions of previous plottingfunctions are plot3(data1, data2, data3), bar3(data) andscatter3(data1, data2, data3).

I Besides, mesh(X,Y,Z) draws a wireframe mesh using Z forthe color data and height. X and Y are vectors or matricesdefining the x and y components of a surface. Anotherchoice is to use surf(X,Y,Z) to create a shaded surface.When plotting in three dimension, [X,Y] = meshgrid(x,y) isuseful, which transforms the domain specified by vectors xand y into arrays X and Y

Page 44: MATLAB Tutorial – The Basics - Intranet home · ˘ by C. Chuang ˝ INTRODUCTION A Powerful Calculator MATRIX String FLOW CONTROL GRAPHICS NUMERICAL VARIABLE Some preset constants

∼ by C. Chuang�

INTRODUCTION A Powerful Calculator MATRIX String FLOW CONTROL GRAPHICS

3-D PLOT

Examples:

Page 45: MATLAB Tutorial – The Basics - Intranet home · ˘ by C. Chuang ˝ INTRODUCTION A Powerful Calculator MATRIX String FLOW CONTROL GRAPHICS NUMERICAL VARIABLE Some preset constants

∼ by C. Chuang�

INTRODUCTION A Powerful Calculator MATRIX String FLOW CONTROL GRAPHICS

BASIC CONTROL OF GRAPHS

I grid on and grid off adds and removes the grid of thegraph respectively.

I title(’PlotTitle’) set the plot title as Plottitle.I xlabel(’xName’), ylabel(’yName’) and zlabel(’zName’) set

the names of x,y and z coordinates as xName, yName andzName individually.

I legend(’string1’,’string2’,...) creates a legend using thespecified strings to describe each set of data.

I hold on retains following pictures in the current graphwhile hold off stops such combining graphs.

Page 46: MATLAB Tutorial – The Basics - Intranet home · ˘ by C. Chuang ˝ INTRODUCTION A Powerful Calculator MATRIX String FLOW CONTROL GRAPHICS NUMERICAL VARIABLE Some preset constants

∼ by C. Chuang�

INTRODUCTION A Powerful Calculator MATRIX String FLOW CONTROL GRAPHICS

BASIC CONTROL OF GRAPHS

Examples:


Recommended