Matlab Last Lec
Matlab Last Lec
with applications
3. Mathematical
Operations with Arrays
Context of This Chapter
• Addition and subtraction of arrays
• Element-by-element operations
Addition and Subtraction
a11 a12 a13 b11 b12 b13 a11 b11 a12 b12 a13 b13
A= , B= AB =
21 22 23
a a a 21 22 23
b b b a b a
21 21 22 22 23 23
b a b
>> 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
– 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)
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
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
-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
>> 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
-20
y
>> x = [1:0.03:3]; -40
-60
>> y = (x.^3 + 5*x)./(4*x.^2 - 10); -80
0.8
0.2
Columns 8 through 13 0
-0.4
y= -0.8
-1
Columns 1 through 7 0 1 2 3 4 5 6
( 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
Columns 1 through 4
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)
• 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
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=
>> 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
» n=1:10;
» s= sum(1./2.^n)
s=
0.9990