Fundamentals of
MATLAB
Yonis Khlif Elmi, Ph.D
ARRAYS: VECTORS AND MATRICES
▪ As mentioned in Lecture 1, the name MATLAB stands for Matrix Laboratory
because MATLAB has been designed to work with matrices. A matrix is a
rectangular object (e.g., a table) consisting of rows and columns. We will postpone
most of the details of proper matrices and how MATLAB works with them until
Lecture 6.
▪ A vector is a special type of matrix, having only one row or one column. Vectors
are called lists or arrays in other programming languages. If you haven’t come
across vectors officially yet, don’t worry—just think of them as lists of numbers.
▪ MATLAB handles vectors and matrices in the same way, but since vectors are
easier to think about than matrices, we will look at them first. This will enhance
your understanding and appreciation of many aspects of MATLAB. As mentioned
above, MATLAB refers to scalars, vectors, and matrices generally as arrays.We
will also use the term array generally, with vector and matrix referring to the one-
dimensional (1D) and two-dimensional (2D) array forms.
Signal and System MATLAB
▪ Initializing vectors: Explicit lists
x = [1 3 0 -1 5]
Remember the following important rules:
✓Elements in the list must be enclosed in square brackets, not parentheses.
✓Elements in the list must be separated either by spaces or by commas.
▪ Initializing vectors: The colon operator
x = 1:10 x = 1:0.5:4 x = 10:-1:1
x = 1:2:6 x = 0:-2:-5 x = 1:0
1+1:5 1+[1:5]
Signal and System MATLAB
▪ The linspace and logspace functions
▪ The function linspace can be used to initialize a vector of equally spaced
values:
linspace(x, y, n)
linspace(0,11 , 5)
logspace(1, 5, 5) ---------- Power
▪ Transposing vectors
x = 1:4 = 1 2 3 4 1:5’
x‘ = 1 [1:5]‘
2
3
4
Signal and System MATLAB
▪ Subscripts
▪ We can refer to particular elements of a vector by means of subscripts. Try the
following:
1. Enter r = rand(1,7). This gives you a row vector of seven random numbers.
2. Enter r(3). This will display the third element of r. The numeral 3 is the
subscript.
3. Enter r(2:4). This should give you the second, third, and fourth elements.
4. What about r(1:2:7) and r([1 7 2 6])?
5. Use an empty vector to remove elements from a vector:
r([1 7 2]) = [ ]
This will remove elements 1, 7, and 2.
Signal and System MATLAB
▪ Matrices
▪ A matrix may be thought of as a table consisting of rows and columns. You
create a matrix just as you do a vector, except that a semicolon is used to
indicate the end of a row. For example, the statement
a = [1 2 3; 4 5 6]
▪ results in a=
123
456
▪ A matrix can be constructed from column vectors of the same length. Thus, the
statements
x = 0:30:180;
table = [x’ sin(x*pi/180)’]
Signal and System MATLAB
▪ Structure plan
▪ Suppose we want to write a script to convert a temperature on the Fahrenheit scale
(where water freezes and boils at 32◦ and 212◦, respectively) to the Celsius scale. A
first-level structure plan might be a simple statement of the problem:
1. Initialize Fahrenheit temperature
2. Calculate and display Celsius temperature
3. Stop
▪ Step 1 is pretty straightforward. Step 2 needs elaborating, so the second-level plan
could be something like this:
1. Initialize Fahrenheit temperature (F)
2. Calculate Celsius temperature (C) as follows: Subtract 32 from F and multiply
by 5/9
3. Display the value of C 4. Stop
Signal and System MATLAB
VERTICAL MOTION UNDER GRAVITY
▪ If a stone is thrown vertically upward with an initial speed u, its vertical
displacement s after an elapsed time t is given by the formula s = ut − gt2/2,
▪ where g is the acceleration due to gravity. Air resistance is ignored.
▪ We would like to compute the value of s over a period of about 12.3 seconds at
intervals of 0.1 seconds, and plot the distance–time graph over this period, as
shown in below.
Signal and System MATLAB
OPERATORS, EXPRESSIONS, AND STATEMENTS
▪ Data types
▪ MATLAB has more than a dozen fundamental data types (or classes). The default
numeric data type is double precision; all MATLAB computations are in double
precision. More information on data types can be found in the Help index.
Operation Algebraic form MATLAB
Addition a+b a+b
Subtraction a–b a–b
Multiplication a×b a*b
Right division a/b a/b
Left division a\b a\b
Power ab a^b
Signal and System MATLAB
Precedence Operator
• 1+2*3
1 Parentheses (round brackets)
• 4/2*2
• 1+2/4
2 Power, left to right • 1+2\4
• 2*2^3
3 Multiplication and division, left to • 2*3\3
right • 2 ^ (1 + 2)/3
• 1/2e-1
4 Addition and subtraction, left to right
Signal and System MATLAB
OUTPUT
There are two straightforward ways of getting output from MATLAB:
▪ Entering a variable name, assignment, or expression on the command line, without
a semicolon
▪ Using the disp statement (e.g., disp( x ))
▪ To display a message and a value on the same line, use the following trick:
x = 2;
disp( [’The answer is ’, num2str(x)] );
▪ The output should be
The answer is 2
▪ You can display more than one number on a line as follows: disp( [x y z] )
Signal and System MATLAB
Flow Chart
Start
IF
False
Logical
Expiration
True
Statement
End
Signal and System MATLAB
Start
IF
Logical
Expiration
Relational Operator Meaning
True
< Less than.
<= Less than or equal to.
Statement
> Greater than.
>= Greater than or equal to.
End == Equal to.
~= Not equal to.
Signal and System MATLAB
Start
IF
False
Logical
Expiration
True
Statement
End
Signal and System MATLAB
65
IF
False
Grade >= 60?
True
Pass
End
Signal and System MATLAB
Start
ELSE
False
Logical
Expiration
True
Statement 1 Statement 2
End
Signal and System MATLAB
Start
ELSE
False
Grade >= 60?
True
Pass Fail
End
Signal and System MATLAB
59
ELSE
False
Grade >= 60?
True
Pass Fail
End
Signal and System MATLAB
ELSE-IF
Start
Logical False Logical False Logical False
Expiration Expiration Expiration
1 2 3
True True True
Statement 1 Statement 2 Statement 3 Statement 4
End
Signal and System MATLAB
ELSE-IF
Start
False False False
Grade >= 80? Grade >= 65? Grade >= 50?
True True True
A B C D
End
Signal and System MATLAB
ELSE-IF
Grade = 52
False False False
Grade >= 80? Grade >= 65? Grade >= 50?
True True True
A B C D
End
Signal and System MATLAB
The if Statement
Start
If Logical expiration(s)
Statement(s) False
Logical
end expiration
True
Statements
End
Signal and System MATLAB
The if Statement
Start
IF If Logical expiration(s)
Statement(s) False
Logical
end expiration
Relational Operator Meaning
True
< Less than.
<= Less than or equal to.
Statements
> Greater than.
>= Greater than or equal to.
== Equal to. End
~= Not equal to.
Signal and System MATLAB
65
IF
False
Grade >= 60?
True
Pass
End
Signal and System MATLAB
The else Statement
If Logical expiration(s) Start
Statement 1
else
False
Statement 2 Logical
Expiration
end
True
Statement 1 Statement 2
End
Signal and System MATLAB
59
ELSE
False
Grade >= 60?
True
Pass Fail
End
Signal and System MATLAB
The elseif Statement
Start
If Logical expiration 1
Statement 1
elseif Logical expiration 2 Logical False Logical False
Statement 2 Expiration Expiration
1 2
Else
Statement 3
end True True
Statement 1 Statement 2 Statement 3
End
Signal and System MATLAB
ELSE-IF
Grade = 52
False False False
Grade >= 80? Grade >= 65? Grade >= 50?
True True True
A B C D
End
Signal and System MATLAB
Introduction of Function
▪ function [output_args] = untitled(input_args)
%UNTITLED Summary of this function goes here
% Detailed explanation goes here
▪ end
Signal and System MATLAB
Introduction of Function
▪ Use the Pythagorean Theorem to calculate the hypotenuse of a right
triangle when the length of the other two sides are known.
A c
Signal and System MATLAB
Introduction of Function
▪ Use the Pythagorean Theorem to calculate the hypotenuse of a right
triangle when the length of the other two sides are known.
A c Input arguments
• a
• b
b Output arguments
• c
Signal and System MATLAB
Introduction of Function
▪ Calculate the area and volume of a cylinder when the radius and height are
known.
Signal and System MATLAB
Introduction of Function
▪ Calculate the area and volume of a cylinder when the radius and height are
known.
Input arguments
h
• r
• h
Output arguments
• A
• V
Signal and System MATLAB
REPEATING WITH for
▪ So far we have seen how to get data into a program (i.e., provide input), how to do
arithmetic, and how to get some results (i.e., get output).
▪ In this section, we look at a new feature: repetition. This is implemented by the
extremely powerful for construct. We will first look at some examples of its use,
followed by explanations
▪ for i = 1:5, disp(i), end
▪ Now change it slightly to
▪ for i = 1:3, disp(i), end
▪ And what about
▪ for i = 1:0, disp(i), end
▪ Can you see what’s happening? The disp statement is repeated five times, three
times, and not at all.
Signal and System MATLAB
Square roots with Newton’s method
▪ So far we have seen how to get data into a program (i.e., provide input), how to do
arithmetic, and how to get some results (i.e., get output).
▪ In this section, we look at a new feature: repetition. This is implemented by the
extremely powerful for construct. We will first look at some examples of its use,
followed by explanations
▪ for i = 1:5, disp(i), end
▪ Now change it slightly to
▪ for i = 1:3, disp(i), end
▪ And what about
▪ for i = 1:0, disp(i), end
▪ Can you see what’s happening? The disp statement is repeated five times, three
times, and not at all.
Signal and System MATLAB
Signal and System MATLAB