CCE402: Modeling and Simulation
Lecture 1
Introduction to Matlab
Scalars in Matlab
Dr. Sawsan Abdellatif 1
Title Modeling and Simulations
Lecturers Dr. Sawsan Abdellatif
[Link] Hossam
Teaching assistant (TA) Eng. Mostafa Mokhtar
Eng. Shrouq Zain
Software packages Matlab/Simulink
Labview
Assessment (100) 1. Final Term Exam (40)
2. Course work (60)
2
MatLab part:
1) Gdeisat, Munther, and Francis Lilley, “MATLAB® by Example:
Programming Basics”. Newnes, 2013.
2) Hahn, Brian, and Daniel Valentine, “Essential MATLAB for engineers
and scientists”. Academic Press, 2013.
3) Attaway, Stormy, “Matlab: a practical introduction to programming and
problem solving, Butterworth-Heinemann, 2013.
3
MatLab part:
Week Topics
Introduction to Matlab software
1
Scalars in matlab
2 Vectors in Matlab.
Arrays in Matlab
3
Functions in Matlab
Conditional statements in Matlab
4 Loop statements in Matlab
Matlab Vectorization
Some Applications on Matlab
5 Symbolic toolbox
Structures and Cell Arrays in Matlab
6
Matlab Debugging
4
Matrix Laboratory
Matlab is programming language and environment for
scientific computing that is centered on matrices
Common Uses of Matlab in Research
Data Acquisition
Multi-format data importing
Analysis tools
Statistics
Graphing
Modeling
5
6
1) The Command Window
The command window allows you to type commands directly and
see the results immediately.
Ex:
>> a=[1 2]
the output of the command
a=
12
2) The Editor Window (m-file)
The Editor Window is a word processor specifically designed for
Matlab commands.
7
The help is built into Matlab and can be accessed from the help
menu.
To get help on a command, type "doc commandname" in
Command Window where commandname is the command of
interest.
Ex:
>> doc plot
8
Matlab is a short name for Matrix laboratory.
So, Matlab is a matrix-based software package. It considers the
scalar variable to be a 1X1 matrix.
A scalar here means a number such as “2” or “ - 100”
Command prompt Scalar variable Semicolon “;”
is used to direct
Matlab not to display
the value of the
variable x in the
Command Window.
9
Note the changes that happened in the
Command Window, the Command
History, and the Workspace windows.
10
Type the Matlab Command:
This creates Row vector with the values indicated in the command
You can draw the vector y if you Right Click on the y variable in the
Workspace and press plot(y). (Check the results)
11
Type the Matlab Command:
This creates Column vector with the values indicated in the command
Note using space or comma “,” for creating Row vector and using semicolon
“;” for creating Column vector
12
Type a Matlab Command:
This creates an array variable with the following values:
Note: “,” or space passes its next value to a new column But “;” passes its next value
to a new row
Right Click on the Z variable in the Workspace and press mesh(Z).
(Check the results)
13
An M-file is a text file that contains a collection of commands that Matlab
executes in a sequential order.
A script file has the following properties:
The commands executed in the script file have the same effect as if
these commands were executed in the Command Window.
The variables created by the script file are displayed in the
Workspace window.
Create a script file that contain the following commands:
14
The following rules must be taken into consideration when a script file is
named:
Remember: It is very helpful to use meaningful and descriptive names
15
Remember Matlab Keywords and reserved words are not allowed to be used
as file names
Matlab reserved words examples Matlab Keywords examples
16
To check that the file name you have chosen is not a Matlab keyword or a
Matlab function, you can use Matlab help.
Example: If you want to name your file “cat.m”, write the following
command on the command window:
Matlab responds and informs you that there
is already a function called “cat” that
concatenates arrays
You can change the name to “cat1.m”,
17
You can add a comment to Matlab code by inserting a percentage sign
“%” at the beginning of the line. For example:
A better method for commenting multiple lines of the code is performed
by using block commenting.
In this method, add the characters “%{” before the first line of the
comments and add the characters “%}” after the last line of the
comments.
18
No section mode Section mode enable you to divide your code into
some sections that can be executed individually
Two ways to create new section:
Right Click in the m-file and
press “Insert Section”.
Type “%%” and space.
To run certain section,
select this section in the
m-file and press “Run
Section”
19
20
21
In Matlab, every variable created should have a value.
Variables are created either by Matlab or by the user.
Variables created by Matlab are considered to be special
variables, whose values are assigned by Matlab.
Then press Enter. Matlab responds with
This command generates another special variable “ans” and assigns the
value 3.1416 to it.
The special variable “ans” saves the result of any Matlab operation if the
value of the result is not specifically assigned to a variable.
22
Other examples of special variables are i and j. The value for both
variables is defined as −1.
To get some help about the variable i
23
To restore the value of the special variable pi,
24
Matlab is Case sensitive
25
Matlab supports four functions to approximate real numbers:
round, fix, ceil, floor
This function rounds a real number upward, or downward, toward the
nearest integer.
This function truncates (eliminates) the decimal part of a real number,
leaving the integer part unchanged.
26
Rounds up a real number toward the nearest higher integer
Rounds up a real number toward the nearest lower integer
27
“fix” and “floor” functions give similar results for positive numbers.
But, they give different results for negative numbers
28
Matlab evaluates mathematical expressions from left to right.
Mathematical expressions may contain addition, subtraction,
multiplication, division, and exponential mathematical operations as well
as parentheses.
These mathematical operations are evaluated in the following order in
Matlab:
>> a=2 - 2^3*2+2 a=-12
>> b=2/3*4^2 b=10.6667
>> c=1+2/3*4+5 c= 8.6667 29
>>
The addition operation needs to be evaluated first followed by the division.
Since the division operation has a higher priority in Matlab than the addition
operation, parentheses are needed to alter this priority order to give the addition
operation a higher priority than that of the division operation.
>>
>>
30
Any variable with a logical class has a value of either true or false.
Matlab represents true as 1, and false as 0.
31
Relational operators require two operands, and they compare two values.
The relational operators produce variables with a logical class.
32
Matlab has three logical operators which are:
The logical operators produce variables with the
logical class.
result
33
Logical and rational operators can be combined. For example:
34
or
Be careful not to use i and j as
variable names. This may
cause unexpected errors in the
use of complex numbers. 35
Note that the angle is given here in radians. To convert the angle from
radians to degrees, multiply it by 180/𝜋 (or using rad2deg command)
36
37
38