Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 8

MATHEMATICAL OPERATIONS

WITH ARRAYS
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.
When a scalar (number) is added to or subtracted from, an array, the
number is added to or subtracted from, all the elements of the array.
example:
1. >>vectA=[8 5 4];
>>vectB=[10 2 7];
>>vectC=vectA+vectB (enter key)
2. >>A=[5 -3 8;9 2 10] (enter key)
>>B=[10 7 4;-11 15 1] (enter key)
>>C=A+B (enter key)
Array Multiplication
The multiplication operation * is executed by MATLAB according to the
rules of linear algebra. If A*B are two matrices, the operation A*B can
be carried out only if the number of columns in matrix A 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.
example:
1. >>A=[1 4 3;2 6 1;5 2 8] (enter key)
>>B=[5 4;1 3;2 6] (enter key)
>>C=A*B
The product of multiplication of two square matrices is also a square
matrix of the same size. The multiplication of matrices is not
commutative.
Two vectors can multiply each other only if both have the same number of
elements, one is a row vector and the other is a column vector. This is
the dot product of two vectors. MATLAB built in function dot(a,b).
Array Division
The division operation is also associated with the rules of linear algebra.
Left Division \:
The left division is used to solve the matrix equation AX=B. X & B column vectors.
Right Division/:
The right division is used to solve the matrix equation XC=D. X & D are row vectors.
example:
Use the matrix operations to solve the following system of linear equations:
4x-2y+6z=8
2x+8y+2z=4
6x+10y+3z=0
1. >>A=[4 -2 6;2 8 2;6 10 3]
>>B=[8;4;0]
>>X=A\B
2. >>C=[4 2 6;-2 8 10;6 2 3]
>>D=[8 4 0]
>>Xc=D/C
Element-by-element Operations
element by element multiplication, division and exponentiation of two
vectors or matrices is entered in MATLAB by typing a period in
front of the arithmetic operator.
example:
1. >>A=[2 6 3;5 8 4]
>>B=[1 4 10;3 2 7]
>>A.*B
>>C=A./B
element by element calculations are very useful for calculating the value
of a function at many values of its argument. This is done by first
defining a vector that contains values of the independent variable.
example:
2. >>x=[1:8] (create a vector x with 8 elements)
>>y=x.^2-4*x (element by element operation is needed when x is
squared.
Example:

The electrical circuit


shown to the right
consists of resistors
and voltage sources.
Determine the current
in each resistor using
the mesh current
method which is based
on Kirchhoff’s voltage
law.
Solution:
>>V1=20;v2=12;v3=40;
>>r1=18;r2=10;r3=16;r4=6;
>>r5=15;r6=8;r7=12;r8=14; I=
0.8411
>>A=[-(r1+r2+r3) r2 r3 0
0.7206
r2 –(r2+r4+r5+r7) r4 r7 0.6127
r3 r4 –(r3+r4+r6) r6 1.5750
0 r7 r6 –(r6+r7+r8)]
>>B=[-V1;0;V2;-V3]
>>I=A\B
Activity1

20 Ohms

You might also like