Computing Tools for Mathematics (COMP1118) Lecture 05 Vector and Scalar

Computing Tools for Mathematics     (COMP1118)

Instructor: Engineer Imran Ahmad
University Of Okara (Renala Campus)
Lecture 05
Vector and Scalar

 Vectors:
A vector is a one-dimensional array of numbers. MATLAB allows creating two types of vectors:
1. Row vectors
2. Column vectors

Row Vectors:

Row vectors are created by enclosing the set of elements in square brackets, using space or comma to delimit the elements.
r = [7 8 9 10 11]
MATLAB will execute the above statement and return the following result:
r =
  Columns 1 through 4
       7              8              9             10    
  Column 5
11  
Or
y = [12,10,-3]

Column Vectors:

Column vectors are created by enclosing the set of elements in square brackets, using semicolon to delimit the elements.
c = [7;  8;  9;  10; 11]
MATLAB will execute the above statement and return the following result:
c =
       7    
       8    
       9    
      10    
      11
Referencing the Elements of a Vector:

You can reference one or more of the elements of a vector in several ways. The ith component of a vector v is referred as v(i).
For example:
v = [ 1; 2; 9; 4; 5; 6]; % creating a column vector of 6 elements
v(3)
MATLAB will execute the above statement and return the following result:
ans =
     9
Reference a vector with a colon

When you reference a vector with a colon, such as v(:), all the components of the vector are listed.
v = [ 1; 2; 3; 4; 5; 6]; % creating a column vector of 6 elements
v(:)
MATLAB will execute the above statement and return the following result:
ans =
     1
     2
     3
     4
     5
     6

Select a range of elements

MATLAB allows you to select a range of elements from a vector.
For example, let us create a row vector rv of 9 elements, then we will reference the elements 3 to 7 by writing rv(3:7) and create a new vector named sub_rv.
rv = [1 2 3 4 5 6 7 8 9];
sub_rv = rv(5:8)
MATLAB will execute the above statement and return the following result:
sub_rv =  3     4     5     6     7
Vector Operations

In this section, let us discuss the following vector operations:
Addition and Subtraction of Vectors
Scalar Multiplication of Vectors
Transpose of a Vector
Appending Vectors
Magnitude of a Vector
Vector Dot Product
Vectors with Uniformly Spaced Elements

Addition and Subtraction of Vectors

o You can add or subtract two vectors. Both the operand vectors must be of same type and have same number of elements.
Example
o Create a script file with the following code:
o A = [7, 11, 15, 23, 9];
o B = [2, 5, 13, 16, 20];
o C = A + B;
o D = A - B;
o disp(C);
o disp(D);
o When you run the file, it displays the following result:
o 9    16    28    39    29
o 5     6     2     7   -11

Scalar Multiplication of Vectors

o When you multiply a vector by a number, this is called the scalar multiplication. Scalar multiplication produces a new vector of same type with each element of the original vector multiplied by the number.
Example
o Create a script file with the following code:
o v = [ 12 34 10 8];
o m = 5 * v
o When you run the file, it displays the following result:
o m =
o    60   170    50    40
o Please note that you can perform all scalar operations on vectors. For example, you can add, subtract and divide a vector with a scalar quantity.

Transpose of a Vector

o The transpose operation changes a column vector into a row vector and vice versa. The transpose operation is represented by a single quote(').
Example
o Create a script file with the following code:
o r = [ 1 2 3 4 ];
o tr = r';
o disp(tr);
o When you run the file, it displays the following result:
o     1
o     2
o     3
o     4
o
o     1     2     3     4

Appending Vectors

MATLAB allows you to append vectors together to create new vectors.
If you have two row vectors r1 and r2 with n and m number of elements, to create a row vector r of n plus m elements, by appending these vectors, you write:
r = [r1,r2]
You can also create a matrix r by appending these two vectors, the vector r2, will be the second row of the matrix:
r = [r1;r2]
However, to do this, both the vectors should have same number of elements.
Similarly, you can append two column vectors c1 and c2 with n and m number of elements. To create a column vector c of n plus m elements, by appending these vectors, you write:
c = [c1; c2]
You can also create a matrix c by appending these two vectors; the vector c2 will be the second column of the matrix:
c = [c1, c2]
However, to do this, both the vectors should have same number of elements.
Example:
Create a script file with the following code:
r1 = [ 1 2 3 4 ];
r2 = [5 6 7 8 ];
r = [r1,r2]
rMat = [r1;r2]

c1 = [ 1; 2; 3; 4 ];
c2 = [5; 6; 7; 8 ];
c = [c1; c2]
cMat = [c1,c2]
When you run the file, it displays the following result:
r =
     1     2     3     4     5     6     7     8
rMat =
     1     2     3     4
     5     6     7     8
c =
     1
     2
     3
     4
     5
     6
     7
     8
cMat =
     1     5
     2     6
     3     7
     4     8
Magnitude of a Vector

Magnitude of a vector v with elements v1, v2, v3, …, vn, is given by the equation:
|v| = √(v12 + v22 + v32 + … + vn2)
You need to take the following steps to calculate the magnitude of a vector:
1. Take the product of the vector with itself, using array multiplication (.*). This produces a vector sv, whose elements are squares of the elements of vector v.
sv = v.*v;
2. Use the sum function to get the sum of squares of elements of vector v. This is also called the dot product of vector v.
dp= sum(sv);
3. Use the sqrt function to get the square root of the sum which is also the magnitude of the vector v.
mag = sqrt(s);
Example
Create a script file with the following code:
v = [1: 2: 20];
sv = v.* v;     %the vector with elements
                      % as square of v's elements
dp = sum(sv);    % sum of squares -- the dot product
mag = sqrt(dp);  % magnitude
disp('Magnitude:'); disp(mag);
When you run the file, it displays the following result:
Magnitude:
   36.4692

Dot product of two vectors

 a = (a1, a2, …, an) and b = (b1, b2, …, bn) is given by:
a.b = ∑(ai.bi)
Dot product of two vectors a and b is calculated using the dot function.
dot(a, b);

Example
Create a script file with the following code:
v1 = [2 3 4];
v2 = [1 2 3];
dp = dot(v1, v2)
disp('Dot Product:'); disp(dp);
When you run the file, it displays the following result:
Dot Product:
    20
Vectors with Uniformly Spaced Elements

MATLAB allows you to create a vector with uniformly spaced elements.
To create a vector v with the first element f, last element l, and the difference between elements is any real number n, we write:
v = [f : n : l]

Example

Create a script file with the following code:
v = [1: 2: 20];
sqv = v.^2;
disp(v);disp(sqv);

When you run the file, it displays the following result:
1     3     5     7     9    11    13    15    17    19
1     9    25    49    81   121   169   225   289   361

Multiplication of Vectors
                      a = [1 2 3]
b = [4;5;6]
c = a.*a
c = a.*b'

The same result is obtained by applying the power operator ^ to the vector a
a.^2
ans =
1 4 9

Division of Vectors
a.\b'
ans =
1 1 1

Dot product of Vectors

c=a*a'
c =
    14

Cross product of Vectors

The cross product of a and b is
b = [-2 1 2];
cp = cross(a,b)
cp =
1 -8 5



Comments

Popular posts from this blog

Zong Whatsapp Packages 2019

Statical Theory -Chapter 4 Measure Of Dispersion, Moments and Skewness