You are on page 1of 14

Lecture 3

Computer Application

Array operations
Array operations are divided into two classes:
1. Element-By-Element Operations
2. Matrix Operations

1. Element-By-Element Operations
The element-by-element operators in MATLAB are as follows:
 Element-by-element multiplication: ".*"
 Element-by-element division: "./"
 Element-by-element addition: "+"
 Element-by-element subtraction: "-"
 Element-by-element exponentiation: ".^"
NON-ACTIVATED VERSION
Example:
www.avs4you.com
Element by element operation when usingscalars (means numbers) and array

>> a = [1 2 3 4 5 6]
a=
1 2 3 4 5 6

>> b = a .* 2 the scalar is 2


b=
2 4 6 8 10 12

>> c = a .^ 2
c=
1 4 9 16 25 36

>> d=a+2
d=
3 4 5 6 7 8

The element-by-element multiplication and exponentiation operators have "."


Lecture 3
Computer Application

appended to the front of them, while the element-by-element addition and


subtraction operators do not. The reason is that there are other kinds of
multiplication, division, and exponentiation operators for matrices, which are not
element-by element, that are denoted by "* “, "/"and "^".

Example:
Element by element operation when using array–array:

>> a = [1 2 3] create a row vector

a=

1 2 3

>> b = [4 5 6] create b row vector

b=

4 5
NON-ACTIVATED VERSION
6

>> c=[4;5;6] www.avs4you.com


create c column vector

c=

>> a.*b element-by-element multiplication

ans =

4 10 18

>> a./b element-by-element division

ans =

0.2500 0.4000 0.5000

>> a.\b
Lecture 3
Computer Application

ans =

4.0000 2.5000 2.0000

>> a.*c

??? Error using ==> times

Matrix dimensions must agree.

Hint: Element by element operation when using array–array requires that the two
arrays have identical size and shape.

Example:
The viscosity of blood is to be determined from measurement of shear
stress,Ԏ, and the rate of sharing strain, du/dy , obtained from small blood sample
tested in suitable viscometer. Based on data given below determine if the blood is
NON-ACTIVATED VERSION
Newtonian or non-Newtonian fluid.

Ԏ 0.04 www.avs4you.com
0.06 0.12 0.18 0.30 0.52 1.12 2.1
(N/m)
du/dy 2.25 4.50 11.25 22.5 45.0 90.0 225 450
(s-1)

Solution:

1st we find viscosities and then if the viscosities are equal this mean that the slop
is identify at each time which mean that the fluid is Newtonian
>>T=[0.04 0.06 0.12 0.18 0.30 0.52 1.12 2.1]

T=

0.0400 0.0600 0.1200 0.1800 0.3000 0.5200 1.1200 2.1000

>>du=[2.25 4.50 11.25 22.5 45.0 90.0 225 450]

du =
Lecture 3
Computer Application

2.2500 4.5000 11.2500 22.5000 45.0000 90.0000 225.0000 450.0000

>>viscosity=T./du

viscosity =

0.0178 0.0133 0.0107 0.0080 0.0067 0.0058 0.0050 0.0047

The ratio of shear stress to the rate of sharing strain is not constant but decreases
as the rate of sharing increases thus the blood is Non-Newtonian fluid.

THE TRANSPOSE OPERATOR


The transpose operator, when applied to a vector, switches a row (column) vector
to a column (row) vector. When applied to a matrix, it switches the rows
(columns) to columns (rows). The transpose operator is applied by typing a single
NON-ACTIVATED VERSION
quote ' following the variable to be transposed.

www.avs4you.com
>> A=[ 1 2 3]
A=
123
>>B= A' switches A row vector to B column vector
B=
1
2
3

>> A=[1 2 3; 4 5 6; 7 8 9]
A=
123
456
789
>> B=A' switches the rowsof matrix A to columnsto create matrix B
B=
147
258
369

Example:
Lecture 3
Computer Application

Estimate the average boiling point of a Water/Ethanol system at different water


compositions (0, 0.1, 0.2 ,0.3…….1.0) knowing that.
Boiling point of water =100 o C.
Boiling point of Ethanol =78.35 o C.
Mixture boiling point= Xwater× TBpwater+ Xethanol × TBpethanol

Solution
You must write the code as following:

TBPwater=100;
TBPethanol=78.35;
X=[0:.1:1]
Xe=1-X
T=TBPwater* X + TBPethanol * Xe
Table=[X',Xe',T']

NON-ACTIVATED VERSION
Then you must press run, the output will be:

X=
www.avs4you.com
0 0.1000 0.2000 0.3000 0.4000 0.5000 0.6000 0.7000 0.8000 0.9000
1.0000

Xe =
1.0000 0.9000 0.8000 0.7000 0.6000 0.5000 0.4000 0.3000 0.2000 0.1000
0

T=
78.3500 80.5150 82.6800 84.8450 87.0100 89.1750 91.3400 93.5050 95.6700
97.8350 100.0000

Table =
0 1.0000 78.3500
0.1000 0.9000 80.5150
0.2000 0.8000 82.6800
0.3000 0.7000 84.8450
0.4000 0.6000 87.0100
0.5000 0.5000 89.1750
0.6000 0.4000 91.3400
0.7000 0.3000 93.5050
Lecture 3
Computer Application

0.8000 0.2000 95.6700


0.9000 0.1000 97.8350
1.0000 0 100.0000

2. Matrix Operation

 Addition and Subtraction


The operations + (addition) and - (subtraction) can be used with arrays of identical
size (the same number of rows and columns). The sum, or the difference of two
arrays is obtained by adding, Or subtracting, their corresponding elements.
NON-ACTIVATED VERSION
In general, if A and B are two arrays (for example 2 * 3 matrices),
Then, the matrix that is obtained by addingA and B is:
www.avs4you.com

 Multiplication of vectors
Two vectors can multiply each other only if both have the same number of
elements and one is a row vector and the other is a column vector.
The multiplication of a row vector times a column vector gives a 1 * 1 matrix,
which is a scalar. If A is a row vector and B is a column vector

B11
B21
B31

A= [ A11 A12 A13] B=


Lecture 3
Computer Application

Then, the matrix that is obtained by multiplication A by B (A*B) is

A*B= A11*B11+ A12 *B21+ A13*B31= Scalar

But, the matrix that obtained by multiplication B by A (B*A) is a matrix have size
(3*3)
B11*A11 B11*A12 B11*A13
B21*A11 B21*A12 B21*A13
B21*A11 B21*A12 B21*A13
B*A =

NON-ACTIVATED VERSION
 Multiplication of Matrix
IfA andB are two matrices, the operation A*B can be carried out only if the
www.avs4you.com
number of columns in matrixA is equal to the number of rows in matrix B.
The result is a matrix that has the same number of rows as A and the same
number of columns as B. For example, if A is a 4 * 3 matrix and B is a 3 x 2 matrix:

Then, the matrix that is obtained by the operation A*B has the dimension of 4 * 2
with the elements:

Example:
Lecture 3
Computer Application

If A and B are two matrix, find AB, BA.

1 8
7 5
3 6
9 -1
2 4

A= B=

Solution:

NON-ACTIVATED VERSION
A=[1 8;7 5;3 6]
B=[9 -1;2 4]

A*B www.avs4you.com
A=
1 8
7 5
3 6

B=
9 -1
2 4

ans =
25 31
73 13
39 21

An error will appear with operation of BA because the inner matrix dimensions
are not equal.

 Array division:
Lecture 3
Computer Application

MATLAB has two types of array division, which are the right division and the left
division.

Left division \:
The left division is used to solve the matrix equation AX = B. In this equation X
and B are column vectors. This equation can be solved by multiplying on the left

both sides by the inverse of A:

The left-hand side of this equation is X since:

NON-ACTIVATED VERSION
So, the solution ofwww.avs4you.com
AX = B is:
In MATLAB the last equation can be written by using the left division character:

In left division the solution X is obtained numerically with a method that is based
on the Gauss elimination method.
 Array exponentiation:
Raising a matrix to a power is equivalent to repeatedly multiplying the matrix by
itself, for example, A2 =A*A.
This process required the matrix to have the same number of rows and columns
(i.e. must be square). MATLAB uses the symbol ( ^ ) for matrix exponentiation.

Linear algebra equations


Linear algebra rules of array multiplication provide a convenient way for writing a
system of linear equations. For example, the following system of three equations
with three unknowns:
Lecture 3
Computer Application

can be written in a matrix form by:

and in matrix notation by:

By using left division, the solution x is obtained numerically with a method that is
NON-ACTIVATED VERSION
based on the Gauss elimination method.

www.avs4you.com
Example:
Use matrix operations to solve the following system of linear equations.

Solution:
The above system of equations can be written in the matrix form AX = B
Lecture 3
Computer Application

>> a=[4 -2 6;2 8 2;6 10 3];


>> B=[8;4;0];
>> x=a\B
x=
-1.8049 x
0.2927 y
2.6341 z

Hint: to solve any set of equation s, the number of equations must equal the
number of unknowns. This is true when the determinant of the coefficients matrix
is not equal zero.
If |A| =0, then the number of equations does not equal the number of unknown.
Type det (A) in command window to find the matrix determinant .

NON-ACTIVATED VERSION
www.avs4you.com

MATLAB special matrix functions


MATLAB has many built-in functions for managing and handling arrays. Some of
these are listed below:

command object
length (A) Return the length of the vector or largest dimension of a 2-
dimension array

linspace (a,b,n) Great a row vector of n regularly spaced values between a and b
logspace (a,b,n) Great a row vector of nlogarithmically spaced values between a
andbEspecially useful for creating frequency vectors, it is a
logarithmic equivalent of linspace and the ":" or colon operator.
Lecture 3
Computer Application

max(A) Return the algebraically largest element in if A is a vector.


Return a row vector containing the largest elements in each
column if A is a matrix.
min(A) Return the algebraically smallest element in if A is a vector.
Return a row vector containing the smallest elements in each
column if A is a matrix.
Sum(A) Sums the elements in each column of the array A and returns a
row vector containing the sums
det(A) Calculate determinant
inv(A) Calculate the inverse of a matrix
sort (A) Arranges elements in ascending order

Example
The following statements explain how you can use the special matrix functions
NON-ACTIVATED VERSION
>> A=[2,4,5;0:2;8,1,3]
A=
2 4 5
www.avs4you.com
0 1 2
8 1 3

>>length(A)
ans =
3

>>max(A)
ans =
8 4 5

>>min(A)
ans =
0 1 2

>>sum(A)
ans =
10 6 10
Lecture 3
Computer Application

>>size(A)
ans =
3 3

>>linspace (1,8,5)
ans =
1.0000 2.7500 4.5000 6.2500 8.0000

>>logspace(0,1,7)
ans =
1.0000 1.4678 2.1544 3.1623 4.6416 6.8129 10.0000

--------------------------------------------------------------------------------------------------------------------
H.W.

NON-ACTIVATED VERSION
1. For the following separation system, we know the inlet mass flow rate (in Kg/hr) and the
mass fractions of each species in the inlet (stream 1) and each outlet (streams 2, 4, and 5).
We want to calculate the unknown mass flow rates of each outlet stream.
www.avs4you.com

--------------------------------------------------------------------------------------------------------------------

1
2. Use MATLAB to show that the sum of the infinite series  converges
n  0 2 n  12 n  2 

to ln2. Do this by computing the sum for:


a) n=50
b) n= 500
c) n = 5,000

3. A train and a car are approaching a road crossing. At t = 0 the train is 400 ft. south of the
crossing traveling north at a constant speed of 54 mile/hr. At the same time the car is
200 ft. west of the crossing traveling east at a speed of 28 mile/hr and accelerating at 4
ft/s2. Determine the positions of the train and the car, the distance between them, and
Lecture 3
Computer Application

the speed of the train relative to the car every second for the next 10 seconds.
The position of an object that moves along a straight line at a constant acceleration is given by

1 2
s  so   o t  at
2

where so and vo are the position and velocity at t = 0, and a is the acceleration.

--------------------------------------------------------------------------------------------------------------------

4. Air enclosed by a rigid cylinder containing a piston. A pressure gage attached to the
cylinder indicates an initial reading of 20 psi. Develop a computer program for
calculating the final gage pressure of air when the piston has compressed the air to one-
fifth, two fifth, and three fifth its original volume. Assume the compression process to
be isothermal and local atmospheric pressure to be 14.7 psi.

--------------------------------------------------------------------------------------------------------------------

NON-ACTIVATED VERSION
5.
www.avs4you.com

You might also like