0% found this document useful (0 votes)
53 views

Online STTP On MATLAB Based Teaching-Learning in Mathematics, Science and Engineering, MAY 18-22, 2020

The document provides instructions for defining arrays and matrices and computing norms in MATLAB. It demonstrates how to create vectors and matrices, perform basic operations on them, and generate special matrices like diagonal, identity, Hilbert and Vandermonde matrices. It also shows how to compute different norms of vectors and matrices.

Uploaded by

KirubaKaran S
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)
53 views

Online STTP On MATLAB Based Teaching-Learning in Mathematics, Science and Engineering, MAY 18-22, 2020

The document provides instructions for defining arrays and matrices and computing norms in MATLAB. It demonstrates how to create vectors and matrices, perform basic operations on them, and generate special matrices like diagonal, identity, Hilbert and Vandermonde matrices. It also shows how to compute different norms of vectors and matrices.

Uploaded by

KirubaKaran S
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/ 25

Online STTP on MATLAB

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

Organized by Department of Electronics Engi-


neering, Ramrao Adik Institute of Technology,
Nerul, Navi Mumbai
Demonstration Session: Defining Arrays and
Matrices and Computation of norms
Prepared by: Vishwesh A. Vyawahare, RAIT
Initial instructions
clear % clear the workspace

clc % clear the command window

close all % close all previously opened plot files

Defining Row Vectors


a = [1 2 3 4] % a 1 x 4 row vector.

b = [1.3 6.9 -8.45 9.33] % another 1 x 4 row vector

[rownumber,colnumber] = size(a)

a =

1 2 3 4

b =

1.3000 6.9000 -8.4500 9.3300

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

a - b % subtraction of two row vectors

% a*b % gives error as inner dimensions don't match.

a.*b % element-wise multiplication

a./b % element-wise division of two vectors

2.2*a % multiplying an array by a scalar

a(3) % retreiving the element at 3rd position in an array

c = [0.1 0.2 0.99] % three column vector

% a+c % this will not work as dimensions are different

sqrt(a) % elementwise square root of an array

a.^2 % elementwise square of an array

theta = [-pi -pi/2 -pi/4 0 pi/4 pi/2 pi] % defining a vector of angles
in radians

sin(theta) % sine function acts on each elemet of an array

cos(theta) % cosine function acts on each elemet of an array

ans =

2.3000 8.9000 -5.4500 13.3300

ans =

-0.3000 -4.9000 11.4500 -5.3300

ans =

1.3000 13.8000 -25.3500 37.3200

ans =

0.7692 0.2899 -0.3550 0.4287

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 =

0.1000 0.2000 0.9900

ans =

1.0000 1.4142 1.7321 2.0000

ans =

1 4 9 16

theta =

-3.1416 -1.5708 -0.7854 0 0.7854 1.5708 3.1416

ans =

-0.0000 -1.0000 -0.7071 0 0.7071 1.0000 0.0000

ans =

-1.0000 0.0000 0.7071 1.0000 0.7071 0.0000 -1.0000

Defining row matrices smartly


clear a b c % clears these variables from workspace

a = 1:5 % creates an array from 1 to 5 with step 1

b = -5:2:5 % creates an array from -5 to 5 with step 2

c = 0:0.1:1 % creates an array with elements from 0 to 1 with step 0.1

x = ones(1,5) % creates a row vector of 1s with 5 columns

y = zeros(1,7) % creates a row vector of zeros with 7 columns

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

0 0.1000 0.2000 0.3000 0.4000 0.5000 0.6000

Columns 8 through 11

0.7000 0.8000 0.9000 1.0000

x =

1 1 1 1 1

y =

0 0 0 0 0 0 0

Appending an element in an array


a = [a 6] % appends element 6 at the sixth position in an array

size(a) % number of columns have increased

a =

1 2 3 4 5 6

ans =

1 6

Some more basic functions for arrays


min(a) % gives the smallest element in an array

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

sum(a) % gives the sum of all elements in an array

ans =

ans =

ans =

21

Defining Column Vectors


clear a b c

a = [1;2;3;4;5] % new row is indicated by semicolon

[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

c = b' % this gives back the column vector

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

b = [2.3 9.6; -9.4 0.05] % defining another 2 x 2 square matrix

c = [1 2 3;4 5 6] % defining a non-square matrix (2 x 3)

a + b % addition of two matrices of same dimensions

a - b % subtraction of two matrices of same dimensions

a*b % multiplication of two matrices

% c*a % this will not work as inner dimensions don't match

a*c % this will work as inner dimensions match

a.^2 % element-wise square of a matrix

a./b % element-wise division of two matrices

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

Extracting an element, a row, a column or a mi-


nor from a matrix
clear a b c

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) % extracts first column from A

A(2,:) % extracts second row from A

A(1:2,1:2) % extracts the minor [1 2;4 5] from A

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

Defining special matrices


X = ones(2,2) % generates a 2 x 2 matrix of ones

Y = zeros(5,3) % generates a 5 x 3 matrix of zeros

Z = eye(4) % generates a 4 x 4 identity matrix

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

Generation of diagonal matrix


v = [1 2 3 4]

A = diag(v) % generates a square matrix with elements of v


% placed in its diagonal.

B = diag(v,2) % % generates a square matrix with elements of v


% placed in second diagonal above the main diagonal.

C = diag(v,-3) % % generates a square matrix with elements of v


% placed in third diagonal below the main diagonal.

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

Block diagonal matrix


help blkdiag

BLKDIAG Block diagonal concatenation of matrix input arguments.

|A 0 .. 0|
Y = BLKDIAG(A,B,...) produces |0 B .. 0|
|0 0 .. |

Class support for inputs:


float: double, single
integer: uint8, int8, uint16, int16, uint32, int32, uint64,
int64
char, logical

See also DIAG, HORZCAT, VERTCAT

Other functions named blkdiag:


InputOutputModel/blkdiag

Reference page in Help browser


doc blkdiag

Generation of block diagonal matrix


A = [1 2;3 4]

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

Upper trangular matrix


help triu

TRIU Extract upper triangular part.


TRIU(X) is the upper triangular part of X.
TRIU(X,K) is the elements on and above the K-th diagonal of
X. K = 0 is the main diagonal, K > 0 is above the main
diagonal and K < 0 is below the main diagonal.

See also TRIL, DIAG.

Other functions named triu:


gf/triu
codistributed/triu
gpuArray/triu

12
Online STTP on MATLAB
based Teaching-Learning in
Mathematics, Science and En-
sym/triu gineering, MAY 18-22, 2020

Reference page in Help browser


doc triu

Generation of Upper trangular matrix


A = rand(6)

B = triu(A) % extracts upper triangular part of A.

C = triu(A,3) % extracts the elements on and above the 3rd diagonal of


A.

D = triu(A,-1) % extracts the elements on and above the (-1)th


diagonal of A.

A =

0.9133 0.4427 0.8687 0.9106 0.5797 0.5132


0.1524 0.1067 0.0844 0.1818 0.5499 0.4018
0.8258 0.9619 0.3998 0.2638 0.1450 0.0760
0.5383 0.0046 0.2599 0.1455 0.8530 0.2399
0.9961 0.7749 0.8001 0.1361 0.6221 0.1233
0.0782 0.8173 0.4314 0.8693 0.3510 0.1839

B =

0.9133 0.4427 0.8687 0.9106 0.5797 0.5132


0 0.1067 0.0844 0.1818 0.5499 0.4018
0 0 0.3998 0.2638 0.1450 0.0760
0 0 0 0.1455 0.8530 0.2399
0 0 0 0 0.6221 0.1233
0 0 0 0 0 0.1839

C =

0 0 0 0.9106 0.5797 0.5132


0 0 0 0 0.5499 0.4018
0 0 0 0 0 0.0760
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0

D =

0.9133 0.4427 0.8687 0.9106 0.5797 0.5132


0.1524 0.1067 0.0844 0.1818 0.5499 0.4018

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

Lower trangular matrix


help tril

TRIL Extract lower triangular part.


TRIL(X) is the lower triangular part of X.
TRIL(X,K) is the elements on and below the K-th diagonal
of X . K = 0 is the main diagonal, K > 0 is above the
main diagonal and K < 0 is below the main diagonal.

See also TRIU, DIAG.

Other functions named tril:


gf/tril
codistributed/tril
gpuArray/tril
sym/tril

Reference page in Help browser


doc tril

Generation of Lower trangular matrix


A = rand(6)

B = tril(A) % extracts lower triangular part of A.

C = tril(A,-1) % extracts the elements on and below the (-1)th


diagonal of A.

D = tril(A,-4) % extracts the elements on and below (-4)th diagonal of


A.

E = tril(A,2) % extracts the elements on and below 2nd diagonal of A.

A =

0.2400 0.4893 0.3897 0.9561 0.0154 0.4509


0.4173 0.3377 0.2417 0.5752 0.0430 0.5470
0.0497 0.9001 0.4039 0.0598 0.1690 0.2963
0.9027 0.3692 0.0965 0.2348 0.6491 0.7447
0.9448 0.1112 0.1320 0.3532 0.7317 0.1890
0.4909 0.7803 0.9421 0.8212 0.6477 0.6868

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 =

0.2400 0.4893 0.3897 0 0 0


0.4173 0.3377 0.2417 0.5752 0 0
0.0497 0.9001 0.4039 0.0598 0.1690 0
0.9027 0.3692 0.0965 0.2348 0.6491 0.7447
0.9448 0.1112 0.1320 0.3532 0.7317 0.1890
0.4909 0.7803 0.9421 0.8212 0.6477 0.6868

Hilbert matrix
help hilb

HILB Hilbert matrix.


HILB(N) is the N by N matrix with elements 1/(i+j-1),
which is a famous example of a badly conditioned matrix.
See INVHILB for the exact inverse.

HILB(N,CLASSNAME) produces a matrix of class CLASSNAME.


CLASSNAME must be either 'single' or 'double' (the default).

This is also a good example of efficient MATLAB programming

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.

See also INVHILB.

Reference page in Help browser


doc hilb

Generation of Hilbert matrix


A = hilb(2) % generates a 2x2 Hilbert matrix.

B = hilb(3) % generates a 3x3 Hilbert matrix.

C = hilb(6) % generates a 6x6 Hilbert matrix.

A =

1.0000 0.5000
0.5000 0.3333

B =

1.0000 0.5000 0.3333


0.5000 0.3333 0.2500
0.3333 0.2500 0.2000

C =

1.0000 0.5000 0.3333 0.2500 0.2000 0.1667


0.5000 0.3333 0.2500 0.2000 0.1667 0.1429
0.3333 0.2500 0.2000 0.1667 0.1429 0.1250
0.2500 0.2000 0.1667 0.1429 0.1250 0.1111
0.2000 0.1667 0.1429 0.1250 0.1111 0.1000
0.1667 0.1429 0.1250 0.1111 0.1000 0.0909

Vandermonde matrix
help vander

VANDER Vandermonde matrix.


A = VANDER(V) returns the Vandermonde matrix whose columns
are powers of the vector V, that is A(i,j) = v(i)^(n-j).

Class support for input V:


float: double, single

Reference page in Help browser

16
Online STTP on MATLAB
based Teaching-Learning in
Mathematics, Science and En-
doc vander gineering, MAY 18-22, 2020

Generation of Vandermonde matrix


v = [1 2 3 4]

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

PASCAL Pascal matrix.


PASCAL(N) is the Pascal matrix of order N: a symmetric positive
definite matrix with integer entries, made up from Pascal's
triangle. Its inverse has integer entries.
PASCAL(N).^r is symmetric positive semidefinite for all
nonnegative r.

PASCAL(N,1) is the lower triangular Cholesky factor (up to signs


of columns) of the Pascal matrix. It is involutory (is its own
inverse).

PASCAL(N,2) is a rotated version of PASCAL(N,1), with sign flipped


if N is even, which is a cube root of the identity.

PASCAL(N,CLASSNAME) or PASCAL(N,K,CLASSNAME) produces a matrix of


class CLASSNAME. CLASSNAME must be either 'single' or 'double'
(the default).

Reference page in Help browser


doc pascal

Generation of Pascal matrix


A = pascal(3) % generates a Pascal matrix.

17
Online STTP on MATLAB
based Teaching-Learning in
Mathematics, Science and En-
gineering,has
Ainv = inv(A) % its inverse MAYinteger
18-22, 2020entries.

B = pascal(7) % pattern of Pascal triangle is visible.

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

Magic Square matrix


help magic

MAGIC Magic square.


MAGIC(N) is an N-by-N matrix constructed from the integers
1 through N^2 with equal row, column, and diagonal sums.
Produces valid magic squares for all N > 0 except N = 2.

Reference page in Help browser


doc magic

Generation of Magic Square matrix


magic(3) % sum of elements in each column, row, diagonal is 15.
magic(8) % sum of elements in each column, row, diagonal is 260

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

Defining complex vectors


% MATLAB can define complex arrays as well.

help j

c = [1+ j*3;-2-j*5;10+j*8;4-j*3;-8+j*7] % j = sqrt(-1).

size(c)

c' % transpose of complex arrays is transpose and conjugate.

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.

Reference page in Help browser


doc j

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

1.0000 - 3.0000i -2.0000 + 5.0000i 10.0000 - 8.0000i 4.0000 +


3.0000i

Column 5

-8.0000 - 7.0000i

Absolute values of real and complex vectors


help abs

v1 = [-6.3 2.3 -8.96 -0.002 4.68 -9.65]' % generating a column array


of reals.

abs(v1) % gives an array with all positive elements

v2 = rand(5,1) % generating an array of real random variables of size


5 x 1.

abs(v2)

ABS Absolute value.


ABS(X) is the absolute value of the elements of X. When
X is complex, ABS(X) is the complex modulus (magnitude) of
the elements of X.

See also SIGN, ANGLE, UNWRAP, HYPOT.

Other functions named abs:


embedded.numerictype/abs
duration/abs
codistributed/abs
gpuArray/abs
iddata/abs
cgslparser/abs
sym/abs

Reference page in Help browser


doc abs

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

Computation of norms of real vectors


help norm

NORM Matrix or vector norm.


NORM(X,2) returns the 2-norm of X.

NORM(X) is the same as NORM(X,2).

NORM(X,1) returns the 1-norm of X.

NORM(X,Inf) returns the infinity norm of X.

NORM(X,'fro') returns the Frobenius norm of X.

In addition, for vectors...

NORM(V,P) returns the p-norm of V defined as SUM(ABS(V).^P)^(1/


P).

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).

NORM(V,-Inf) returns the smallest element of ABS(V).

By convention, NaN is returned if X or V contains NaNs.

See also COND, RCOND, CONDEST, NORMEST, HYPOT.

Other functions named norm:


DynamicSystem/norm
codistributed/norm
gpuArray/norm
mfilt.norm
adaptfilt.norm
dfilt.norm
sym/norm

Reference page in Help browser


doc norm

Computation of 1-Norm
v = rand(7,1)

v = 0.5 - v % this will make some elements of v negative.

% norms are computed for above vector v.

OneNorm = norm(v,1) % computes 1-norm of v.

% it is sum of absolute values of elements of v.

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.

% This is the conventional Euclidean norm.

TwoNorm =

0.5523

Computation of p-Norm
% MATLAB can compute p-norm of a vector where p is a positive number.

p = 3

ThreeNorm = norm(v,p) % computes p-norm of v with p = 3

p = 6

SixNorm = norm(v,p) % computes p-norm of v with 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.

% it is same as the largest of the absolute values of all elements of


v.

max(abs(v))

v3 = rand(10,1) + j*rand(10,1) % array of complex random variables of


size 10 x 1.

abs(v3) % gives an array of magnitude of each complex element of v3.

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

Computation of norms of real matrices


A = rand(3) % generates a 3x3 matrix

TwoNorm = norm(A) % computes 2-norm of A

A =

0.1707 0.3111 0.1848


0.2277 0.9234 0.9049
0.4357 0.4302 0.9797

TwoNorm =

1.7497

References
% 1. MATLAB Documentation (URL: https://in.mathworks.com/help/
matlab/).

% 2. Strang, G., Linear Algebra and Its Applications, CENGAGE


LEARNING,
% India, 2005.

% 3. Hill, D. R., Zitarelli, D. E., Linear Algebra Labs with MATLAB,


% Pearson Education, USA, 2004.

% 4. Xue, D., Chen, YQ, Solving applied mathematical problems with


MATLAB,
% Chapman & Hall/CRC, USA, 2009.

Published with MATLAB® R2015a

25

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy