You are on page 1of 4

Computational Mathematics Lab

Introduction to MATLAB
R Prasanth Kumar

Department of Mechanical and Aerospace Engineering


Indian Institute of Technology Hyderabad
2015

Introduction to this Course




MATLAB stands for MATrix LABoratory.

This one credit lab course runs for two months and deals with how to use MATLAB
for scientic computation.

Beginner level only

Lecture is followed by tutorial, exercises to be done during the lecture


Evaluation




Tutorial is not evaluated.


One exam every month

Most used command by beginners to read the documentation on a command or


function

>> help <command or function name>

Simple Computation on Command Line


1. Open MATLAB
2. Type the following in the Command Window
and press enter key after each line.

>> x = 5
>> y = 2
>> z = x+y
3. Repeat with semicolon at the end of rst and
second lines.
4. Try this:

>> x = 5;y = 2;z = x+y

Variables can be dened and


initialized with equal to sign.

Semicolon at the end of a line


suppresses the display or
result.

Multiple statements can be


entered in the same line using
semicolon.

Scalars, Vectors, and Matrices




We have already seen how to dene scalar variables in the previous slide.

Dening a row vector:

>> x = [1 0 -8 6 7]


Dening a column vector:

>> y = [1 0 -8 6 7]
Single quote at the end of a vector or matrix means transpose.


Dening a matrix:

>> z = [1 2 3;4 5 6]
a 2 x 3 matrix. Semicolon separates the rows of matrix.


To know the size of a matrix:

>> size(z)
gives a two element row vector as output with rows of z as the rst element, and
columns of z as the second element. To get number of rows or columns separately,
use size(z,1) or size(z,2)

Creating Special Matrices




Identity Matrix

>> idmat = eye(4)


creates a 4 x 4 identity matrix.


Diagonal matrix from elements of any


vector

>> elements = [1 2 0 -3];


>> dmat = diag(elements)


Matrix of all elements as zeros

Elements in series using colon


operator

>> x = 1:10
>> y = 1:2:10


>> zeromat = zeros(2,3)

Random number matrix

>> z = rand(3,4)

creates a zero matrix of size 2 x 3.




Matrix of all elements as ones

>> onemat = ones(4,2)

Operation on All Elements of a Matrix




Multiplying with or dividing by a scalar

>> A = [1 2;3 4];


>> A*2
>> A/5


>> x = 0:0.5:3
>> y = 5*x.^3-50

Addition or subtraction of a scalar

>> A+2
>> A-3





Element-wise multiplication or
division

>> B = [4 5;6 7];


>> A.*B
>> A./B
Element-wise exponential

>> A.^2

Let us nd the data for a curve


y = 5x3 50 for a set of values of x.

Is 5 multiplied before or after taking


cube?
Why is it x3 instead of x47 ?
Precedence, which operators have
priority over others.

Precedence (high precedence to low)


1.
2.
3.
4.
5.

Paranthesis ( )
Transpose, power (with/without .)
Unary plus, unary minus
Multiplication and division
Addition and subtraction

Some Often Used Functions on Matrices




Trace of a square matrix

>> A = [1 2 3;4 5 6;7 8 9]


>> trace(A)

Find the average of elements of a


matrix along the columns and
along the rows

Get the diagonal elements of a square


matrix

>> mean(A,1)
>> mean(A,2)

>> diag(A)


Sum of elements of a row matrix

>> x = 1:10
>> sum(x)


>>
>>
>>
>>

Sum of elements of each column of a


matrix

det(A)
rank(A)
eig(A)
[V,D] = eig(A)

V contains eigenvectors as
columns, and D contains
eigenvalues as diagonal elements

>> sum(A)


Determinant, rank, eigenvalues,


eigenvectors

Sum of elements of each row of a matrix

>> sum(A,2)

Extracting Elements of Matrices




Extract a particular element of a matrix

>> A = [1 2 3;4 5 6;7 8 9];


>> A(1,3)


>>
>>
>>
>>
>>
>>

Extract a row or column

>> A(1,:)
>> A(:,2)
Colon indicates all.


Extract a submatrix from a matrix

>> A(2:3,1:2)

For a row or column matrix, column or


row index can be dropped.

x = [5 6 7 8 9];
y = [4 5 1 2 3];
x(1,4)
x(4)
y(3,1)
y(3)

Extracting elements corresponding to


odd indices in a row matrix.

gives submatrix from rows 2 to 3 and


columns 1 to 2.

>> x(1,[1 3 5])

Extract particular columns or rows

>> x([1 3 5])

>> A(:,[1 3])

or

gives all rows of columns 1 and 3.

>> x(1:2:size(x,2))

or

Concatenation of Matrices

Concatenate two matrices

>> x = [1 2 3 4;5 6 7 8];y = [-5 -6 -7 -8];


>> z = [-5 -6 -7 -8;-1 -2 -3 -4];
>> A = [x;y]
Number of columns in x and y should be same.

>> B = [x z]
Number of rows in x and z should be same.

Find Numbers in a Matrix Based on Some Condition




Minimum and maximum of a vector

>> x = [3 0 5 -2 8];
>> min(x)
>> max(x)


>>
>>
>>
>>

Minimum and maximum of columns of a


matrix

>> z = [3 0 5 -2 8;2 -2 9 -4 10];


>> min(z)
>> max(z)


Minimum and maximum of rows of a


matrix

Indices of numbers greater/less


than or equal to a given number

Caution: == sign works only when


comparison is exact. Try replacing 7
with 6.999999999999.


>> min(z,[],2)
>> max(z,[],2)

Numbers greater/less than or equal


to a given number

>> x(find(x>4))

Miscellaneous Commands

Clear a specic variable x and y from workspace

>> clear x y


Clear all variables from workspace

>> clear all




Clear the command window and home the cursor

>> clc

x = [-3 2 0 7 -5 10];
find(x>4)
find(x<2)
find(x==7)

You might also like