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

Matlab Last Lec

This document provides an introduction to performing mathematical operations with arrays in MATLAB. It discusses how to add, subtract, multiply and divide arrays. It also covers element-by-element operations and using arrays to represent and solve systems of linear equations. Key points include: arrays can be added or subtracted element-by-element; matrix multiplication follows rules of linear algebra; multiplication is non-commutative; the inverse of a matrix allows division; and left or right division can solve systems of linear equations. Exercises are provided to work with matrices and solve sample systems.

Uploaded by

Muhammad Rashid
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)
55 views

Matlab Last Lec

This document provides an introduction to performing mathematical operations with arrays in MATLAB. It discusses how to add, subtract, multiply and divide arrays. It also covers element-by-element operations and using arrays to represent and solve systems of linear equations. Key points include: arrays can be added or subtracted element-by-element; matrix multiplication follows rules of linear algebra; multiplication is non-commutative; the inverse of a matrix allows division; and left or right division can solve systems of linear equations. Exercises are provided to work with matrices and solve sample systems.

Uploaded by

Muhammad Rashid
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/ 46

MATLAB: An introduction

with applications
3. Mathematical
Operations with Arrays
Context of This Chapter
• Addition and subtraction of arrays

• Multiplication, division, and exponentiation

• Element-by-element operations
Addition and Subtraction
 a11 a12 a13  b11 b12 b13   a11  b11 a12  b12 a13  b13 
A= , B=   AB =  
 21 22 23 
a a a  21 22 23 
b b b a  b a 
 21 21 22 22 23 23 
b a  b

>> v = [3 6 2]; w=[9 -1 5]; >> D = A + B


>> z = v + w D = 12 4 11 When a scalar is
z= -3 19 6 added to an array,
12 5 7 >> D-2 the scalar is
>> A=[2 -3 7; 8 4 5]; ans = 10 2 9 added to all the
>> B=[10 7 4; -11 15 1]; -5 17 4 elements of the
>> C = A - B >> C * 2 array.
C= ans =
-8 -10 3 -16 -20 6
19 -11 4 38 -22 8
Array Multiplication
• The multiplication operation * is executed by MATLAB according
to the rules of linear algebra.
b11 b12 
 a11 a12 a13   
A=  , B = b21 b22 
a21 a22 a23 
b31
 b32 
 a11b11 + a12b21 + a13b31 a11b12 + a12b22 + a13b32 
 AB =  
a21b11 + a22b21 + a23b31 a21b12 + a22b22 + a23b32 

>> A = [ 1 4 3; 2 6 1; 5 2 8];
>> B = [5 4; 1 3; 2 6]; 1 4 3 5 4 
   
>> C = A*B  2 6 1 
 1 3 
5 2 8 2 6
T= 15 34    
18 32 1 5 + 4 1 + 3  2 1 4 + 4  3 + 3  6  15 34 
43 74    
= 2  5 + 6 1 + 1 2 2  4 + 6  3 + 1 6  = 18 32 
>> D = B*A 5  5 + 2 1 + 8  2 5  4 + 2  3 + 8  6 43 74
??? Error using ==> mtimes    
Inner matrix dimensions must agree.
Array Multiplication
• The multiplication of matrices is not commutative.
A*B ≠ B*A

 w1   w1   w1v1 w1v2 w1v3 


     
v1 v2 v3   w2  = v1w1 + v2 w2 + v3 w3 ,  w2   v1 v2 v3  =  w2 v1 w2 v2 w2 v3 
 w3   w3   w3v1 w3v3 
     w3v2

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


>> C = A*B, D = B*A % A*B≠B*A
C=
28 56
47 43
D=
54 69
34 17
Array Multiplication
>> v = [ 6 2 4 ]; w = [3; 9; 5]; >> G*F
>> x = v*w ans =
14 26
x=
v*w 31 45
56
≠ w*v >> A=[2 5 7 0; 10 1 3 4; 6 2 11 5]
>> z = w*v
A=
z= 2 5 7 0
18 6 12 10 1 3 4
54 18 36 6 2 11 5
30 10 20 >> C = 3*A
>> dot(v, w) % v*w C=
ans = 6 15 21 0
56 30 3 9 12
>> F=[1 3; 5 7]; G=[4 2; 1 6]; 18 6 33 15
>> F*G >> D=A*3
ans = D=
7 20 6 15 21 0
27 52 30 3 9 12
18 6 33 15
A System of Linear Equations
• Linear algebra rules of array multiplication provide a
convenient way for writing a system of linear
equations.

a11 x1 + a12 x2 + a13 x3 = b1  a11 a12 a13   x1   b1 


a21 x1 + a22 x2 + a23 x3 = b2 a a23   x  = b   Ax = b
 21 a22  2  2
a31 x1 + a32 x2 + a33 x3 = b3  a31 a32 a33   x3  b3 
Array Division
• Identity matrix: eye(n)
– A square matrix in which the diagonal
elements are 1s, and the rest of the
elements are 0s.

– AI=IA=A

1 0 0  1 0 0   7   7 
6 2 9    6 2 9  0 1 0   3  =  3 
5 10 4  0 1 0  = 5 10 4  ,      
      0 0 1  9  9 
 0 0 1 
Array Division
• Inverse of a matrix
– The matrix B is the inverse of the matrix A if, when the two
matrices are multiplied, the product is the identity matrix.
– BA=AB=I
– A^-1, or inv(A)

>> A= [ 7 4 6; 3 1 8; 2 5 4]; >> A^-1 % inverse of A


>> B=inv(A) ans =
B= 0.2278 -0.0886 -0.1646
0.2278 -0.0886 -0.1646 -0.0253 -0.1013 0.2405
-0.0253 -0.1013 0.2405 -0.0823 0.1709 0.0316
-0.0823 0.1709 0.0316
>> A*B % equals to B*A >> A*A^-1 % AA-1=I
5.55 e-17
ans = ans =
1.0000 0.0000 0.0000 1.0000 0.0000 0.0000
0 1.0000 0 0 1.0000 0
-0.0000 0.0000 1.0000 -0.0000 0.0000 1.0000
Array Division
• Determinants
– A matrix has an inverse only if it is square and its
determinant is not equal to zero.
– Denoted by det(A) or |A|
a11 a12
A= = a11a22 − a12 a21
a21 a22
a11 a12 a13
B = a21 a22 a23 = a11a22 a33 + a12 a23 a31 + a13 a21a32 − a23 a32 a11 − a33 a21a12
a31 a32 a33
1 2 0
>> A= [ 1 2 0; 0 1 1; 3 0 1]; det(A)  
A = 0 1 1 
ans = 3 0 1 
7  
>> B=[ 1 2 1; 0 1 1; 3 1 1]; det(B) 1 2 1
 
ans = B = 0 1 1
3 3 1 1
 
Array Division
• Left division, \
– AX=B, A-1AX=A-1B
– X=A-1B
– X=A\B

– The solution X is obtained numerically with a method


based on Gauss elimination.
– The left division is recommended for solving a set of
linear equations because the calculation of the inverse
may be less accurate than the Gauss elimination method
when large matrices are involved.
Array Division
• Right division, /
– XC=D, X and D are row vectors.
– X•CC-1=D•C-1
– X=D•C-1

– In MATLAB the last equation can be written using


the right division character:
– X=D/C
Array Division
• Use matrix operations to solve the following system of linear equations.
4x – 2y + 6z = 8
2x + 8y + 2z = 4
6x + 10y + 3z = 0
The above system of equations can be written in the matrix form
AX=B or in the form XC=D
 4 −2 6   x  8   4 2 6
 2 8 2  y  =  4 z   −2 8 10 = 8 4 0
      or x y
6 10 3  z   0   6 2 3 

>> A=[ 4 -2 6; 2 8 2; 6 10 3]; b=[8; 4; 0]; Ax = b


>> x=A\ b % left division → A-1Ax=A-1b
x = -1.8049 → x=A\b
0.2927
xC=w
2.6341
→ xCC-1=wC-1
>> C=[4 2 6;-2 8 10; 6 2 3]; w = [8 4 0];
→ x = w/C
>> x = w/C % right division
x = -1.8049 0.2927 2.6341
Exercises
• Create the following three matrices:

3 5 − 2  − 1 4 1 1 2 6 
A = 4 2 − 6 B = − 2 5 6 C = 3 0 − 3
1 0 5   0 7 8 3 5 − 2

• Use the matrices A,B, and C to answer the following:


• (a) Does A*B=B*A?
• (b) Does (A*B)t=Bt*At? (t means transpose)
• (c) Does (A+B)t=At+Bt
Exercises
• Solve the following system of three linear equations:

4x-2.5y+3z=22.2
6x-4y-2z=16.6
2x+4y-5.5z=-11.3
Exercises
• Solve the following system of three linear equations:

4x-2.5y+3z=22.2
6x-4y-2z=16.6
2x+4y-5.5z=-11.3

A=[4 -2.5 3; 6 -4 -2; 2 4 -5.5]


b=[22.2; 16.6; -11.3]
x=A\b or inv(A)*b
Or
C=[4 6 2;-2.5 -4 4;3 -2 -5.5],D=[22.2,16.6,-11.3]
x=D/C
Exercises
• Solve the following system of five linear equations:

-u+0.5v+2w-1.75x+y=9.5
3u+12v-9w+1.5x-6y=-4.5
3u-1.5v+w+1.25x+0.5y=-11.5
3u+2v-w+1.5x-3y=-3.5
6u+3v+2w+x+8y=-23.5
Exercises
• Solve the following system of five linear equations:

-u+0.5v+2w-1.75x+y=9.5
3u+12v-9w+1.5x-6y=-4.5
3u-1.5v+w+1.25x+0.5y=-11.5
3u+2v-w+1.5x-3y=-3.5
6u+3v+2w+x+8y=-23.5

A=[-1 0.5 2 -1.75 1; 3 12 -9 1.5 -6; 3 -1.5 1 1.25 0.5;3 2 –1 1.5


-3; 6 3 2 1 8]
b=[9.5; -4.5; -11.5; -3.5; -23.5]
x=A\b or inv(A)*b
Element-by-Element Operations
• Element-by-element operations can be done only with arrays of
the same size.

• .* (Multiplication), .^(Exponentiation), ./(Right division), .\(Left


division)

• If two vectors v and w are v=[v1, v2, v3], w=[w1 w2 w3]


v.*w = [ v1w1, v2w2, v3w3 ] v./w = [ v1/w1, v2/w2, v3/w3 ]
v.^w = [ v1w1, v2w2, v3w3 ]
For two matrices A and B,
 a11 a12 a13   b11 b12 b13   a11b11 a12b12 a13b13 
A.* B =  a21 a22 a23  b21 b22 b23  =  a21b21 a22b22 a23b23 
 a31 a32 a33  b31 b32 b33   a31b31 a32b32 a33b33 
Element-by-Element Operations
 a11 a12 a13  b11 b12 b13   a11 / b11 a12 / b12 a13 / b13 
     
A. / B = a21 a22 a23  ./ b21 b22 b23  = a21 / b21 a22 / b22 a23 / b23 
a a33  b31 b32 b33   a31 / b31 a33 / b33 
 31 a32 a32 / b32
 a11n a12n a13n 
 n n 
A.^ n = a21 n
a22 a23  >> C=A./B
a n n
a32 n 
a33 C=
 31  2.0000 1.5000 0.3000
1.6667 4.0000 0.5714
 2 6 3 >> A=[2 6 3; 5 8 4];
A=  >> 2.^B
 5 8 4  >> B=[1 4 10; 3 2 7];
ans =
>> A.*B
1 4 10 ans = 2 16 1024
B= 
2 24 30 8 4 128
3 2 7 
15 16 28 >> A*B
??? Error using==> mtimes
Inner matrix dimensions must agree.
Element-by-Element Operations
• Element-by-element calculations are very useful for
calculating the value of a function at many values of
its argument.
• y(x)= 2x2- 5x+1
>> x = 1:7
x=
1 2 3 4 5 6 7
>> y = 2*x.^2 – 5*x + 1
y=
-2 -1 4 13 26 43 64

• y(x) = (x3 + 5x)/(4x2 – 10)


>> x = [1:0.03:3];
>> y = (x.^3 + 5*x)./(4*x.^2 - 10);
Element-by-Element Operations
• Element-by-element calculations are very useful for
calculating the value of a function at many values of
its argument. y=2x - 5x + 1
70
2

• y(x)= 2x2- 5x+1 60


50

>> x = 1:7 40
30

y
x=
20
1 2 3 4 5 6 7 10
>> y = 2*x.^2 – 5*x + 1 0

y= -10
2 4 6
x
-2 -1 4 13 26 43 64
y=(x.3 + 5x)/(4x2 - 10)
60
>> plot(x, y) 40

• y(x) = (x3 + 5x)/(4x2 – 10) 20

-20
y
>> x = [1:0.03:3]; -40

-60
>> y = (x.^3 + 5*x)./(4*x.^2 - 10); -80

>> plot(x, y) -100


1 1.5 2 2.5 3
x
Using Arrays in MATLAB Built-In
Math Functions
• When the argument is an array, the operation that is
defined by the function is executed on each element
of the array.
>> x=[0:pi/6:2*pi]
x= 1

0.8

Columns 1 through 7 0.6

0 0.5236 1.0472 1.5708 2.0944 2.6180 3.1416 0.4

0.2

Columns 8 through 13 0

3.6652 4.1888 4.7124 5.2360 5.7596 6.2832 -0.2

-0.4

>> y=cos(x) -0.6

y= -0.8

-1

Columns 1 through 7 0 1 2 3 4 5 6

1.0000 0.8660 0.5000 0.0000 -0.5000 -0.8660 -1.0000


Columns 8 through 13
-0.8660 -0.5000 -0.0000 0.5000 0.8660 1.0000
>> plot(x, y), grid on
Using Arrays in MATLAB Built-In
Math Functions
• Function sqrt(x)

>> d=[1 4 9; 16 25 36; 49 64 81]


d=
1 4 9
16 25 36
49 64 81
>> h=sqrt(d)
h=
1 2 3
4 5 6
7 8 9
Exercises
• For the function y = 0.8x3 − 2.1x 2 + 0.75 x , calculate the
value of y for the following values of x using element-
by-element operations: -2,-1,0,1,2,3,4

( x 2 − 2)( x + 4)
• For the function y = , calculate the value
of y for the following values 2 xof x using element-by-
element operations: 1,2,3,4,5,6,7
Exercises
• For the function y = 0.8 x3 − 2.1x 2 + 0.75 x, calculate the value
of y for the following values of x using element-by-
element operations: -2,-1,0,1,2,3,4
• x=-2:4;
• y=0.8*x.^3-2.1*x.^2+0.75*x
• y = -16.3000 -3.6500 0 -0.5500 -0.5000
4.9500 20.6000
( x 2 − 2)( x + 4)
y=
• For the function 2 x , calculate the value of y
for the following values of x using element-by-element
operations: 1,2,3,4,5,6,7
• X=1:7;
• y=(X.^2-2).*(X+4)./(2*sqrt(X))
• y = -2.5000 4.2426 14.1451 28.0000 46.2866
69.4022 97.7038
Exercises
• Define the vector v=[2 4 6 8 10]. Then use the vector in a
mathematical expression to create the following vectors:

1 1 1 1 1
• (a) a=
2 4 6 8 10 
1 1 1 1 1 
• (b) b= 2
2 42 62 82 102 

• (c) c = 1 2 3 4 5

a=1./v
b=1./v.^2
c=v/2
Exercises
• Define the vector v=[2 4 6 8 10]. Then use the vector in a
mathematical expression to create the following vectors:
1 1 1 1 1 
• (a) a =  2 4 6 8 10 
 
• (b) b =  2
1 1 1 1 1 
2 42 62 82 102 
• (c) c = 1 2 3 4 5

a=1./v
a = 0.5000 0.2500 0.1667 0.1250 0.1000
b=1./v.^2
b = 0.2500 0.0625 0.0278 0.0156 0.0100
c=v/2
Exercises
• Define x and y as the vector x=[1 3 5 7 9] and y=[2 5
8 11 14]. Then use them in the following expressions
to calculate z using element-by-element calculations.

xy 2
• (a) z=
x+ y
• z=x.*y.^2./(x+y)

• (b) z = x( x 2 − y ) − ( x − y ) 2
z=x.*(x.^2-2)-(x-y).^2
Exercises
• Define x and y as the vector x=[1 3 5 7 9] and y=[2 5
8 11 14]. Then use them in the following expressions
to calculate z using element-by-element calculations.

xy 2
z=
• (a) x+ y
• z=x.*y.^2./(x+y)
• z = 1.3333 9.3750 24.6154 47.0556 76.6957
(b) z = x( x − y ) − ( x − y )
2 2

• z=x.*(x.^2-2)-(x-y).^2
• z = -2 17 106 313 686
Exercises
sin x
• Show that lim
x →0
= 1.
x

Do this by first creating a vector that has the


elements 1.5, 1.0, 0.5, 0.01, 0.005, and 0.00005. Then
create a new vector y in which each element is
determined from the elements of x by sin x .
x
Compare the elements of y with the value 1 (use
format long to display the numbers).
Exercises
sin x
• Show that lim = 1.
x →0 x
Do this by first creating a vector that has the elements 1.5, 1.0, 0.5, 0.01, 0.005, and
0.00005. Then create a new vector y in which each element is determined from the
elements of x by sin x .
x
Compare the elements of y with the value 1 (use format long to display the numbers).

>> x=[1.5 1.0 0.5 0.01 0.005 0.00005];


>> format long
>> y=sin(x)./x
y=

Columns 1 through 4

0.664996657736036 0.841470984807897 0.958851077208406 0.999983333416666

Columns 5 through 6

0.999995833338542 0.999999999583333
• The median is the middle value when the data are arranged in as
cending order(1...........n)

• The mode of a sample is the most frequent value

• Mean: arithmetic mean

• Standard deviation
Built-In Functions for Analyzing
Arrays
Function Description Example
mean(A) If A is a vector, returns the mean value of the >> A=[5,9,2,4];
elements of the vector >> mean(A)
ans = 5
C=max(A) If A is a vector, C is the largest element in A. If A >> A=[5,9,2,4,11,6,11,1];
is a matrix, C is a row vector containing the >> C=max(A)
C = 11
largest element of each column of A.
[d,n]=max(x) If A is a vector, d is the largest element in A, and >>[d,n]=max(A)
n is the position of the element (the first if d = 11
n= 5
several have the max value.)
min(A) The same as max(A), but for the smallest >> A=[5,9,2,4];
element. >> min(A)
ans = 2
[d,n]=min(A) The same as [d, n]=max(A), but for the smallest
element.
sum(A) If A is a vector, returns the sum of the elements >> A=[5,9,2,4];
of the vector. >> sum(A)
ans = 20
Function Description Example
sort(A) If A is a vector, arranges the elements of the vector in >> A=[5,9,2,4];
ascending order. >> sort(A)
ans = 2 4 5 9
median(A) If A is a vector, returns the median value of the >> median(A)
elements of the vector. ans = 4.5

std(x) If A is a vector, returns the standard deviation of the >> std(A)


elements of the vector. ans = 2.9439

det(A) Returns the determinant of a square matrix A. >> A=[2, 4; 3, 5];


>> det(A)
ans = -2
dot(a,b) Calculates the scalar (dot) product of two vectors a >> a=[1,2,3]; b=[3,4,5];
and b. The vectors can each be row or column vectors. >> dot(a, b), cross(a, b)
ans = 26
cross(a,b) Calculates the cross product of two vectors a and b ans = -5 3 -2
(aXb). The two vectors must have each three elements.
inv(A) Returns the inverse of a square matrix A. >> A=[2,-2,1;3,2,-1;2,-
3,2];
>> inv(A)
ans = 0.2000 0.2000 0
-1.6000 0.4000 1.0000
-2.6000 0.4000 2.0000
Generation of Random Numbers
• rand, randn, randi
Command Description Example
rand Generates a single random number between 0 >> rand
and 1. ans = 0.2311

rand(1,n) Generates an n-element row vector of random >> a=rand(1,4)


A=
numbers between 0 and 1.
0.6068 0.4860 0.8913
0.7621
rand(n) Generates an nxn matrix with random numbers >>b=rand(3)
between 0 and 1. b=
0.4565 0.4447 0.9218
0.0185 0.6154 0.7382
0.8214 0.7919 0.1763
rand(m,n) Generates an mxn matrix with random numbers >> c=rand(2,3)
between 0 and 1. c=
0.4057 0.9169 0.8936
0.9355 0.4103 0.0579
randperm(n) Generates a row vector with n elements that are >> c=randperm(8)
random permutation of integers 1 through n. ans =
82743651
Generation of Random Numbers
• Sometimes there is a need for random numbers that
are distributed in an interval other than (0,1), or for
numbers that are integers only.

• Range (a,b): (b-a)*rand+a

• A vector of 10 elements with random values between


-5 and 10 (a=-5, b=10)
>> v=15*rand(1,10)-5
v = 7.2209 8.5869 -3.0952 8.7006 4.4854 -3.5369 -0.8225 3.2032 9.3626
9.4733
Randi
• The randi command generates uniformly distributed
random integer.

Command Description Example


randi Generates a single random number between 1 >> a=randi(15)
and imax. a=
9
randi(imax,n) Generates an mxn matrix with random integers >> c=randi(15,3)
between 1 and imax. b=
4 8 11
14 3 8
1 15 8
randi Generates an mxn matrix with random integers >> c=randi(15,2,4)
(imax,m,n) between 1 and imax. c=
1 1 8 13
11 2 2 13
Randi
• The range of the random integers can be set to be
between any two integers by typing [imin imax]
instead of imax.

>> d=randi([50 90], 3, 4)

d=

83 87 61 89
87 75 72 56
55 53 89 89
Randn
• The randn command generates normally distributed
numbers with mean 0 and standard deviation of 1.

>> d=randn(3,4)

d=

0.7254 -0.2050 1.4090 -1.2075


-0.0631 -0.1241 1.4172 0.7172
0.7147 1.4897 0.6715 1.6302
Randn
• The mean and standard deviation of the numbers can be changed
by mathematical operations to have any values.

• Ex) six numbers with a mean of 50 and standard deviation of 6

>> v=6*randn(1,6)+50

v=
51.9556 54.1388 52.9075 48.7862 51.1755 46.8509

>> w=round(6*randn(1,6)+50)

w=
54 45 46 47 38 56
Exercises
• Define the vectors:
u = −2i + 6j + 5k, v = 5i − 1j + 3k, w = 4i + 7 j − 2k

Use the vectors to verify the identity:


u  ( v  w) = v(u  w) − w(u  v)

Using MATLAB’s built-in functions cross, calculate


the value of the left and right sides of the identity.
Exercises
• Define the vectors:
u = −2i + 6j + 5k, v = 5i − 1j + 3k, w = 4i + 7 j − 2k

Use the vectors to verify the identity:


u  ( v  w) = v(u  w) − w(u  v)

Using MATLAB’s built-in functions cross, calculate


the value of the left and right sides of the identity.
u=[-2 6 5]; v=[5 -1 3]; w=[4 7 -2];
cross(u,cross(v,w))
dot(u,w)*v-dot(u,v)*w
Exercises
• Use MATLAB to show that the sum of the infinite

series  1 = 1 + 1 + 1 +  converges to 1.
n
n =1 2 2 22 23

Do it by computing the sum for:

(a) n =10 (b) n=20


(c) n=30 (d) n=40
Exercises
• Use MATLAB to show that the sum of the infinite series

converges to 1. 1 1 1 1

n =1 2
n
= + 2 + 3 +
2 2 2
Do it by computing the sum for:

(a) n =10 (b) n=20


(c) n=30 (d) n=40

» n=1:10;
» s= sum(1./2.^n)
s=
0.9990

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