Professional Documents
Culture Documents
AE 315 Week 1 Tutorial Notes
AE 315 Week 1 Tutorial Notes
MATLAB
Aerospace Department
King Fahd University of Petroleum and Minerals
Dhahran, Saudi Arabia
Abstract
This document provides a bird’s-eye overview to MATLAB, aiming to familiarize the
readers with this software tool. While acknowledging its limitations this document serves
as a starting point for the readers to explore and utilize these tools. However, through the
course progression, students will delve into a more comprehensive methods that are central
to the first course in controls, alongside a taste of advanced topics and techniques, enabling
them to a get access to array of skill set to effectively employ them in favour of control
theory.
Contents
1 MATLAB 2
1.1 MATLAB Installation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
1.2 MATLAB Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
1.3 Basic Mathematical Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
1.3.1 Using MATLAB as a calculator . . . . . . . . . . . . . . . . . . . . . . . . 2
1.4 Matrices and Vectors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
1.4.1 Vectors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
1.4.2 Matrices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
1.5 Visualization and Plotting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
1.5.1 Creating 2-D plots . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
1.5.2 Adding title, axis labels and legend . . . . . . . . . . . . . . . . . . . . . . 7
1.5.3 Multiple data sets in one plot . . . . . . . . . . . . . . . . . . . . . . . . . 8
1.5.4 Subplots . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
List of Figures
1 MATLAB desktop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
2 Plotting basic functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
3 Plotting basic functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
4 Multiple data sets super-imposed in one plot . . . . . . . . . . . . . . . . . . . . 9
5 Subplot example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
Listings
1 Plot basics example 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
2 Plot basics example 2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
3 Multiple data sets in one plot example . . . . . . . . . . . . . . . . . . . . . . . . 8
4 Subplot example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
1
1 MATLAB
1.1 MATLAB Installation
We will consider the fastest way to install MATLAB by using the internet connection method
provided in [1] as following:
1. Sign in to Your MathWorks Account from here or sign up if you do not have account. Use
your university email in the sign up process in order to get the academic licence.
3. In MATLAB installer, sign in using the email you have set in step 1.
2
Figure 1: MATLAB desktop
answer. This is typically the case when you did not assign a variable 1 (will come to this later)
name to the operation. Note that this value (ans) is assigned if it does not exist from the first
place, and if existed, MATLAB overwrites it with the new value. So, suppose you conducted
another arithmetic as follows,
>> (4-2) * 4
ans =
8
Notice that the new value for ans is overwritten by the new value which is 8. That means if you
are going to use it in the future it will have the value of 8 not the previous value of 11.
To avoid this, you can easily assign a variable name to the operation instead of just over-
writing the ans every time as follow,
>> x = (4-2) * 4
x =
8
This variable can be always used in future operations. For example, you can calculate the value
of 4*x. It will result in,
>> 4*x
ans =
32
To suppress the answer from appearing in the command window, one can add a semicolon (;)
after each command.
>> 4*x;
1
It is advised to give your variables a meaningful names. If you are calculating, for example, the control
input for a system, you should avoid naming your variable as u = <<something>>. Instead, use a proper naming
like controlInput = <<something>>. This will make your code more readable as you go on, as well as, more
searchable.
3
As a concluding remark for this subsection, table 1. summarises the operations mentioned
earlier and some more.
1.4.1 Vectors
A matrix’s special case is a vector. This section’s goal is to demonstrate how to build vectors
and matrices in MATLAB. Following the math notation, any vector will be denoted by a bold
symbol. A vector is typically, an array of numbers, ordered in a row or a column structure. An
array of dimensions 1 × n is a row vector. On the other hand, an array of dimensions m × 1 is
a column vector. In MATLAB, the elements of a vector are enclosed by a square brackets and
are separated by spaces, commas, or semi-colons. For examples, let x be a vector that has the
elements
x= 1 2 3 4 5
and we want to define in MATLAB. One can note this a vector of size 1 × 5, which is a row
vector. Two ways of doing this is
>> x = [1 2 3 4 5];
or
>> x = [1, 2, 3, 4, 5];
Also, let y be column vector defines as,
⊤
y= 1 2 3 4 5
where ⊤ is the transpose operator. This can be written in MATLAB language as,
>> y = [1; 2; 3; 4; 5];
also, the transpose operator turns a row vector into a column vector. An apostrophe or
single quote (’) indicates the transposition operation.
So, considering, on can define the y vector by simply transposing the x by the following
command,
>> y = x’
y =
1
2
3
4
5
4
One can access any element by the following command,
>> y(1)
ans =
1
also, a stack of elements can be accessed by,
>> x(1:3)
ans =
1 2 3
or, from on index to the end as,
>> x(3:end)
ans =
3 4 5
The matrices or vectors we work with have too many elements for usto enter one at a time. As
an illustration, let’s say we want to enter a vector x with the points x = 0 0.1 0.2 0.3 . . . 5 .
Utilization the colon operator will facilitate the operation,
>> x = 0:0.1:5
and the resulting vector will have 51 elements.
1.4.2 Matrices
A matrix is also an array of numbers. In order to define a matrix in MATLAB do the
following,
1. Begin with a square bracket ([), as you have seen with vectors.
5
ans =
1 2 0
4 5 6
7 8 9
Single elements of a matrix are accessed as A(i,j), where i ≥ 1 and j ≥ 1. negative and zero
subscripts are not available in MATLAB.
A specific row or column can be chosen using the colon operator. A(m:n,k:l), for instance,
defines rows m to n and columns k to l. Parts of a matrix are referred to through subscript
expressions. For instance,
>> A(2,:)
ans =
4 5 6
The colon operator can also be used to extract a sub-matrix from a matrix A
>> A(:,2:3)
ans =
1 2
4 5
7 8
The matrix transposition operator is the same as done in vectors using (’).
Basic matrix generation functions are available in MATLAB. The functions zeros, ones,
and eye, respectively, return the matrix of zeros, the matrix of ones, and the identity matrix.
Table 2 describes them.
Command Description
eye(m,n) Returns an m × n matrix with 1 on the main diagonal
ones(n,m) Returns an m × n matrix of ones
zeros(m,n) Returns an m × n matrix of zeros
rand(m,n) Returns an m × n matrix of random numbers
As we just mentioned, MATLAB enables the arithmetic operations +,-,*,/ and to be per-
formed on matrices. These operations are valid for matrices as long as they satisfy the mathe-
matical conditions required to make a specific operations. For example, if A and B are matrices
of the same size, we can add them, if A has a number columns as the same number of B’s number
of row, we can multiply them, and so on.
On the other hand, array arithmetic, also known as array operations, are carried out ele-
ment by element. The array operations are distinguished from matrix operations by the period
character (.). The character pairs (.+) and (.-) are not utilized, however, because addition and
subtraction are performed using the same matrix and array operations.
For example, consider
1 2 3 10 20 30
A = 4 5 6 , B = 40 50 60
7 8 9 70 80 90
6
Operation Matrix Array
Addition + +
Subtraction − −
Multiplication ∗ .∗
Division / ./
Left division \ .\
Exponentiation ˆ .ˆ
7
1
0.8
6
0.6
0.4
5
0.2
4 0
-0.2
3
-0.4
-0.6
2
-0.8
1 -1
1 1.5 2 2.5 3 3.5 4 4.5 5 0 1 2 3 4 5 6
7
>> ylabel(’sin(x)’)
>> title(’plot of sine function vs. angle’)
0.8
6
0.6
0.4
5
0.2
f(x)
f(x)
4 0
-0.2
3
-0.4
-0.6
2
-0.8
1 -1
1 1.5 2 2.5 3 3.5 4 4.5 5 0 1 2 3 4 5 6
x x
To be able to distinguish between different plots on the same figure, command legend can
be used to name each data set on the plot.
>> legend(’2*cos(x)’,’cos(x)’,’0.5*cos(x)’);
The names in the command line should be ordered according to the first plotted data sets. The
code result can be found in figure 4.
8
2
2*cos(x)
1.5 cos(x)
0.5*cos(x)
1
Cosine functions
0.5
-0.5
-1
-1.5
-2
0 1 2 3 4 5 6
0 x 2
1.5.4 Subplots
Subplot command divide the figure into different subplots in the same figure and create axes
for each one. subplot(m,n,p) divides the figure into m × n grid. p indicates the position for
the subplot figure. The first subplot is the first column of the first row, the second subplot is
the second column of the first row, and so on. If axes exist in the specified position, then this
command makes the axes the current axes.
9
2 1
2*cos(x)
cos(x)
0 0
-2 -1
0 2 4 6 0 2 4 6
0 x 2 0 x 2
0.5 1
0.5*cos(x)
sin(x)
0 0
-0.5 -1
0 2 4 6 0 2 4 6
0 x 2 0 x 2
10
References
[1] “Install Products Using Internet Connection - MATLAB & Simulink,” 2023. https://www.
mathworks.com/help/install/ug/install-products-with-internet-connection.
html.
11