You are on page 1of 20

EP314

Process Dynamics and Control


Laboratory Session : #1

Software Interface

Command window: Type your


instructions here and press
ENTER to execute them.

Workspace:
shows a list of
variables created
by MATLAB.

Software Interface

Example: Declare a variable, a


with value of 1

Software Interface

Workspace: shows a
list of variables
created by MATLAB.
As you can see, the
value of a is shown.

Software Interface

Another way to create


a variable Is to press
this button.
MATLAB will prompt
you to enter the
variable name.

Software Interface

By double
clicking on b, the
variable editor
window appears.
You can type in
new values into b
by filling in the
cells.

Software Interface

To clear text in Command


Windows: clc
To clear all variables at
workspace: clear all
To close all figure: close all

By double
clicking on b, the
variable editor
window appears.
You can type in
new values into b
by filling in the
cells.

Declaring & Manipulating variables


MATLAB can be used to initialize and
manipulate many types of variables.
Single value i.e.: a =1;
Matrix i.e.: A = [1;2] ; %(2x1) matrix
String i.e.: name = John;
Try It Yourself !!!

Create a matrix (3x1) named var1 with the values of


1, 3, 5 in it.
Create a matrix (1x7) named var2 with the values of
1, 3, 5, 6, 8, 10, 11 in it.

Declaring & Manipulating variables


Matrix can be created by enclosing a set of number in square
bracket, [ ].
Create a matrix (1x10) named varA by entering in MATLAB
Command Windows as followed:
varA = [1 2 3 4 5 6 7 8 9 10] OR varA = [1 2 3 4 5 6 7 8 9 10] ;
varA = [1,2,3,4,5,6,7,8,9,10]

OR

varA = [1,2,3,4,5,6,7,8,9,10] ;

Note: semicolon at the end is to suppress the result at command


windows. Keep in mind, variable name are case sensitive.
The easiest way to create a matrix (10x1) from varA is to take the transpose of varA
varB = varA;
OR
varB = [1; 2; 3; 4; 5; 6; 7; 8; 9; 10];

Try It Yourself !!!

Create a matrix (3x3)



=

Accessing Matrix Values


To access a specific value inside a matrix, use this command:

matrixName(rowNumber, colNumber)
Example: Given a matrix, M as
>> M = [1,2,3,4,5;3,4,5,6,7]
M=
1 2 3 4 5
3 4 5 6 7
Determine value for 2nd row and 4th column
>> ans = M(2,4)
ans =
6

Whole Columns and Rows


To access a specific value inside a matrix, use this command:

matrixName(rowNumber, colNumber)
Use

: to indicate all columns or rows.

Example: Given a matrix, M as

>> M = [1,2,3,4,5;3,4,5,6,7]
M=
1 2 3 4 5
3 4 5 6 7
Determine values for 2nd row
>> ans = M(2,:)
ans =
3 4 5 6 7

Matrix Zeroes, Ones, & Random


To create the matrix as below, use this command:

Zeroes zeros(rowNumber, colNumber)


Ones
ones(rowNumber, colNumber)
Random rand(rowNumber, colNumber)
Example:
0
= 0
0
=

1
1

0
0
0

>> Z=zeros(3,2)

1 1
1 1

>> K = ones(2, 3)

0.8147 0.9134 0.2785


= 0.9058 0.6324 0.5469
0.1270 0.0975 0.9575

>> R = rand(3, 3)

Operator

Operator
Mathematical

Mathematical Array

Addition

Addition

Subtraction

Subtraction

Multiplication

.* Multiplication

Division

./ Division

Power

.^ Power

Now, we do simple mathematical operation with matrix, M and N


Given: >>M=[2,4;5,1]; N=[1,4;3,2];
Compute as follows:
>> Ans1 = 2*M
>> Ans2 = M + N
>> Ans3 = Ans1*Ans2
>> Ans4 = sqrt(M)

Now, we do simple mathematical operation with matrix, M and N

Given: >>M=[2,4;5,1]; N=[1,4;3,2];


Compute as follows:
>> Ans1 = 2*M
Ans1 =
4 8
10 2

>> Ans3 = Ans1*Ans2


Ans3 =
76 56
46 86

>> Ans2 = M + N
Ans2 =
3 8
8 3

>> Ans4 = sqrt(M)


Ans4 =
1.4142 2.0000
2.2361 1.0000

Operator
Mathematical

Mathematical Array

Addition

Addition

Subtraction

Subtraction

Multiplication

.* Multiplication

Division

./ Division

Power

.^ Power

Now, Mathematical Array operation with matrix, M and N


Given: >>M=[2,4;5,1]; N=[1,4;3,2];
>> ans1 = M.^3
>> ans2 = M^3
>> ans3 = ans1.*ans2 Multiplies each element M(I,j)*N(I,j)
>> ans4 = ans1*ans2 Cross matrix multiplication

Now, Mathematical Array operation with matrix, M and N

Given: >>M=[2,4;5,1]; N=[1,4;3,2];


>> ans1 = M.^3
ans1 =
8 64
125 1

>> ans3 = ans1.*ans2


ans3 =
864
6912
16875
81

>> ans2 = M^3


ans2 =
108 108
135 81

>> ans4 = ans1*ans2


ans4 =
9504
6048
13635
13581

Plotting graphs
Available Plot Formats
plot(x,y)

Plot the vector x versus the vector y

semilogx(x,y)

Plot the vector x versus the vector y


The x-axis is log10, y-axis is linear

semilogy(x,y)

Plot the vector x versus the vector y


The x-axis is linear, y-axis is log10

loglog(x,y)

Plot the vector x versus the vector y


Both are log10 axes.

Function for Customized Plots


title(Text)

Put Text at the top of the plot

xlabel(TextX)

Label the x-axis TextX

ylabel(TextY)

Label the y-axis TextY

subplot(row,col,no)

Subdivides the plot in the Figure

grid

Draws grid line

legend(M,N,K)

Add legend for M, N, & K profiles

Typical Plot

Step 1: Prepare dataset

Plotting graphs

>> t = 1:0.5:10;
>> y1 = 2*t.^2 + 2*t + 9;
>> y2 = 3*t.^2 + 0.6*t + 0.1;
Step 2: Now create plot
y1 vs t, and y2 vs t together

>> plot(t,y1,b)
>> plot(t,y2,g)
Step 3: Add title and axis label
>> title(Quadratic plots)
>> xlabel(The x-axis label, t)
>> ylabel(The y-axis label, y)

Step 4: Add legend and grid


>> legend(y1,y2)
>> grid

Starting value

Final value

x = [xi dx xf ]
increment

You might also like