Non-Activated Version: Array Operations
Non-Activated Version: Array Operations
Non-Activated Version: Array Operations
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
>> c = a .^ 2
c=
1 4 9 16 25 36
>> d=a+2
d=
3 4 5 6 7 8
Example:
Element by element operation when using array–array:
a=
1 2 3
b=
4 5
NON-ACTIVATED VERSION
6
c=
ans =
4 10 18
ans =
>> a.\b
Lecture 3
Computer Application
ans =
>> a.*c
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=
du =
Lecture 3
Computer Application
>>viscosity=T./du
viscosity =
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.
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
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
2. Matrix Operation
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
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
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
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.
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
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
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
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 12 n 2
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