0% found this document useful (0 votes)
38 views67 pages

MATH F112 (Mathematics-II) : Matlab

Lecture 19

Uploaded by

Hrithik Piyush
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views67 pages

MATH F112 (Mathematics-II) : Matlab

Lecture 19

Uploaded by

Hrithik Piyush
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 67

BITS Pilani

Pilani Campus

MATH F112 (Mathematics-II)


MATLAB

Introduction of MATLAB & Its


Applications
BITS Pilani
Pilani Campus

Dr Trilok Mathur,
Assistant Professor,
Department of Mathematics

MATLAB is a high-level language and interactive


environment for numerical computation,
visualization and programming.
Using MATLAB you can be able to analyze data,
develop algorithms, and create models and
applications.

3
BITS Pilani, Pilani Campus

The language, tools, and built-in math functions


enable you to explore multiple approaches.
Developed by MathWorks, in 1984, Full Name:
MATrix LABoratory.

The MathWorks Inc., branded as simply


MathWorks, is a privately held, multi-national
corporation that specializes in mathematical
computing software.
4
BITS Pilani, Pilani Campus

MATLAB is an interactive system whose basic data


element is an array/matrix. It considers all data
elements as an array and results are also shown in
form of arrays.
This allows you to solve many technical computing
problems, especially those with matrix and vector
formulations, in a fraction of the time it would take
to write a program in a scalar non-interactive
language such as C or Fortran.
5
BITS Pilani, Pilani Campus

MathWorks Web site:


1.www.mathworks.com
2.www.mathworks.com/education
3.www.mathworks.com/support/Books
Through these website you will be able to access:
1.Information about MathWorks products
2.How they are used?
3.Demos
4.MATLAB based books/documents
5.Technical support in form of m-files
6
BITS Pilani, Pilani Campus

This is the set of tools and facilities that help you use:
1. MATLAB functions and files,
2. MATLAB Editor,
3. A Command Window,
4. A Command History,
5. The workspace,
6. Current Directory,
7. Array Editor,
8. Help,
9. Toolbar,
10. Shortcuts
7
BITS Pilani, Pilani Campus

6. Current Directory

9. Toolbar
10. Shortcuts

8.Help

5. Workspace

3. Command Window
2. MATLAB Editor

1. MATLAB functions

7. Array Editor

4. Command History

8
BITS Pilani, Pilani Campus

clc: Clears the command window


clear: Clears the workspace
clear x y: Clear variables x and y only from work space
disp(A): displays the value of A
whos (A): displays all the details related to variable A
stored in workspace
who: List known variables
help: displays all the topics you need to know about
help------: help for using a particular keyword
(for example >> help sqrt
Help on using sqrt) 9
BITS Pilani, Pilani Campus

Orange line indicates the warring


Red line indicates the error in program
Save project before running your program
If you are using button to run the program it will
automatically save your program
Use break points to debug large program

10
BITS Pilani, Pilani Campus

11
BITS Pilani, Pilani Campus

Matrix/Arrays are used in MATLAB very


frequently, one can specify the way to generate a
Matrix/Arrays according to the need .
Various ways are available with MATLAB for the
generation of Matrix/Arrays.

12
BITS Pilani, Pilani Campus

Some most commonly used approaches are:


User defined
Using pre-defined operators
Using pre-defined function
Random Generation

13
BITS Pilani, Pilani Campus

A matrix can be created in MATLAB as follows


(note the commas AND semicolons):
matrix = [1, 2, 3; 4, 5, 6; 7, 8, 9]
matrix =
1 2
4 5
7 8

3
6
9
14
BITS Pilani, Pilani Campus

Specify Range

Row Separator

Column Separator

15
BITS Pilani, Pilani Campus

16
BITS Pilani, Pilani Campus

MATLAB has some built-in functions for the generation


of matrix.
One can use such built-in functions, according to the
need.
These function can mainly be used initialize a
matrix/array.

Also, the matrix created through such functions are


useful in logical operations.
17
BITS Pilani, Pilani Campus

This function creates an array of all zeros, that is


all the elements of such created matrix are zero.
B = zeros(n) returns an n-by-n matrix of zeros.
B = zeros(m,n) or B = zeros([m n]) returns an
m-by-n matrix of zeros.
B = zeros(m,n,p,...) or B = zeros([m n p ...])
returns an m-by-n-by-p-by-... array of zeros.
B = zeros(size(A)) returns an array the same
size as A consisting of all zeros.
18
BITS Pilani, Pilani Campus

Note: The size inputs m, n, p, ... should be nonnegative integers. Negative integers are
treated as 0.

19

BITS Pilani, Pilani Campus

Y = ones(n) returns an n-by-n matrix of 1s. An


error message appears if n is not a scalar.
Y = ones(m,n) or Y = ones([m n]) returns an
m-by-n matrix of ones.
Y = ones(m,n,p,...) or Y = ones([m n p ...])
returns an m-by-n-by-p-by-... array of 1s.
Y = ones(size(A)) returns an array of 1s that is
the same size as A.
20
BITS Pilani, Pilani Campus

Note The size inputs m, n, p, ... should be nonnegative integers. Negative integers are
treated as 0.

21

BITS Pilani, Pilani Campus

Y = eye(n) returns the n-by-n identity matrix.


Y = eye(m,n) or eye([m n]) returns an m-by-n matrix
with 1's on the diagonal and 0's elsewhere.
Y = eye(size(A)) returns an identity matrix the same
size as A.

22
BITS Pilani, Pilani Campus

Note The size inputs m and n should be nonnegative integers.


Negative integers are treated as 0.

23
BITS Pilani, Pilani Campus

To generate a random scalar


Syntex: randn
>> randn
>> randn
To generate m x n matrix with random entries
randn(m x n) :
>> randn(4, 4)
>> randn(4)
24
BITS Pilani, Pilani Campus

A matrix having at least one dimension equal to


zero is called an empty matrix.
The simplest empty matrix is 0-by-0 in size.
Examples of more complex matrices are those of
dimension 0-by-5 or 10-by-0.

25
BITS Pilani, Pilani Campus

26
BITS Pilani, Pilani Campus

27
BITS Pilani, Pilani Campus

28
BITS Pilani, Pilani Campus

Addition / subtraction of two vectors


>> v1 = [ 1, 3, 4 ]
>> v2 = [ 2, 5, 3 ]
>> v1 + v2
>> v1 v2
29
BITS Pilani, Pilani Campus

Scalar multiplication of a vector


>> v = [ 2, 3, 4]
>> k = 10
>> k * v
ans =
20 30 40
30
BITS Pilani, Pilani Campus

Vector cross product. w = u x v


>> u = [2 4 7]
>> v = [10 20 30]
>>w = cross(u, v)
NOTE: u and v must be
3 element vectors
Try for
>> help dot
31
BITS Pilani, Pilani Campus

Ex:

Let p(t ) = 2 t3 + 5 t2 + t 2
q(t ) = t3 + 3 t + 5
Find 1. p(t) + q(t)
2. 5 p(t)
3. 3 p(t) 4 q(t)
32
BITS Pilani, Pilani Campus

>> p = [ 2, 5, 1, -2]
>> q = [ 1, 0, 3, 5]
>> p + q
ans = 3 5 4

>> 5 * p
>> 3 * p - 4 * q
33
BITS Pilani, Pilani Campus

Ex: Let V = R3 and W = {(2, a , b ) | a,b R } Is W


a subspace of V?
Sol: >> a1 = randn
>> b1 = randn
>> x = [2, a1, b1]
>> a2 = randn
>> b2 = randn
>> y = [2, a2, b2]
>> x + y
34
>> 5 * x
BITS Pilani, Pilani Campus

Multiplication of
Matrices
If A is an m-by-p and B is a p-by-n matrix,
the i, j entry of C is defined by

35
BITS Pilani, Pilani Campus

Multiplication of
Matrices
3x2

2x3

3x3
36
BITS Pilani, Pilani Campus

Multiplication of
Matrices

37
BITS Pilani, Pilani Campus

Let A be Matrix, Calculate A3


>> A^3

Let A = (aij)
Find matrix B = (bij) such that bij=(aij2)
>>B = A . ^ 2
Dot
38
BITS Pilani, Pilani Campus

Appending a Row or Column


>> [A v]
append column vector v to
columns of A
>> [A;u]

append row vector u to


rows of A
39
BITS Pilani, Pilani Campus

Ex:
1

A 4

7
u [10

2
5
8
20

6,

9
11

30 ], v 22

33

40

BITS Pilani, Pilani Campus

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


>>u = [10 20 30]
>>v = [11 22 33]
>> [ A v ]
ans

1
4
7

2
5
8

3
6
9

11
22
33
41
BITS Pilani, Pilani Campus

>> [ A ; u ]
ans
1
4
7
10

2
5
8
20

3
6
9
30
42
BITS Pilani, Pilani Campus

You can delete rows and columns from a matrix


using just a pair of square brackets.
This can be done by using colon (:) operator and
an empty matrix.
For example if you want to delete column i / row
i of a matrix A, than following expression can be
used:
A(:, i) = []
B(i, :)= []
43
BITS Pilani, Pilani Campus

44
BITS Pilani, Pilani Campus

If you delete a single element from a matrix, the


result is not a matrix anymore.
So, expressions like X(1,2) = [] result in an error.

45
BITS Pilani, Pilani Campus

Extract diagonal of matrix A


>>A=[ 1 2; 3 4]
>> diag(A)
ans
1
4
46
BITS Pilani, Pilani Campus

Generate a diagonal matrix with v as


diagonal
>> v = [1, 2]
>> diag(v)
ans
1
0
0
2
47
BITS Pilani, Pilani Campus

Syntax Y=inv(X)
Description

Y = inv(X) returns the inverse of the square matrix


X.

48
BITS Pilani, Pilani Campus

49
BITS Pilani, Pilani Campus

Syntax d = det(X)
Description
d = det(X) returns the determinant of the square
matrix X.

50
BITS Pilani, Pilani Campus

51
BITS Pilani, Pilani Campus

To find Row Reduced Echelon form of


matrix A: rref(A)

>> A=rand(3)
>> rref(A)

52
BITS Pilani, Pilani Campus

To find Rank of matrix A: rank(A)


>> A=rand(3)
>> rank(A)

53
BITS Pilani, Pilani Campus

Ex. Let S = {(1, 0, 0, 1), (0, 1, 1, 0), (1, 1, 1, 1)},


Check (0, 1, 1, 1) is in Span(S)
SOL:>> v1 = [1, 0, 0, 1]
>> v2 = [0, 1, 1, 0]
>> v3 = [1, 1, 1, 1]
>> v = [0, 1, 1, 1]
>> Ab = [v1 , v2, v3, v ]
>> rref(Ab)
If System is consistent, then v [ S ]
54
BITS Pilani, Pilani Campus

Ex. Let S = {(1, 0, 0, 1), (0,1, 1, 0),(1, 1, 1, 1) }


Check S is LI or LD
Sol.: >> v1 = [ 1, 0, 0, 1]
>> v2 = [ 0, 1, 1, 0]
>> v3 = [ 1, 1, 1, 1]
>> b = zeros(4, 1)
>> A = [ v1, v2, v3]
>> Ab = [A, b]
>> rref(Ab) OR >>rref([A, b])
Rank(Ab) = no. of vectors, then LI
55
BITS Pilani, Pilani Campus

Ex. Let S = { (1, 1, 0, 0), (-2, -2, 0, 0), (1, 0, 2, 1),


(2, 1, 2, 1), (0, 1, 1, 1) }
Find dim (Span S )
Sol. >> v1 = [ 1, 1, 0, 0]
Vectors
>> v2 = [ -2, -2, 0, 0]
corresponding to
>> v3 = [ 1, 0, 2, 1] leading 1s
>> v4 = [ 2, 1, 2, 1] will be basis of [S]
>> v5 = [ 0, 1, 1, 1]
>> rref([ v1 , v2, v3, v4 , v5])
56
BITS Pilani, Pilani Campus

Example: Check the consistency for

1
0

x1
21

x
2 8
x 3
16
x 4
57
BITS Pilani, Pilani Campus

>>A = [1 2 4 -1;
0 1 2
0;
3 1 1 -2 ]
>> b = [21, 8, 16]
>> rank (A) - rank( [A , b] )
If output is 0 then system is consistent
58
BITS Pilani, Pilani Campus

Ex. Let V = R4
S={(1, 2, 3, 0), (0, 1, 2, 3), (3, 0, 1, 2),
(2, 3, 0, 1) }
T={ e1 , e2 , e3 , e4 }
Find Transition matrix PS

59
BITS Pilani, Pilani Campus

>>A = [1, 2, 3, 0; 0, 1, 2, 3;
3, 0, 1, 2; 2, 3, 0, 1 ]
>> B = eye(4) (WHY)!!
>> rref( [A, B] )
Transpose
>>P = ans(: , 5 : 8)

60
BITS Pilani, Pilani Campus

Ex. Let L : R3
L( v ) = C v,

R4

1
2
C
3

0
1

2
1
1
0

Find matrix of L with respect to bases

1
2
0

S s1 0 , s 2 0 , s3 1

1
1
2


and

61
BITS Pilani, Pilani Campus

T w1 , w 2
1

1
0
0
1
1
0

, w3
, w4

1
1
1



0
1
0

62
BITS Pilani, Pilani Campus

>>C=[ 1 2 0; 2
>>s1=[1 0 1]
>>s2=[2 0 1]
>>s3=[0 1 2]
>>w1 = [1 1 1
>>w2 = [1 1 1
>>w3 = [0 1 1
>>w4 = [0 0 1

1 -1; 3 1 0; -1 0 2]

2]
0]
-1]
0]

63
BITS Pilani, Pilani Campus

>>Ls1 = C * s1
>>Ls2 = C * s2
>>Ls3 = C * s3
>>M=[ w1 w2 w3 w4
Ls1 Ls2 Ls3 ]
>>rref( M )
>>A = ans(: , 5 : 7 )
64
BITS Pilani, Pilani Campus

Ex. L(x) = A x be a LinearTransformation


Find basis of Ker L , Range L

1
A
2

2
3

5
8

65
BITS Pilani, Pilani Campus

>> A = [1, 2, 5, 5; -2, -3, -8, -7]


>>rref( A ) further simplify to get
basis of ker L

Each non-zero column of this matrix


having non zero pivot is a basis of
Range L
66
BITS Pilani, Pilani Campus

BITS Pilani, Pilani Campus

You might also like