Mod&Sim Matlab Lect1 Introduction Scalars
Mod&Sim Matlab Lect1 Introduction Scalars
Lecture 1
Introduction to Matlab
Scalars in Matlab
2
MatLab part:
3
MatLab part:
Week Topics
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
Ex:
>> a=[1 2]
the output of the command
a=
12
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.
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:
Note: “,” or space passes its next value to a new column But “;” passes its next value
to a new row
14
The following rules must be taken into consideration when a script file is
named:
15
Remember Matlab Keywords and reserved words are not allowed to be used
as file names
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:
17
You can add a comment to Matlab code by inserting a percentage sign
“%” at the beginning of the line. For example:
18
No section mode Section mode enable you to divide your code into
some sections that can be executed individually
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.
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
26
Rounds up a real number toward the nearest higher 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:
>>
>>
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:
result
33
Logical and rational operators can be combined. For example:
34
or
36
37
38