EP314_Lab1
description
Transcript of EP314_Lab1
-
EP314Process 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 alist of variablescreated by MATLAB.As you can see, thevalue 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
By double clicking on b, the variable editor window appears. You can type in new values into b by filling in the cells.
To clear text in Command Windows: clcTo clear all variables at workspace: clear allTo close all figure: close all
-
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 squarebracket, [ ].Create a matrix (1x10) named varA by entering in MATLABCommand 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 commandwindows. Keep in mind, variable name are case sensitive.
The easiest way to create a matrix (10x1) from varA is to take the transpose of varAvarB = 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 53 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 53 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 00 00 0
>> Z=zeros(3,2)
=1 1 11 1 1
>> K = ones(2, 3)
=0.8147 0.9134 0.27850.9058 0.6324 0.54690.1270 0.0975 0.9575
>> R = rand(3, 3)
-
Operator
-
+ Addition
- Subtraction
* Multiplication
/ Division
^ Power
Operator
+ Addition
- Subtraction
.* Multiplication
./ Division
.^ Power
Mathematical Mathematical Array
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*MAns1 =
4 810 2
>> Ans3 = Ans1*Ans2Ans3 =
76 5646 86
>> Ans2 = M + NAns2 =
3 88 3
>> Ans4 = sqrt(M)Ans4 =
1.4142 2.00002.2361 1.0000
-
+ Addition
- Subtraction
* Multiplication
/ Division
^ Power
Operator
+ Addition
- Subtraction
.* Multiplication
./ Division
.^ Power
Mathematical Mathematical Array
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>> ans4 = ans1*ans2
Multiplies each element M(I,j)*N(I,j)
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.^3ans1 =
8 64125 1
>> ans3 = ans1.*ans2ans3 =
864 691216875 81
>> ans2 = M^3ans2 =
108 108135 81
>> ans4 = ans1*ans2ans4 =
9504 604813635 13581
-
Plotting graphs
plot(x,y) Plot the vector x versus the vector y
semilogx(x,y) Plot the vector x versus the vector yThe x-axis is log10, y-axis is linear
semilogy(x,y) Plot the vector x versus the vector yThe x-axis is linear, y-axis is log10
loglog(x,y) Plot the vector x versus the vector yBoth are log10 axes.
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
Available Plot Formats
Function for Customized Plots
-
Typical Plot
-
Plotting graphsStep 1: Prepare dataset
>> 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
x = [xi dx xf]
Final value
increment
Starting value