Online STTP On MATLAB Based Teaching-Learning in Mathematics, Science and Engineering, MAY 18-22, 2020
Online STTP On MATLAB Based Teaching-Learning in Mathematics, Science and Engineering, MAY 18-22, 2020
based Teaching-Learning in
Mathematics, Science and
Engineering, MAY 18-22, 2020
Table of Contents
Organized by Department of Electronics Engineering, Ramrao Adik Institute of Technology,
Nerul, Navi Mumbai ............................................................................................................ 2
Demonstration Session: Defining Arrays and Matrices and Computation of norms .......................... 2
Prepared by: Vishwesh A. Vyawahare, RAIT ........................................................................... 2
Initial instructions ............................................................................................................... 2
Defining Row Vectors ......................................................................................................... 2
Basic operations of row vectors ............................................................................................. 3
Defining row matrices smartly .............................................................................................. 4
Appending an element in an array ......................................................................................... 5
Some more basic functions for arrays ..................................................................................... 5
Defining Column Vectors ..................................................................................................... 6
Transpose of vectors ............................................................................................................ 7
Defining Matrices ............................................................................................................... 7
Extracting an element, a row, a column or a minor from a matrix ................................................ 8
Defining special matrices ..................................................................................................... 9
Generation of diagonal matrix ............................................................................................. 10
Block diagonal matrix ........................................................................................................ 11
Generation of block diagonal matrix ..................................................................................... 11
Upper trangular matrix ....................................................................................................... 12
Generation of Upper trangular matrix ................................................................................... 13
Lower trangular matrix ....................................................................................................... 14
Generation of Lower trangular matrix ................................................................................... 14
Hilbert matrix ................................................................................................................... 15
Generation of Hilbert matrix ............................................................................................... 16
Vandermonde matrix .......................................................................................................... 16
Generation of Vandermonde matrix ...................................................................................... 17
Pascal Matrix .................................................................................................................... 17
Generation of Pascal matrix ................................................................................................ 17
Magic Square matrix .......................................................................................................... 18
Generation of Magic Square matrix ...................................................................................... 18
Defining complex vectors ................................................................................................... 19
Absolute values of real and complex vectors .......................................................................... 20
Computation of norms of real vectors ................................................................................... 21
Computation of 1-Norm ..................................................................................................... 22
Computation of 2-Norm ..................................................................................................... 23
Computation of p-Norm ..................................................................................................... 23
Computation of infinity-Norm ............................................................................................. 24
Computation of norms of real matrices ................................................................................. 25
References ........................................................................................................................ 25
1
Online STTP on MATLAB
based Teaching-Learning in
Mathematics, Science and En-
gineering, MAY 18-22, 2020
[rownumber,colnumber] = size(a)
a =
1 2 3 4
b =
rownumber =
colnumber =
2
Online STTP on MATLAB
based Teaching-Learning in
Mathematics, Science and En-
gineering, MAY 18-22, 2020
Basic operations of row vectors
a + b % addition of two row vectors
theta = [-pi -pi/2 -pi/4 0 pi/4 pi/2 pi] % defining a vector of angles
in radians
ans =
ans =
ans =
ans =
ans =
3
Online STTP on MATLAB
based Teaching-Learning in
Mathematics, Science and En-
gineering, MAY 18-22, 2020
2.2000 4.4000 6.6000 8.8000
ans =
c =
ans =
ans =
1 4 9 16
theta =
ans =
ans =
4
Online STTP on MATLAB
based Teaching-Learning in
Mathematics, Science and En-
gineering, MAY 18-22, 2020
a =
1 2 3 4 5
b =
-5 -3 -1 1 3 5
c =
Columns 1 through 7
Columns 8 through 11
x =
1 1 1 1 1
y =
0 0 0 0 0 0 0
a =
1 2 3 4 5 6
ans =
1 6
5
Online STTP on MATLAB
based Teaching-Learning in
Mathematics, Science and En-
gineering, MAY 18-22, 2020
max(a) % gives the largest element in an array
ans =
ans =
ans =
21
[rownumber,colnumber] = size(a)
% All the above basic operations defined for row vectors can also
% be applied to column vectors.
a =
1
2
3
4
5
rownumber =
colnumber =
6
Online STTP on MATLAB
based Teaching-Learning in
Mathematics, Science and En-
gineering, MAY 18-22, 2020
Transpose of vectors
b = a' % converting a column vector to row vector by transpose
b =
1 2 3 4 5
c =
1
2
3
4
5
Defining Matrices
a = [1 2; 3 4] % defining a 2 x 2 square matrix
a =
1 2
3 4
b =
2.3000 9.6000
7
Online STTP on MATLAB
based Teaching-Learning in
Mathematics, Science and En-
-9.4000 0.0500 gineering, MAY 18-22, 2020
c =
1 2 3
4 5 6
ans =
3.3000 11.6000
-6.4000 4.0500
ans =
-1.3000 -7.6000
12.4000 3.9500
ans =
-16.5000 9.7000
-30.7000 29.0000
ans =
9 12 15
19 26 33
ans =
1 4
9 16
ans =
0.4348 0.2083
-0.3191 80.0000
A = [1 2 3;4 5 6;7 8 9]
8
Online STTP on MATLAB
based Teaching-Learning in
Mathematics, Science and En-
A(1,1) % gives elementgineering, MAY 18-22,
at location 1,12020
in A
A(3,2)
A =
1 2 3
4 5 6
7 8 9
ans =
ans =
ans =
1
4
7
ans =
4 5 6
ans =
1 2
4 5
9
Online STTP on MATLAB
based Teaching-Learning in
Mathematics, Science and En-
gineering, MAY 18-22, 2020
X =
1 1
1 1
Y =
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
Z =
1 0 0 0
0 1 0 0
0 0 1 0
0 0 0 1
v =
1 2 3 4
A =
1 0 0 0
0 2 0 0
0 0 3 0
0 0 0 4
B =
10
Online STTP on MATLAB
based Teaching-Learning in
Mathematics, Science and En-
0 0 1 gineering,
0 MAY
0 18-22,
0 2020
0 0 0 2 0 0
0 0 0 0 3 0
0 0 0 0 0 4
0 0 0 0 0 0
0 0 0 0 0 0
C =
0 0 0 0 0 0 0
0 0 0 0 0 0 0
0 0 0 0 0 0 0
1 0 0 0 0 0 0
0 2 0 0 0 0 0
0 0 3 0 0 0 0
0 0 0 4 0 0 0
|A 0 .. 0|
Y = BLKDIAG(A,B,...) produces |0 B .. 0|
|0 0 .. |
B = [1 2 3;4 5 6;7 8 9]
C = [1:4;5:8;9:12;13:16]
D = blkdiag(A,B,C)
11
Online STTP on MATLAB
based Teaching-Learning in
Mathematics, Science and En-
% matrices A, B and C gineering,
are placedMAY 18-22, 2020
as blocks
% in the diagonal of matrix D.
A =
1 2
3 4
B =
1 2 3
4 5 6
7 8 9
C =
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
D =
1 2 0 0 0 0 0 0 0
3 4 0 0 0 0 0 0 0
0 0 1 2 3 0 0 0 0
0 0 4 5 6 0 0 0 0
0 0 7 8 9 0 0 0 0
0 0 0 0 0 1 2 3 4
0 0 0 0 0 5 6 7 8
0 0 0 0 0 9 10 11 12
0 0 0 0 0 13 14 15 16
12
Online STTP on MATLAB
based Teaching-Learning in
Mathematics, Science and En-
sym/triu gineering, MAY 18-22, 2020
A =
B =
C =
D =
13
Online STTP on MATLAB
based Teaching-Learning in
Mathematics, Science and En-
0 0.9619 gineering,
0.3998 MAY 18-22, 2020
0.2638 0.1450 0.0760
0 0 0.2599 0.1455 0.8530 0.2399
0 0 0 0.1361 0.6221 0.1233
0 0 0 0 0.3510 0.1839
A =
14
Online STTP on MATLAB
based Teaching-Learning in
Mathematics, Science and En-
B = gineering, MAY 18-22, 2020
0.2400 0 0 0 0 0
0.4173 0.3377 0 0 0 0
0.0497 0.9001 0.4039 0 0 0
0.9027 0.3692 0.0965 0.2348 0 0
0.9448 0.1112 0.1320 0.3532 0.7317 0
0.4909 0.7803 0.9421 0.8212 0.6477 0.6868
C =
0 0 0 0 0 0
0.4173 0 0 0 0 0
0.0497 0.9001 0 0 0 0
0.9027 0.3692 0.0965 0 0 0
0.9448 0.1112 0.1320 0.3532 0 0
0.4909 0.7803 0.9421 0.8212 0.6477 0
D =
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0.9448 0 0 0 0 0
0.4909 0.7803 0 0 0 0
E =
Hilbert matrix
help hilb
15
Online STTP on MATLAB
based Teaching-Learning in
Mathematics, Science and En-
gineering, MAY
style where conventional FOR 18-22,
or DO2020
loops are replaced by
vectorized statements.
A =
1.0000 0.5000
0.5000 0.3333
B =
C =
Vandermonde matrix
help vander
16
Online STTP on MATLAB
based Teaching-Learning in
Mathematics, Science and En-
doc vander gineering, MAY 18-22, 2020
A = vander(v)
v =
1 2 3 4
A =
1 1 1 1
8 4 2 1
27 9 3 1
64 16 4 1
Pascal Matrix
help pascal
17
Online STTP on MATLAB
based Teaching-Learning in
Mathematics, Science and En-
gineering,has
Ainv = inv(A) % its inverse MAYinteger
18-22, 2020entries.
A =
1 1 1
1 2 3
1 3 6
Ainv =
3 -3 1
-3 5 -2
1 -2 1
B =
1 1 1 1 1 1 1
1 2 3 4 5 6 7
1 3 6 10 15 21 28
1 4 10 20 35 56 84
1 5 15 35 70 126 210
1 6 21 56 126 252 462
1 7 28 84 210 462 924
ans =
8 1 6
3 5 7
18
Online STTP on MATLAB
based Teaching-Learning in
Mathematics, Science and En-
4 9 2 gineering, MAY 18-22, 2020
ans =
64 2 3 61 60 6 7 57
9 55 54 12 13 51 50 16
17 47 46 20 21 43 42 24
40 26 27 37 36 30 31 33
32 34 35 29 28 38 39 25
41 23 22 44 45 19 18 48
49 15 14 52 53 11 10 56
8 58 59 5 4 62 63 1
help j
size(c)
J Imaginary unit.
As the basic imaginary unit SQRT(-1), i and j are used to enter
complex numbers. For example, the expressions 3+2i, 3+2*i, 3+2j,
3+2*j and 3+2*sqrt(-1) all have the same value.
Since both i and j are functions, they can be overridden and used
as a variable. This permits you to use i or j as an index in FOR
loops, subscripts, etc.
See also I.
c =
1.0000 + 3.0000i
-2.0000 - 5.0000i
10.0000 + 8.0000i
4.0000 - 3.0000i
-8.0000 + 7.0000i
ans =
19
Online STTP on MATLAB
based Teaching-Learning in
Mathematics, Science and En-
5 1 gineering, MAY 18-22, 2020
ans =
Columns 1 through 4
Column 5
-8.0000 - 7.0000i
abs(v2)
v1 =
-6.3000
2.3000
20
Online STTP on MATLAB
based Teaching-Learning in
Mathematics, Science and En-
-8.9600 gineering, MAY 18-22, 2020
-0.0020
4.6800
-9.6500
ans =
6.3000
2.3000
8.9600
0.0020
4.6800
9.6500
v2 =
0.1835
0.3685
0.6256
0.7802
0.0811
ans =
0.1835
0.3685
0.6256
0.7802
0.0811
21
Online STTP on MATLAB
based Teaching-Learning in
Mathematics, Science and En-
gineering, MAY 18-22, 2020
NORM(V,Inf) returns the largest element of ABS(V).
Computation of 1-Norm
v = rand(7,1)
sum(abs(v))
v =
0.9294
0.7757
0.4868
0.4359
0.4468
0.3063
0.5085
v =
-0.4294
-0.2757
0.0132
22
Online STTP on MATLAB
based Teaching-Learning in
Mathematics, Science and En-
0.0641 gineering, MAY 18-22, 2020
0.0532
0.1937
-0.0085
OneNorm =
1.0378
ans =
1.0378
Computation of 2-Norm
TwoNorm = norm(v,2) % computes 2-norm of v.
TwoNorm =
0.5523
Computation of p-Norm
% MATLAB can compute p-norm of a vector where p is a positive number.
p = 3
p = 6
p =
ThreeNorm =
0.4759
p =
23
Online STTP on MATLAB
based Teaching-Learning in
Mathematics, Science and En-
6 gineering, MAY 18-22, 2020
SixNorm =
0.4348
Computation of infinity-Norm
InfNorm = norm(v,inf) % computes infinity norm of v.
max(abs(v))
InfNorm =
0.4294
ans =
0.4294
v3 =
0.5108 + 0.5502i
0.8176 + 0.6225i
0.7948 + 0.5870i
0.6443 + 0.2077i
0.3786 + 0.3012i
0.8116 + 0.4709i
0.5328 + 0.2305i
0.3507 + 0.8443i
0.9390 + 0.1948i
0.8759 + 0.2259i
ans =
0.7507
1.0276
0.9881
0.6770
24
Online STTP on MATLAB
based Teaching-Learning in
Mathematics, Science and En-
0.4838 gineering, MAY 18-22, 2020
0.9383
0.5805
0.9143
0.9590
0.9046
A =
TwoNorm =
1.7497
References
% 1. MATLAB Documentation (URL: https://in.mathworks.com/help/
matlab/).
25