Modeling & simulation
MEC 242
Introduction to MATLAB
Table of content
• MATLAB windows
• MATLAB Operators
• MATLAB mathematical function
• Simple Math
• Vectors and matrices
1. MATLAB windows
2. MATLAB Operators
2. MATLAB Operators
3. MATLAB mathematical function
• Note: In sin(x) Input angle in radians, but sind(x) Input angle in
degrees.
4. Simple Math
• First we start with numerical that is using the MATLAB as calculator.
You can use “+” to add, “-” to subtract, “*” to multiply, “/” to divide
and “ˆ” to exponentiation. For example:
>> 3^2 - (5 + 4)/2 + 6*3
ans =
22.5000
• MATLAB prints the answer and assigns the value to a variable called
ans.
4. Simple Math
• If you want to perform further calculations with the answer, you can
use the variable ans rather than retype the answer. For example, you
can compute the sum of the square and the square root of the
previous answer as follows:
>> ans^2 + sqrt (ans)
ans =
510.9934
• Observe that MATLAB assigns a new value to ans with each
calculation.
>> u = cos (10)
4. Simple Math
u=
• To do more complex calculations, you can assign -0.8391
computed values to variables of your choosing. >> a=3
For example:
a=
>> c = a
c=
3
5. Vectors and matrices
• Defining a row vector with components the numbers 1, 2, 3, 4, 5 and
assigning it a variable name, say x.
>> x = [1 2 3 4 5] >> x = [1,2,3,4,5]
x= x=
1 2 3 4 5 1 2 3 4 5
• Note that we used the equal sign for assigning the variable name x to
the vector, brackets to enclose its entries and spaces to separate
them. We could have used commas ( , ) instead of spaces to separate
the entries, or even a combination of the two. The use of either
spaces or commas is essential!
5. Vectors and matrices
• Defining a column vector with components the numbers 6, 7, 8, 9, 10
and assigning it a variable name, say y.
>> y = [6;7;8;9;10]
y=
6
7
8
9
10
5. Vectors and matrices >> y = [6,7,8,9,10]
y=
• To take the transpose of a vector (or a matrix
for that matter) we use the single quote ( '). 6 7 8 9 10
>> y'
ans =
6
7
8
9
10
5. Vectors and matrices
• Here we specified the first entry 0 and the last entry 8, separated by a
colon ( : ). MATLAB automatically filled-in the (omitted) entries using
the (default) increment 1.
>> u = [0:8]
u=
0 1 2 3 4 5 6 7 8
5. Vectors and matrices
• You could also specify an increment as is done.
• Here we specified the first entry 0, the increment value 2, and the last
entry 8. The two colons ( :) “tell” MATLAB to fill in the (omitted)
entries using the specified increment value.
>> v = [0:2:8]
v=
0 2 4 6 8
5. Vectors and matrices
• To look at specific parts of the vector. >> v = [0:2:8]
for example, to only look at the first 3
entries in the vector v. v=
• Nots: MATLAB automatically assumes
that the increment is 1. 0 2 4 6 8
>> v(1:3)
ans =
0 2 4
5. Vectors and matrices
• To lists the first 4 entries of the vector v, >> v = [0:2:8]
using the increment value 2 :
v=
0 2 4 6 8
>> v(1:2:4)
ans =
0 4
5. Vectors and matrices
• Defining a matrix is similar to defining a vector. To >> A = [1 2 3; 3 4 5; 6 7 8]
define a matrix A, you can treat it like a column of
row vectors. That is, you enter each row of the
matrix as a row vector (remember to separate the A=
entries either by commas or spaces) and you
separate the rows by semicolons ( ; ). 1 2 3
3 4 5
6 7 8
5. Vectors and matrices
• You can refer to a particular entry in a matrix by >> A = [1 2 3; 3 4 5; 6 7 8]
using parentheses. For example, the number 5 lies
in the 2nd row, 3rd column of A, thus A=
1 2 3
3 4 5
6 7 8
>> A(2,3)
ans =
5
5. Vectors and matrices >> A = [1 2 3; 3 4 5; 6 7 8]
A=
• We can “extract” submatrices using a similar
notation as above. For example to obtain the
submatrix that consists of the first two rows and 1 2 3
last two columns of A. 3 4 5
6 7 8
>> A(1:2,2:3)
ans =
2 3
4 5
>> A = [1 2 3; 3 4 5; 6 7 8]
5. Vectors and matrices
A=
• We could even extract an entire row or column of a
1 2 3
matrix, using the colon ( : ) as follows.
3 4 5
1. Columns: Suppose we want to get the 2nd column of A. 6 7 8
>> A(:,2)
ans =
2
4
7
5. Vectors and matrices >> A = [1 2 3; 3 4 5; 6 7 8]
A=
• We could even extract an entire row or column of a
matrix, using the colon ( : ) as follows.
1 2 3
2. Rows: Suppose we want to get the 3rd row of A. 3 4 5
6 7 8
>> A(3,:)
ans =
6 7 8
Exercise
• Calculate the following:
(a) A-1
(b) A+B
(c) S-t'
(d) B*t
(e) A/B
(f) S.*S
>> A=[1 2 3;3 4 5;6 7 8]
Exercise A=
>> S=[-1 8 5]
S=
1 2 3
3 4 5
-1 8 5
6 7 8
>> t=[7;0;11]
>> B=[-1 3 10;-9 5 25;0 14
2]
t=
B=
7
0
-1 3 10
11
-9 5 25
0 14 2
>> S-t'
>> A-1
Exercise ans =
ans =
-8 8 -6
0 1 2 >> A/B
>> S*t
2 3 4
5 6 7 ans =
ans =
>> A+B 0.7875 -0.1986 0.0450
48
1.8222 -0.5358 0.0866
ans = >> B*t
3.3741 -1.0416 0.1490
0 5 13 ans = >> S.*S
-6 9 30
6 21 10 103 ans =
212
22 1 64 25
Thank you