Sie sind auf Seite 1von 29

MATLAB 101

A workshop by
Society of Chemical Engineers
Department of Chemical Engineering & Technology, IIT (BHU) Varanasi

MATLAB WINDOW

Society of Chemical Engineers, Dept. of Chemical Engineering & Technology, IIT (BHU) Varanasi
2

MATLAB WINDOW

Current Folder

Society of Chemical Engineers, Dept. of Chemical Engineering & Technology, IIT (BHU) Varanasi
3

MATLAB WINDOW

Command
Window
Current Folder

Society of Chemical Engineers, Dept. of Chemical Engineering & Technology, IIT (BHU) Varanasi
4

MATLAB WINDOW

Command
Window
Current Folder
Command History

Society of Chemical Engineers, Dept. of Chemical Engineering & Technology, IIT (BHU) Varanasi
5

MATLAB WINDOW

Workspace

Command
Window
Current Folder
Command History

Society of Chemical Engineers, Dept. of Chemical Engineering & Technology, IIT (BHU) Varanasi
6

STARTING OFF

M AT L A B i s a n i n t e r p r e t e r . i . e . I t r e a d s l i n e b y l i n e .

Any operation you do is stored in a variable called ans.

Make it a habit to end statements with a semicolon if you


dont want to echo its value.

Also, remember the three magic keywords:

clc , clear , help xxxxxx

Society of Chemical Engineers, Dept. of Chemical Engineering & Technology, IIT (BHU) Varanasi
7

DEFINING MATRICES

M AT L A B ( M a t r i x L a b o r a t o r y ) s t o r e s a l l t h e v a r i a b l e s a s

matrices.

Separate column elements by a comma or space.


e.g. A = [1 2 3]

Separate row elements by a semicolon.


e.g. A=[1,2,3;4,5,6]
A=[1 2 3;4 5 6]

Yo u c a n a l s o u s e z e r o s ( ) o r o n e s ( ) o r e y e ( ) o r r a n d n ( ) .

Society of Chemical Engineers, Dept. of Chemical Engineering & Technology, IIT (BHU) Varanasi
8

SUBSCRIPTING AND THE :

A ( i , j) u s e d t o ac c e s s t h e j t h el em e nt of t h e i t h r ow.

T h e c o l o n o p e r a t o r, : i s u s e d t o s e l e c t m u l t i p l e e l e m e n t s a t a
time
e.g. A(1,:) selects all columns for the first row
A(2:3,1:5) selects first five columns of 2 nd & 3rd row

The : can also be used for generating a vector of equallyspaced elements.


e.g. A = [1:20] generates a row of integers from 1 to 20
A = [1:2:20] generates a row of odd numbers till 20
(or numbers spaced by 2 starting from 1)
Society of Chemical Engineers, Dept. of Chemical Engineering & Technology, IIT (BHU) Varanasi
9

TASK 1

Construct the following matrix, without declaring it directly


and using at most 3 statements.
A =
0

Society of Chemical Engineers, Dept. of Chemical Engineering & Technology, IIT (BHU) Varanasi
10

MATRIX OPERATIONS

Concatenation : Normal matrix definitions except with


matrices instead of constants.
e.g. A= [a b ;c d]

Row/Column Deletion : Select the row/colum with the colon


operator and assign it to null.
e.g. a( 2,: )= []

Tr a n s p o s e : J u s t a d d a n a p o s t r o p h e a f t e r t h e m a t r i x n a m e .
e . g . Tr a n s p o s e o f A : A

Society of Chemical Engineers, Dept. of Chemical Engineering & Technology, IIT (BHU) Varanasi
11

TASK 2

Construct the following matrix using a single statement


A =
8

*the first 3*3 matrix (in red) is a magic square.

Then delete the alternate rows.

Society of Chemical Engineers, Dept. of Chemical Engineering & Technology, IIT (BHU) Varanasi
12

MATRIX ALGEBRA

The + and operator add the corresponding elements of the


matrices, if their dimensions agree.

Normal matrix multiplication by the * operator.

To m u l t i p l y / d i v i d e t h e c o r r e s p o n d i n g e l e m e n t s o f t h e m a t r i c e s ,
precede the operator by a dot.
e.g. A = B.*C

Society of Chemical Engineers, Dept. of Chemical Engineering & Technology, IIT (BHU) Varanasi
13

TASK 3

Assign A as a 6*6 magic square . After this substitute all the


diagonal elements with 0 in a single statement. Getting the final
answer as :
A =
0

26

19

24

21

23

25

31

22

27

20

28

33

10

15

30

34

12

16

36

29

13

18

Society of Chemical Engineers, Dept. of Chemical Engineering & Technology, IIT (BHU) Varanasi
14

MATLAB SCRIPTS

Writing codes directly on command window isnt usually


convenient since you cannot change a statement after youve
p r e s s e d e n t e r.

Thus for any code which exceeds a few lines, we make


scripts on the editor , and execute them all at once.

This makes it easier to edit and debug the code.

M AT L A B e d i t o r c a n b e a c c e s s e d b y u s i n g t h e k e y w o r d e d i t .

Society of Chemical Engineers, Dept. of Chemical Engineering & Technology, IIT (BHU) Varanasi
15

THE .M FILES

After the script is edited, you need to save it.

The script will be saved in the form of xxxxx.m files in the


folder specified.

If you want to run the script, type the name of the script in the
c o m m a n d w i n d o w a n d p r e s s e n t e r.

Caution: Before running the script, make sure that the current
folder holds the desired file to be executed.
Also, you need to save the file whenever a change is
made.

Society of Chemical Engineers, Dept. of Chemical Engineering & Technology, IIT (BHU) Varanasi
16

CONTROL STATEMENTS IN MATLAB


1.

Conditional Statements

2.

If else end
switch case end

Looping Statements

for end
while end

Society of Chemical Engineers, Dept. of Chemical Engineering & Technology, IIT (BHU) Varanasi
17

CONDITIONAL STATEMENTS
If else end

Syntax:
if <condition>

SWITCH CASE END

Syntax:

Switch <x>

Statement 1;

Case a

statement 1;

else

Case b

statement 2;

Statement 2;

Case cstatement 3;

end

Otherwise

statement 4;

end

Society of Chemical Engineers, Dept. of Chemical Engineering & Technology, IIT (BHU) Varanasi
18

TASK 4

Find the greatest of 3 numbers.

Society of Chemical Engineers, Dept. of Chemical Engineering & Technology, IIT (BHU) Varanasi
19

LOOPING IN MATLAB

T h e 2 b a s i c l o o p s i n M AT L A B a r e
For loop
While loop

For loop is used when we know beforehand the number of


iterations, thus has reduced flexibility but has shorter code.

While loop is the generic form and although has slightly


l o n g e r c o d e b u t h a s m o r e f l e x i b i l i t y.

Society of Chemical Engineers, Dept. of Chemical Engineering & Technology, IIT (BHU) Varanasi
20

LOOPING STATEMENTS
For end

Syntax:

WHILE END

Syntax:

for <x> = a:b

while <condition>

Statement 1;

statement 1;

Statement 2;

statement 2;

end

end

Society of Chemical Engineers, Dept. of Chemical Engineering & Technology, IIT (BHU) Varanasi
21

TASK 5
A man is standing on the number 5 on the number line .
After every turn , the man doubles the integer he is on and
moves back 3 integers .

Continue the loop till the position crosses 100 th , store


his positions in a matrix and compute the number of
steps taken.

Now once he crosses 100th position, he decides to triple


the integer he is on and moves back the same integer of
his last position. Repeat the same problem as above
with max limit 2000.

Society of Chemical Engineers, Dept. of Chemical Engineering & Technology, IIT (BHU) Varanasi
22

PLOTTING IN MATLAB

We can use the plot function to plot x-vs-y data.


e . g . p l o t ( x , y, r o )

The first 2 parameters are the x and y matrices and the third
one is the line specification.

Caution: x & y should have the same dimensions.

More than one line can be plotted by just adding on the variables
like before.
e . g . p l o t ( a , b , r. , c , d , b o , e , f , g - )

Society of Chemical Engineers, Dept. of Chemical Engineering & Technology, IIT (BHU) Varanasi
23

TASK 6

Plot a sine wave of increasing magnitude along with its


limiting values as shown below:

Society of Chemical Engineers, Dept. of Chemical Engineering & Technology, IIT (BHU) Varanasi
24

FUNCTIONS

What a function basically does is


Ta k e i n a s e t o f i n p u t v a r i a b l e s

Process them
Either display an output or return a set of output

variables.

Society of Chemical Engineers, Dept. of Chemical Engineering & Technology, IIT (BHU) Varanasi
25

FUNCTION SYNTAX

Syntax for a user-defined function is as follows:


function [<return variables>] = <func_name> (<input arguments>)
statement 1;
statement 2;
statement 3;
end

Syntax for a function call:

[<returned variables>] = <func_name> (<input arguments>)

Society of Chemical Engineers, Dept. of Chemical Engineering & Technology, IIT (BHU) Varanasi
26

SOME KEY POINTS WHILE USING FUNCTIONS

The function is saved as a .m file and can be executed in the


same manner as a pre-defined one.
Caution: The name of the .m file should be exact same
as the name of the function

As with the case of the scripts, the current folder should


hold the function to be executed.

Use very high caution when dealing with the order of the
input and output arguments.

Society of Chemical Engineers, Dept. of Chemical Engineering & Technology, IIT (BHU) Varanasi
27

SOME PRE-DEFINED MATRIX MANIPULATION


FUNCTIONS FOR YOUR USE
det (A)

Determinant:

Length of 1D matrix:

Number of rows & columns:

Diagonal elements of a matrix:

Sum of the matrix elements:

length (A)
[ r, c ] = s i z e ( A )
diag (A)

sum (A,1) : Returns a row vector containing the sum of all the columns
of matrix A

sum (A,2) : Returns a column vector containing the sum of all the rows
of matrix A

Trace of matrix: trace (A)

Rank of matrix: rank (A)

Society of Chemical Engineers, Dept. of Chemical Engineering & Technology, IIT (BHU) Varanasi
28

Society of Chemical Engineers, Dept. of Chemical Engineering & Technology, IIT (BHU) Varanasi
29

Das könnte Ihnen auch gefallen