(1.) MATLAB Tutorial (Part 1)

34
1 CHE338_Lecture#1 For reading Reference Chapter Page Chapra Appendix 1 933-940 MATLAB Tutorial http://www.mathworks.com/academia/student_center/tutorials/launchpad. html http://www.mathworks.com/help/techdoc/learn_matlab/bqr_2pl.html

description

MATLAB Tutorial

Transcript of (1.) MATLAB Tutorial (Part 1)

Page 1: (1.) MATLAB Tutorial (Part 1)

1

CHE338_Lecture#1

For reading

Reference Chapter Page

Chapra Appendix 1 933-940

MATLAB Tutorial

http://www.mathworks.com/academia/student_center/tutorials/launchpad.

html

http://www.mathworks.com/help/techdoc/learn_matlab/bqr_2pl.html

Page 2: (1.) MATLAB Tutorial (Part 1)

2

Objectives

To explain the MATLAB interface

To illustrate basic operations and commands

To give examples of scripts and functions

Page 3: (1.) MATLAB Tutorial (Part 1)

3

MATLAB Interface

Command Window (performing all

operations)

Page 4: (1.) MATLAB Tutorial (Part 1)

4

MATLAB Interface

To make your own layout:

ADD or delete and arrange the desktop components.

Save your layout.

To change the current directory and make folders:

Demonstration:

Page 5: (1.) MATLAB Tutorial (Part 1)

5

To make your own layout

Editor (writing scripts and build

functions)

Page 6: (1.) MATLAB Tutorial (Part 1)

6

Introduction Documents

Reference Chapter Page

Alkis Appendix 1 531-544

http://www.mathworks.com/academia/student_center/tutorials/launchpad.

html

From WWW

From the reference books

http://www.mathworks.com/help/techdoc/learn_matlab/bqr_2pl.html

Page 7: (1.) MATLAB Tutorial (Part 1)

7

Introduction documents for self-learning

From the MATLAB Interface:

• Use the Help menu

From the Command Window:

>> help :the most important function to get

Information on functions;

displays its definition, shows how to use it,

lists functions related and links to their doc.

>> help print : this displays information on ‘print’

>> doc print

Page 8: (1.) MATLAB Tutorial (Part 1)

8

How to create and define variables

To define variables (scalars, vectors, and matrixes),

simply give a value to name with equal sign:

For vectors, use ‘[..]’

>> c=[1 2 3]

>> c=[1,2,3]

>> d=[1;2;3]

For matrixes

>> e=[1 2 3;1 2 3] or >>e=[1 2 3

1 2 3]

>> a=3

>> a=5

For complex, use ‘i or j’

>> comp=2+3i

Page 9: (1.) MATLAB Tutorial (Part 1)

9

Variables

For vectors and matrixes:

>> variable name (ii,jj)= value

ii indicates row elements and jj = column elements

>> g1(1,1)= 1

>> g1(1,2)= 2

>> g1(1,3) =3

>> g1(1)=1

>> g1(2)=2

>> g1(3)=3

g1= 1 2 3

Page 10: (1.) MATLAB Tutorial (Part 1)

10

Variables

For vectors and matrixes:

>> variable name (ii,jj)= value

ii indicates row elements and jj = column elements

>> g2(1,1)=1

>> g2(2,1)=2

>> g2(3,1)=3

>> g4(1,1)= 1

>> g4(1,2)= 2

>> g4(2,1) =3

>> g4(2,2) =4

𝑔2 = 123

𝑔3 =1 23 4

Page 11: (1.) MATLAB Tutorial (Part 1)

11

Variables

For vectors and matrixes:

>> variable name (ii,jj)= value

ii indicates row elements and jj = column elements

>> g2(1,1)=1

>> g2(2,1)=2

>> g2(3,1)=3

>> g4(1,1)= 1

>> g4(1,2)= 2

>> g4(2,1) =3

>> g4(2,2) =4

𝑔2 = 123

𝑔3 =1 23 4

Page 12: (1.) MATLAB Tutorial (Part 1)

12

Variables

For vectors and matrixes:

>> variable name (ii,jj)= value

ii indicates row elements and jj = column elements

What happens if you write

>> g5 (4,4) = 4

Page 13: (1.) MATLAB Tutorial (Part 1)

13

Variables

For vectors and matrixes:

>> variable name (ii,jj)= value

ii indicates row elements and jj = column elements

What happens if you write

>> g6 (1:10,1:10) = 4

Page 14: (1.) MATLAB Tutorial (Part 1)

14

Variables

Matrixes can be combined to form a new matrix

Use the colon to generate a row vector;

>> h=1:10

You can control the increment by adding a value

between

>>h=1:0.5:10

>>f=[c;c]

>>g=[f,f]

Page 15: (1.) MATLAB Tutorial (Part 1)

15

Variables

To define character variables: use single quotes and equal

sign.

>> k=‘rilla’

• Case sensitive; a and A are recognized as different

variables

• First character should be a letter

To save your variables (results): use ‘save’

>> save ‘filename’ ‘variables’

>>save lecture1 a b c

To load the file

>>load ‘filename’; >> load lecture1

Page 16: (1.) MATLAB Tutorial (Part 1)

16

Variables

Built-in variables:

To clear the Command Window:

>>clc

i and j

pi

ans

Inf and –Inf

NaN

Some useful functions:

size

length = max(size( ))

who

whos

To clear variables from the Workspace:

>> clear ‘variable’; >> clear a b c or >>clear all

Page 17: (1.) MATLAB Tutorial (Part 1)

17

Basic operations and commands

Mathematical functions: *,-,+,/,\, and ^

+

-

*

/

\

^

Additional operator

Subtraction operator

Multiplication

Right division

Left division

Exponentiation

Scalar to scalar operations and scalar to matrix operations

are straightforward

>>2+3/4*(5/3)^(2/3)

>>a=[1 2 3]

>>3*a

>>a/3

Page 18: (1.) MATLAB Tutorial (Part 1)

18

Basic operations and commands

Addition and subtraction of matrix operations are similar to

scalars, however their sizes must match: a(1x3) - b(3x1)

gives an error;

>>a=[1,2,3]

>>b=[1;2;3]

>>c=a-b

For multiplication, inner dimensions must match:

a(1x3)*a(1x3) gives an error;

>> c=a*a

>> c=a*b

>> d=[ 1 2; 1 2]

>> dd=d*d

>>d2=d^2

Page 19: (1.) MATLAB Tutorial (Part 1)

19

Basic operations and commands

To perform element-by-element operations, you must use

the period ‘.’ before operators, and both dimensions must

be the same;

>>a=[1 2 3]

>>b=a

>>c=a.*b

>>d=a.^2

How about?

>>c=a*b

>>d=a^2

Page 20: (1.) MATLAB Tutorial (Part 1)

20

Basic operations and commands

Other useful operation functions:

log

sqrt

exp

det(A)

eig(A)

dot(A,B)

cross(A,B)

inverse(A)

transpose(A)

sum(A)

prod(A)

>> a = (1 2 3)

>> prod(a)

>> b = [ 1 2 3

0 2 5]

>> prod(b)

Page 21: (1.) MATLAB Tutorial (Part 1)

21

Basic operations and commands

Other useful operation functions:

rand(N) or rand(N,M)

zeros(N) or zeros(N,M)

ones(N) or ones(N,M)

nan(N) or nan(N,M)

>> rand(4)

>> zeros(4)

>> rand(4,4)

Page 22: (1.) MATLAB Tutorial (Part 1)

22

Editor for scripts and functions

Where you write a sequence of

commands executed to run programs

and also you can create functions.

Saved as *.m file

Page 23: (1.) MATLAB Tutorial (Part 1)

23

Editor for scripts

% : at beginning of a sentence in order to write comments

; : at end of each line to avoid printing all values

Saved in the current directory as *.m file.

% Script example file

R=8314; % gas constant

t=input('temperature=')

p=input('pressure=')*1e5

v=R*t/p; % ideal gas law

save vol v

Page 24: (1.) MATLAB Tutorial (Part 1)

24

Flow control

Relational operators:

eq - Equal ==

ne - Not equal ~=

lt - Less than <

gt - Greater than >

le - Less than or equal <=

ge - Greater than or equal >=

>> 1 ==1

>> eq(1,1)

Page 25: (1.) MATLAB Tutorial (Part 1)

25

if (condition)

statements

to be executed

end

Flow control: conditional statement

x=1

if x>0

y=x*2;

end

Page 26: (1.) MATLAB Tutorial (Part 1)

26

if (condition)

statements

to be

executed

else

statements

to be

executed

end

if (condition)

statements

to be executed

elseif …

statements

to be executed

else

statements

to be executed

end

Flow control: conditional statement

x=1;

if x<0

y=x*2;

else

x = 4;

end

Page 27: (1.) MATLAB Tutorial (Part 1)

27

for ii =1:2

ii

end % If ii = the last number, then

get out of the loop

Flow control: loop statement

for (condition)

statements

to be executed

end

for ii =1:10

ii

end

for ii=1:2:10

ii

end

for

ii=1:2.5:10

ii

end

Page 28: (1.) MATLAB Tutorial (Part 1)

28

while (condition)

statements

to be executed

end

x=0

while x < 10

x=x+1

end

Flow control: loop statement

for (condition)

statements

to be executed

end

x=0

for ii=1:10

x=x+1

end

save add x

Page 29: (1.) MATLAB Tutorial (Part 1)

29

while (condition)

statements

to be executed

end

x=0

while x < 10

x=x+1

end

Flow control: loop statement

for (condition)

statements

to be executed

end

x=0

for ii=1:10

x=x+1

end

save add x

Page 30: (1.) MATLAB Tutorial (Part 1)

30

Flow control: double loop statement

for (condition)

for (condition)

statements

to be executed

end

end

for ii =1:2

for jj = 1:2

ii

jj

end

end

Page 31: (1.) MATLAB Tutorial (Part 1)

31

Flow control: create a matrix using a double loop statement

for ii =1:2

for jj = 1:2

z(ii,jj) = ii + jj

end

end

Page 32: (1.) MATLAB Tutorial (Part 1)

32

Functions

There are many built-in functions in MATLAB, however you

can still build your own functions for your programs to run;

function [‘output’]=‘function name’[‘input’]

function v=myfunction(t,p) %for single output variable

%you don’t need the square brackets.

function [v,w,z]=myfunction(t,p)

Save as ‘function name’.m file; myfunction.m

Page 33: (1.) MATLAB Tutorial (Part 1)

33

Example on function

function v=myfunction(t,p)

%function test

%this calculates the specific volume of an ideal gas

R=8314;

v =R*t/p;

p=10; t=10;

Vol=myfunction(t,p)

pp=10; tt=10;

vol2=myfunction(tt,pp)

Page 34: (1.) MATLAB Tutorial (Part 1)

34

MATLAB Interface

Basic operations and commands

Scripts and functions using the Editor

Summary