Computer Programming (MATLAB)
Dr. Vamsi Krishna Narla
Guest Faculty
Work Integrated Learning Programmes (WILP)
Birla Institute of Technology & Science, Pilani
Hyderabad Campus
V. K. NARLA MATLAB 1 / 29
Objectives
Objectives
The main objectives are:
Understanding the MATLAB environment.
Being able to do simple calculations using MATLAB.
Being able to carry out simple numerical computations and
analyses using MATLAB.
V. K. NARLA MATLAB 2 / 29
Basic Operations
Variables: Variables are defined with the assignment operator, “=”.
Example
>>x=17 >> x = [3*4, pi/2]
x= x=
17 12.0000 1.5708
>> x=’hat’ >> y = 3*sin(x)
x= y=
hat -1.6097 3.0000
Note!: MATLAB is case sensitive! The variables x and x are not the
same.
V. K. NARLA MATLAB 3 / 29
Continued...
Note!:The semicolon is used to terminate commands.
Example
>> a=5
a=
5
>>a=5;
>>
V. K. NARLA MATLAB 4 / 29
Built-in Functions:
Here are some descriptions for the most used basic built-in MATLAB
functions.
Function Description Example
help MATLAB displays the help >>help
information available
help Display help about a specific function >>help plot
<function>
who, whos who lists in alphabetical order all >>who
variables in the currently active >>whos
workspace.
V. K. NARLA MATLAB 5 / 29
Arrays; Vectors and Matrices:
☞ Arrays are the backbone of MATLAB computation.
☞ A 2-D array is a list of numbers arranged in rows and columns.
☞ If you form an array by writting numbers in rows, all rows must
have the same number of entities. Same is true for columns.
☞ An array with m rows and n columns is called m × n array and it
has a total of m.n entries.
Example
Array Creation:
>> a=[3 5 7 -8 5 2]
a=
3 5 7 -8 5 2
This type of array is called row vector
V. K. NARLA MATLAB 6 / 29
Matrices
A general matrix A may be written like this:
a11 · · · a1m
A = ... .. .. ∈ Rn×m
. .
an1 · · · anm
Example
>> A = [1 2; 3 4]
A=
1 2
3 4
➝ To separate rows, we use a semicolon “;”
➝ To separate columns, we use a comma “,” or a space “ “.
V. K. NARLA MATLAB 7 / 29
Continued...
To get a specific part of a matrix and from 2 vectors x and y we can
create a matrix like this:
Example
>> A(2,1) >> x = [1; 2; 3];
ans = >> y = [4; 5; 6];
3 >> B = [x y]
>> A(:,1)
ans = B=
1 14
3 25
>> A(2,:) 36
ans =
34
V. K. NARLA MATLAB 8 / 29
Colon Notation:
The “colon notation” is very useful for creating vectors:
x=[xi:dx:xf]
Here
➝ xi is Starting value
➝ dx is Increment
➝ xf is Final value
Example
>>x=[0:0.1:0.5];y=x·∗sin(x)
>>[x′ y ′ ]
ans=
0 0
0.1000 0.0100
0.2000 0.0397
0.3000 0.0887
0.4000 0.1558
0.5000 0.2397
V. K. NARLA MATLAB 9 / 29
linspace(a,b,n)
It generates a linearly spaced vector of length n from a to b i.e., It
divides [a, b] into n-1 equally spaced sub-intervals
Example
>>a=linspace(0,1,5)
a=
0 0.2500 0.5000 0.7500 1.0000
>>b=linspace(0,20,5)
b=
0 5 10 15 20
b−a
Hence,linspace(a,b,n) is the same as v = a : n−1 : b.
Also linspace(a,b) produce a vector of length 100 from a to b.
V. K. NARLA MATLAB 10 / 29
Deleting Rows and Columns:
You can delete rows and columns from a matrix using just a pair of
square brackets [ ].
Example
" #
0 1
Given A =
−2 −3
To delete the second column of a matrix A, use:
>>A=[0 1; -2 -3];
>>A(:,2) = [ ]
A=
0
-2
V. K. NARLA MATLAB 11 / 29
Multiple commands on same line:
It is possible to type several commands on the same line. In some cases
this is a good idea to save space.
Example
>> x=1,y=2,z=3
x=
1
y=
2
z=
3
V. K. NARLA MATLAB 12 / 29
Linear Algebra
Linear Algebra is a branch of mathematics concerned with the study of
matrices, vectors, vector spaces (also called linear spaces), linear maps
(also called linear transformations), and systems of linear equations.
Here are some useful functions for Linear Algebra in MATLAB:
Function Description Example
rank Find the rank of a matrix. Provides > >A=[1 2; 3 4]
an estimate ofthe number of linearly >>rank(A)
independent rows orcolumns of a.
matrix A
det Find the determinant of a square >> det(A)
matrix
inv Find the inverse of a square >>inv(A)
matrix
eig Find the eigenvalues of a square >>eig(A)
matrix
V. K. NARLA MATLAB 13 / 29
Vectors
x1
x2
n
.. ∈ R
Given a vector x :
.
xn
Example
1
Given x = 2
3
>> x=[1; 2; 3]
x=
1
2
3
V. K. NARLA MATLAB 14 / 29
Continued...
The Transpose
h of vector
i x:
xT = x1 x2 · · · xn ∈ R1×n .
The Length of q
vector x:
√
∥x∥ = x x = x21 + x22 + · · · + x2n .
T
Example
′
>> x
ans =
12p3
>> l = (x′ ∗ x)
l=
13
V. K. NARLA MATLAB 15 / 29
Transpose
a11 · · · an1
T .. .. .. ∈ Rm×n
The ranspose of matrix A: A = . . .
a1m · · · anm
Example
>> A = [0 1; -2 -3]
A=
0 1
-2 -3
>>A′
ans=
0 -2
1 -3
V. K. NARLA MATLAB 16 / 29
Diagonal
The Diagonal
elements
of matrix A is the vector
a11
a22
p=min(x,m)
.. ∈ R
diag(A) =
.
app
Example
>> diag(A)
ans=
0
-3
V. K. NARLA MATLAB 17 / 29
Matrix Multiplication
Given the matrices A ∈ Rn×n and B ∈ Rm×p then C = AB ∈ Rn×p
Example
>>A = [0 1; -2 -3]
A=
0 1
-2 -3
>>B = [1 0; 3 -2]
B=
1 0
3 -2
>>A*B
ans=
3 -2
-11 6
V. K. NARLA MATLAB 18 / 29
Matrix Addition
Given the matrices A ∈ Rn×m and B ∈ Rn×m then
C = A+B ∈ Rn×m
Example
>>A = [0 1; -2 -3]
A=
0 1
-2 -3
>>B = [1 0; 3 -2]
B=
1 0
3 -2
>>A+B
ans=
1 1
1 -5
V. K. NARLA MATLAB 19 / 29
Determinant
Given the matrices A ∈ Rn×n , then the Determinant is given by:
det(A) = |A| " #
T a11 a12
Given a 2 × 2 matrix:A = ∈ R2×2 Then
a21 a22
det(A) = |A| = a11 a22 − a21 a22
Example
>>A = [0 1; -2 -5]
A=
0 1
-2 -5
>>det(A)
ans=
2
V. K. NARLA MATLAB 20 / 29
Inverse Matrices
The inverse of a quadratic matrix A ∈ Rn×n is defined by: A−1 if
AA−1 "= A−1 A #= I. For a 2×2 matrix we have:
a a
AT = 11 12 ∈ R2×2 The inverse A−1 is then given by
a21 a22
" #
a22 −a12
A−1 = 1
det(A) ∈ R2×2
−a21 a11
Example
>>A = [0 1; -2 -3]
A=
0 1
-2 -3
>>inv(A)
ans=
-1.5000 -0.5000
1.0000 0
V. K. NARLA MATLAB 21 / 29
Eigenvalues
Given A ∈ Rn×n , then the Eigenvalues is defined as: det(λI − A) = 0
Example
A=
0 1
-2 -3
>>eig(A)
ans=
-1
-2
V. K. NARLA MATLAB 22 / 29
Solving Linear Equations
MATLAB can easily be used to solve a large amount of linear
equations using built-in functions.
The methods are used to find the solution, such as:
☞ Least Square method
☞ LU factorization
In MATLAB we can also simply use the backslash operator “\” in
order to find the solution.
x = A\b
The given system of equations written in the matrix form
Ax = b
when A is a square matrix.
V. K. NARLA MATLAB 23 / 29
Continued...
We can find the solution by taking the inverse of the A matrix:
x = A−1 b
Example
>>A= [1 2; 3 4; 7 8];
>>b = [5;6;9];
>>x=inv(A)* b
Error using inv
Matrix must be square.
V. K. NARLA MATLAB 24 / 29
Continued...
Example
>>A= [1 2; 3 4; 7 8];
>>b = [5;6;9];
>>x=A\b
ans=
-3.5000
4.1786
V. K. NARLA MATLAB 25 / 29
Least Square Method
Given
Ax = b
The Least Square Method formula is given by
xLS = (AT A)−1 AT b
Example
>>A= [1 2; 3 4; 7 8];
>>b = [5;6;9];
>>xls=inv(A′ ∗ A) ∗ A′ ∗ b
ans=
-3.5000
4.1786
V. K. NARLA MATLAB 26 / 29
System of equations
Given the equations:
x + 2y =5
3x + 4y =6
Set the equations on the following form:
Ax = b
Example
>>A= [1 2; 3 4];
>>b = [5;6];
>>xls=inv(A−1 )*b
ans=
-4.0000
4.5000
V. K. NARLA MATLAB 27 / 29
Continued...
Task
1). 3x + 2y = 9
5x + y = 10.
2). 2x – y + 3z = 9
x – 3y – 2z = 0
3x + 2y – z = -1.
3). x + y + z = 6
3x – 2y – z = 4
2x + 3y – 2z = 2.
V. K. NARLA MATLAB 28 / 29
THANK YOU
V. K. NARLA MATLAB 29 / 29