Professional Documents
Culture Documents
212 EEE 2000 Labsheet 01
212 EEE 2000 Labsheet 01
Lab Sheet 1
Introduction to MATLAB Environment, Syntax,
Operators, Data Types, and Simple Mathematical
Operations
Prerequisite
Before starting this lab, students should know:
1. a fundamental knowledge on Matrix Algebra.
2. basic typing skills.
Outcome
After finishing this lab, students should be able to:
1. get familiarized about the MATLAB environment.
2. examine various MATLAB desktop and editing features.
3. use some simple MATLAB commands from the Command Window.
Outline of this lab
1. Lab Session 1.0: Introduction
2. Lab Session 1.1: Familiarization of the MATLAB Environment
3. Lab Session 1.2: Working on the Command Window
4. Lab Session 1.3: Real Life Problems
5. Lab Session 1.4: In Lab Evaluation
6. Home Work
Workspace
Current View
Directory Command window
program
View like a calculator
variables
and many more
folders
and m
files
First OBE version, prepared by: B.K.M Mizanur Rahman, Assistant Professor, Department of EEE, UIU
Updated on Summer 2021, by Dr. Sadid Muneer, Assistant Professor, Department of EEE, UIU
2|Page
This shows previously executed commands. Commands can be re-executed
History Window
by double-clicking.
First OBE version, prepared by: B.K.M Mizanur Rahman, Assistant Professor, Department of EEE, UIU
Updated on Summer 2021, by Dr. Sadid Muneer, Assistant Professor, Department of EEE, UIU
3|Page
MATLAB immediately adds variable a to the workspace and displays the result in the
Command Window.
a=
1
When you do not specify an output variable, MATLAB uses the variable ans, short for answer,
to store the results of your calculation.
sin(a)
ans =
0.8415
The value of ans changes with every command that returns an output value that is not assigned
to a variable.
If you end a statement with a semicolon, MATLAB performs the computation, but suppresses
the display of output in the Command Window.
b = 2;
B. Entering Multiple Statements in Command Window
To enter multiple statements on multiple lines before running any of the statements, use Shift+Enter
between statements. This action is unnecessary when you enter a paired keyword statement on
multiple lines, such as for and end.
You also can enter more than one statement on the same line by separating statements. To
distinguish between commands, end each one with a comma or semicolon. Commands that end with
a comma display their results, while commands that end with a semicolon do not. For example, enter
the following three statements at the command line:
A = magic(5), B = ones(5) * 4.7; C = A./B
A=
17 24 1 8 15
23 5 7 14 16
4 6 13 20 22
10 12 19 21 3
11 18 25 2 9
C=
3.6170 5.1064 0.2128 1.7021 3.1915
4.8936 1.0638 1.4894 2.9787 3.4043
0.8511 1.2766 2.7660 4.2553 4.6809
2.1277 2.5532 4.0426 4.4681 0.6383
2.3404 3.8298 5.3191 0.4255 1.9149
MATLAB displays only the values of A and C in the Command Window.
Why is B not displayed?
C. Variable Naming Rules
First OBE version, prepared by: B.K.M Mizanur Rahman, Assistant Professor, Department of EEE, UIU
Updated on Summer 2021, by Dr. Sadid Muneer, Assistant Professor, Department of EEE, UIU
4|Page
Must begin with letter of the alphabet. >> myvar = 4 Correct.
1. After the first character, it can contain >> 2num = 6 Incorrect.
special characters or numbers. >> rad23 = 2.3e3 Correct.
There is a limit to the length of a
variable name. Typically, ≤ 64 >> namelengthmax
Maximum length of a
2. characters. ans =
variable name is 63.
(Variable name should be as minimum 63
as possible but it should be meaningful)
>> mynum = 4
MATLAB is case-sensitive, so All are correct but have
3. >> MYNUM = 6
capitalization matters different meanings.
>> myNum = 8
Works, but should be
>> my_num = 5
avoided.
4. Avoid underscores. Use mixed case.
Much safer, still easy to
>> myNum = 5
read.
Certain words are reserved and cannot >> for = 4 This is not allowed, for is
5.
be used as variable names. a keyword.
Note:
Write
>> iskeyword
in command prompt. It will give the following output, means that the following are key word
and cannot be used as variable names.
Ans =
‘break’
‘case’
‘catch’
‘classdef’
‘continue’
‘else’
‘elseif’
‘end’
‘for’
‘function’
‘global’
‘if’
‘otherwise’
‘parfor’
‘persistent’
‘return’
‘spmd’
First OBE version, prepared by: B.K.M Mizanur Rahman, Assistant Professor, Department of EEE, UIU
Updated on Summer 2021, by Dr. Sadid Muneer, Assistant Professor, Department of EEE, UIU
5|Page
‘switch’
‘try’
‘while’
Technically allowed but is
Names of built-in functions should not
6. >> sin = 2 bad programming style &
be used as variable names.
may cause problems.
temperatureInTopek
Variable names should be mnemonic & aKansasAug19Recor
7. Too long!
short. dedByChrisSmithTan
nerTheSecond = 2
G. Predefined Variables
First OBE version, prepared by: B.K.M Mizanur Rahman, Assistant Professor, Department of EEE, UIU
Updated on Summer 2021, by Dr. Sadid Muneer, Assistant Professor, Department of EEE, UIU
6|Page
Several frequently used variables are already defined when MATLAB is started. Some of the
predefined variables are:
Predefined
Meaning
Variables
A variable that has the value of the last expression that has not been assigned to a
ans specific variable. If the user does not assign the value of an expression to a variable,
MATLAB automatically stores the result in ans.
Pi Value of the number 𝜋.
eps The smallest difference between two numbers. Its value is 2.2204e-016.
Inf Used for infinity.
i or j Defined as √−1
Stands for Not-a-Number. It is used when MATLAB cannot determine a valid
NaN
numeric value. As an example, 0/0.
H. Complex Numbers
MATLAB also supports complex numbers. The imaginary number is denoted with the symbol i or j,
assuming that you did not use these symbols anywhere in your program (that is very important!). Try
the following:
Expression Meaning
>> z =3 + 4i Assigns a complex number to variable z. Note that you do not need the ‘*’ after 4.
>> conj(z) Computes the conjugate of z.
>> angle(z) Computes the phase of z.
>> real(z) Computes the real part of z.
>> imag(z) Computes the imaginary part of z.
>> abs(z) Computes the magnitude of z.
I. Random Numbers
MATLAB can generate pseudo-random numbers which are useful for creating synthetic data or adding
noise.
Command Meaning
>> rand Gives a random floating point number between 0-1.
>> round(rand*10) To get random integers use “round”, rand integers from 0-10.
WARNING: rand will always give the exact same random number when
>> rng(‘shuffle’)
you first start. Use rng first to prevent this.
First OBE version, prepared by: B.K.M Mizanur Rahman, Assistant Professor, Department of EEE, UIU
Updated on Summer 2021, by Dr. Sadid Muneer, Assistant Professor, Department of EEE, UIU
7|Page
ans =
4
>> exp(3)
exp(x) Exponential (𝒆𝒙 ) ans =
20.0855
>> abs(-4)
ans =
4
abs(x) Absolute value
>> abs(3+4j)
ans =
5
>> log(1000)
log(x) Natural logarithm (Base e) ans =
6.9078
>> log10(1000)
log10(x) Base 10 logarithm ans =
3
>> factorial(5)
The factorial function x!
factorial(x) ans =
(x must be a positive integer)
120
>> sin(pi/6)
sin(x) Sine of angle x (x in radians) ans =
0.5000
>> cos(pi/4)
cos(x) cosine of angle x (x in radians) ans =
0.7071
>> tan(pi/3)
tan(x) Tangent of angle x (x in radians) ans =
1.7321
>> tand(60)
tand(x) Tangent of angle x (x in degree) ans =
1.7321
>> asin(sqrt(3)/2)
ans =
asin(x) Inverse sine of x
1.0472
(Note that result is in radians)
>> round(17/5)
round(x) Round to the nearest integer ans =
3
>> ceil(11/5)
ceil(x) Round towards infinity
ans =
First OBE version, prepared by: B.K.M Mizanur Rahman, Assistant Professor, Department of EEE, UIU
Updated on Summer 2021, by Dr. Sadid Muneer, Assistant Professor, Department of EEE, UIU
8|Page
3
>> floor(9/4)
floor(x) Round towards minus infinity ans =
2
>> rem(13,5)
Returns the remainder after x is
rem(x,y) ans =
divided by y
3
Signum function >> sign(-5)
sign(x) Returns 1 if x > 0 ans =
Returns -1 if x < 0 -1
First OBE version, prepared by: B.K.M Mizanur Rahman, Assistant Professor, Department of EEE, UIU
Updated on Summer 2021, by Dr. Sadid Muneer, Assistant Professor, Department of EEE, UIU
9|Page
The larger the number in the type name, the larger the number that can be stored in it. We will,
for the most part, use the type int32 when an integer type is required.
The type char is used to store either single characters (e.g., ‘x’) or strings, which are sequences
of characters (e.g., ‘cat’). Both characters and strings are enclosed in single quotes. The type logical
is used to store true/false values. Variables that have been created in the Command Window can be
seen in the Workspace Window. In that window, for every variable, the variable name, value, and
class (which is, essentially, its type) can be seen. Other attributes of variables can also be seen in the
Workspace Window. Which attributes are visible by default depends on the version of MATLAB.
However, when the Workspace Window is chosen, clicking on the down arrow allows the user
to choose which attributes will be displayed by modifying Choose Columns. By default, numbers are
stored as the type double in MATLAB. There are, however, many functions that convert values from
one type to another. The names of these functions are the same as the names of the types shown in this
section. These names can be used as functions to convert a value to that type. This is called casting
the value to a different type, or type casting. For example, to convert a value from the type double,
which is the default, to the type int32, the function int32 would be used. Entering the assignment
statement
>> val = 6 +3;
would result in the number 9 being stored in the variable val, with the default type of double, which
can be seen in the Workspace Window. Subsequently, the assignment statement
>> val = int32(val);
would change the type of the variable to int32, but would not change its value.
Here is another example using two different variables.
>> num=6+3
num =
9
>> whos
Name Size Bytes Class Attributes
Note that whos shows the type (class) of the variables, as well as the number of bytes used to
store the value of a variable. One byte is equivalent to eight bits, so the type int32 uses four bytes. The
function class can also be used to see the type of a variable.
One reason for using an integer type for a variable is to save space in memory.
M. Numerical Expressions
Expressions can be created using values, variables that have already been created, operators, built-in
functions, and parentheses. For numbers, these can include operators, such as multiplication, and
functions, such as trigonometric functions.
Sample Example-1
>> a=1;
First OBE version, prepared by: B.K.M Mizanur Rahman, Assistant Professor, Department of EEE, UIU
Updated on Summer 2021, by Dr. Sadid Muneer, Assistant Professor, Department of EEE, UIU
10 | P a g e
>> b=2;
>> c=4;
>> d=5;
>> x= a^3+sqrt(b*d)-4*c
x=
-11.8377
Sample Example-2
>> a=2;
>> b=-3;
>> c=4;
>> x=5;
>> y=a*x^2+b*x+c
y=
39
Note the semicolon after each variable assignment. If you omit the semicolon, then MATLAB
echoes back on the screen the variable value.
First OBE version, prepared by: B.K.M Mizanur Rahman, Assistant Professor, Department of EEE, UIU
Updated on Summer 2021, by Dr. Sadid Muneer, Assistant Professor, Department of EEE, UIU
11 | P a g e
3.00
>> format compact
>> b=7 There is no spacing between command
>> format compact
b= and output.
7.00
Long expressions can be continued on
the next line by typing three (or more)
>> 3 +55 - 62 + 4 - 5…
periods, which is the continuation
+22-1
operator, or the ellipsis. To do this, type
Ellipsis ans =
part of the expression followed by an
16
ellipsis, then hit the Enter key and
continue typing the expression on the
next line.
First OBE version, prepared by: B.K.M Mizanur Rahman, Assistant Professor, Department of EEE, UIU
Updated on Summer 2021, by Dr. Sadid Muneer, Assistant Professor, Department of EEE, UIU
12 | P a g e
Solution:
Code:
a=16; b=11;
C=pi*(3*(a+b)-sqrt((3*a+b)*(a+3*b)))
Command Window:
C=
85.5518
Example 1.2:
The resonant frequency 𝑓(in Hz) for the circuit shown is given by,
1 1 𝑅 2
𝑓= √ −( )
2𝜋 𝐿𝐶 𝐿
Calculate the resonant frequency when, L = 0.15 H, R = 14 Ω and C = 2.6×10-6 F.
Solution:
Code:
L=0.15; R=14; C=2.6e-6;
f=1/(2*pi)*sqrt(1/(L*C)-(R/L)^2)
Command Window:
f=
254.4186
First OBE version, prepared by: B.K.M Mizanur Rahman, Assistant Professor, Department of EEE, UIU
Updated on Summer 2021, by Dr. Sadid Muneer, Assistant Professor, Department of EEE, UIU
13 | P a g e
Lab Session 1.4: In Lab Evaluation
In this section, students will solve the following problem(s) during lab hour
by themselves.
ILE 01: After t seconds of switching, the voltage Vc is given by:
𝑉𝑐 = 𝑉0 (1 − 𝑒 −𝑡/𝑅𝐶 )
Given that Vo = 36 V, R = 2500 Ω and C=1000µF. Calculate the current in the circuit 8 sec after the
switch is closed.
Home Work:
HW 01 An object with an initial temperature of T0 that is placed at time t = 0 inside a chamber
that has a constant temperature of T3 will experience a temperature change according to
the equation:
𝑇 = 𝑇3 + (𝑇0 − 𝑇3 )𝑒 −𝑘𝑡
where T is the temperature of the object at time t, and k is a constant. A soda can at a
temperature of 120 °F (after being left in the car) is placed inside a refrigerator where the
temperature is 38 °F. Determine, to the nearest degree, the temperature of the can after
three hours. Assume k = 0.45 hr-1. First define all the variables and then calculate the
temperature using one MATLAB command.
HW 02 The arc length of a segment of a parabola ABC of an ellipse with semi-minor axes a and
b is given approximately as,
1 𝑏2 4𝑎 + √𝑏 2 + 16𝑎2
𝐿𝐴𝐵𝐶 = √𝑏 2 + 16𝑎2 + ln ( )
2 8𝑎 𝑏
Determine LABC if a = 11 cm and b = 9 cm.
First OBE version, prepared by: B.K.M Mizanur Rahman, Assistant Professor, Department of EEE, UIU
Updated on Summer 2021, by Dr. Sadid Muneer, Assistant Professor, Department of EEE, UIU
14 | P a g e
First OBE version, prepared by: B.K.M Mizanur Rahman, Assistant Professor, Department of EEE, UIU
Updated on Summer 2021, by Dr. Sadid Muneer, Assistant Professor, Department of EEE, UIU
15 | P a g e