CIVL 5320 Thomas Ng 1. What is Matlab? MATrix LABoratory A high-performance language for technical...

18
CIVL 5320 Thomas Ng MATLAB tutorial 1

Transcript of CIVL 5320 Thomas Ng 1. What is Matlab? MATrix LABoratory A high-performance language for technical...

Page 1: CIVL 5320 Thomas Ng 1.  What is Matlab? MATrix LABoratory A high-performance language for technical computing An easy-to-use interactive environment.

1

CIVL 5320

Thomas Ng

MATLAB tutorial

Page 2: CIVL 5320 Thomas Ng 1.  What is Matlab? MATrix LABoratory A high-performance language for technical computing An easy-to-use interactive environment.

2

What is Matlab?

MATrix LABoratory A high-performance language for technical

computingAn easy-to-use interactive environment for

computation, visualization and programming

Getting Started with Matlab

Page 3: CIVL 5320 Thomas Ng 1.  What is Matlab? MATrix LABoratory A high-performance language for technical computing An easy-to-use interactive environment.

3

Interactive: Direct key in command after “>>”>> help>> lookfor CCChang

Off-line: Put all commands into a m-file (name.m) and execute the file as follows>> name (without suffix .m)All commands in the file will be executed

sequentiallyThe m-file has to be in the current directory or

directories set by “pathtool”

Input and Output

Page 4: CIVL 5320 Thomas Ng 1.  What is Matlab? MATrix LABoratory A high-performance language for technical computing An easy-to-use interactive environment.

4

mat-file (name.mat): Contains pre-defined variables, vectors and matrices>> save name

Save all variables into a file called name.mat>> save name CCChang is wonderful

Save variables, vectors or matrices “CCChang”, “is” and “wonderful” into a file called name.mat

>> load nameLoad all variables in the name.mat to the MATLAB

registry

Input and Output

Page 5: CIVL 5320 Thomas Ng 1.  What is Matlab? MATrix LABoratory A high-performance language for technical computing An easy-to-use interactive environment.

5

Do the followings >> help help >> help lookfor >> help plot >> lookfor plot

Put sdof.m and elcen.mat into the current directory >> sdof >> whos >> save temp >> clear all >> whos >> load temp >> whos

Create a m-file that contains the above commands and run the m-file

Exercise

Page 6: CIVL 5320 Thomas Ng 1.  What is Matlab? MATrix LABoratory A high-performance language for technical computing An easy-to-use interactive environment.

6

Sequence of operations >> CCChang

Is CCChang a variable? If not, next Is CCChang a built-in function? If not, next Is CCChang a m-file? If not, next ??? Undefined function or variable 'CCChang‘

Some useful commands Interrupting running the program: Ctrl+c Long command: …

>> x=sin(1)+1+cos(2)+sin(2)+ sin(2)+...

sin(4)+ sin(5)+ sin(6)+ sin(7) Suppress echo: ; Comment: %

Basic Operations

Page 7: CIVL 5320 Thomas Ng 1.  What is Matlab? MATrix LABoratory A high-performance language for technical computing An easy-to-use interactive environment.

7

Some useful commands>> Clear >> Close

Scalar, vector and matrix>> A=123 >> a=321 % >> Rowvector = [12 14 63]>> Colvector= [13; 45; -2]>> Matrix= [1 2 3; 4 5 6; 7 8 9]>> sub_matrix= Matrix( 1:2 , 2:3)

Basic Operations

aA

Page 8: CIVL 5320 Thomas Ng 1.  What is Matlab? MATrix LABoratory A high-performance language for technical computing An easy-to-use interactive environment.

8

Do the followings>> A=123>> A=123 %This is fun!>> A=123; >> x=sin(1)+1+cos(2)+sin(2)+ sin(2)+...

sin(4)+ sin(5)+ sin(6)+ sin(7)>> ColA=[1 ; 2 ; 3]>> RowA=[1 2 3]>> GuessA=[1:10]>> GuessB=[1:3:10]>> GuessC=[1:10]’

Exercise

Page 9: CIVL 5320 Thomas Ng 1.  What is Matlab? MATrix LABoratory A high-performance language for technical computing An easy-to-use interactive environment.

9

>> CC=[1 2 3; 4 5 6; 7 8 9]>> CC=[1:3; 4:6; 7:9]>> subCC1=CC(1:2, 2:3) % what do

you expect?>> subCC2=CC(: , 2:3) % what do

you expect?>> subCC3=CC(1:2, :) % what do you

expect?>> clear all

Exercise

Page 10: CIVL 5320 Thomas Ng 1.  What is Matlab? MATrix LABoratory A high-performance language for technical computing An easy-to-use interactive environment.

10

Basic OperationsPower ^ or .^ a^b or a.^b

Multiplication * or .* a*b or a.*b

Division / or ./ a/b or a./b

or \ or .\ b\a or b.\a

NOTE: 56/8 = 8\56

- (unary) + (unary)

Addition + a + b

Subtraction - a - b

Assignment = a = b (assign b to a)

Element by element operation

Matrix operation

Page 11: CIVL 5320 Thomas Ng 1.  What is Matlab? MATrix LABoratory A high-performance language for technical computing An easy-to-use interactive environment.

11

Special Variables

ans Default variable name for results

pi Value of inf

NaN Not a number e.g. 0/0

i and j i = j =

eps Smallest incremental number

realmin The smallest usable positive real number

realmax The largest usable positive real number

1

Page 12: CIVL 5320 Thomas Ng 1.  What is Matlab? MATrix LABoratory A high-performance language for technical computing An easy-to-use interactive environment.

12

Command Description

eye Identity matrix

zeros Zeros array

norm Norm of vector or matrix

det Matrix determinant

eig Eigenvalues and eigenvectors

lsim Simulate response of LTI models

for … end Repeat statements (do loop)

demo Run demonstrations

Many many more!Toolboxes contain specialized commands

Built-in Functions

Page 13: CIVL 5320 Thomas Ng 1.  What is Matlab? MATrix LABoratory A high-performance language for technical computing An easy-to-use interactive environment.

13

Do the followings>> RowA=[1 2 3]; ColA=[1 ; 2 ; 3]; >> RowA*ColA>> ColA*RowA>> ColA.*RowA>> ColA.*RowA’>> RowA.*ColA>> RowA.*ColA’>> RowA./ColA>> RowA./ColA’

Exercise

Page 14: CIVL 5320 Thomas Ng 1.  What is Matlab? MATrix LABoratory A high-performance language for technical computing An easy-to-use interactive environment.

14

>> CC=[1 2; 3 4]>> det(CC)>> eye(2)>> zeros(2,2)>> DD=[CC zeros(2,2); zeros(2,2) eye(2)]

Exercise

Page 15: CIVL 5320 Thomas Ng 1.  What is Matlab? MATrix LABoratory A high-performance language for technical computing An easy-to-use interactive environment.

15

Figure control>> figure(x) % Create/go to figure x>> clf(x) % Clear figure x>> close(x)% Close figure x>> title(‘I love MATLAB’) % Figure caption

Plotting>> plot(y) % Plot y versus index>> plot(x,y) % Plot y versus x>> semilogx(x,y) >> semilogy(x,y)>> loglog(x,y) >> plot3(x,y,z)>> subplot(m,n,p) % Create tiled plots

Plotting Figures

Page 16: CIVL 5320 Thomas Ng 1.  What is Matlab? MATrix LABoratory A high-performance language for technical computing An easy-to-use interactive environment.

16

Plotting Figures Plotting control

>> axis([xmin xmax ymin ymax])>> xlabel(‘This is x-axis label’)>> ylabel(‘This is y-axis label’)>> text(x, y, ‘This is a label’) % Add text to

(x,y)

Page 17: CIVL 5320 Thomas Ng 1.  What is Matlab? MATrix LABoratory A high-performance language for technical computing An easy-to-use interactive environment.

17

Open sdof.m file Go through the codes Run sdof.m file Modify the file Re-run

Exercise

Page 18: CIVL 5320 Thomas Ng 1.  What is Matlab? MATrix LABoratory A high-performance language for technical computing An easy-to-use interactive environment.

18

demo