You are on page 1of 7

EXPERIMENT No.

05
OBJECTIVE
i) Become familiar with the vector representation in MATLAB
ii) Become familiar with the matrix representation in MATLAB
iii) Become familiar with the basic operations on vectors and matrices
iv) Analyze the conversion of different coordinate systems.

OVERVIEW:
Since MATLAB is matrix laboratory, it is efficient in performing calculations related
to MATLAB. In fact, we can translate almost all of our problems in MATLAB into
matrix calculations and thus avoiding the conventional FOR or WHILE loops, which
are not recommended in MATLAB.

EXTRACTING BITS OF A VECTOR:


Run the following command.

>>r = [1 : 2 : 6 , -1 : -2 : -7]
>>r = (3 : 6)
>>r (1 : 2 : 7)
>>r (6 : -2 : 1)

Carefully analyze output of above commands and note down your observations.

OUTPUT & COMMENTS:

TRANSPOSING:
We can convert a row vector into a column vector ( and vice versa ) by a process
called transposing, denoted by ' .

Run the following commands and record output and observations.

>>c=[1 ; 3 ; sqrt(5) ]
>>w=[1 ; -2 ; 3]
>>t=w+2*c'
>>T=5*w'-2*c
OUTPUT & COMMENTS:

Take transpose of
>>x = [1+3i , 2-2i]
i.e.
>>x'
What is output? Why?
OUTPUT & COMMENTS:

What is the difference from the previous output? Why?


OUTPUT & COMMENTS:

SCALAR PRODUCT:
The scalar produce is defined by multiplying the corresponding elements together and
adding the results to give a single number. Suppose we have the following vectors:

>> u= [10 -11 12]


>> v= [20 ; -21 ; -22]
>> w= [2 1 3]
The command for scalar produce is "*". However the vector (or matrix) dimensions
must agree, i.e. 1xn vector can be multiplied with nx1 vector.

Try the following commands and find out output. Write down your comments and
observations.
>> u * v
>> u * w
>> u * w'
>> u * u'

Try ":*" instead of "*".

What is the difference between these commands? Dot (A,B) is another command used
to calculate dot produce. Try dot command on some of the above vectors.

OUTPUT & COMMENTS:

DOT DIVISION OF ARRAYS/VECTORS:


There is no mathematical definition for the division of one vector by another.
However, in MATLAB, the operator ./ is defined to give element by element division.
It is therefore defined for vectors of same size and type.

>>a = 1:5, b= 6:10


>>a./b
>>a./a
>>c = -2:2
>>a./c
>>c./c
OUTPUT & COMMENTS:

You will have encountered INF and NAN in the output of above commands. Explain
them:
OUTPUT & COMMENTS:

DOT POWER OF ARRAYS/VECTORS:


To square each element of a vector we could, for example, do u.*u. however, a neater
way is to use the .^ operator.

Try the following commands:

>> u.^2
>> u.^4
>> u.*w.^(-2)
OUTPUT & COMMENTS:

VECTOR PRODUCT:
Cross (A,B) is the command used to find cross product or vector product of two
vectors. Find cross product (A x B), where A = 3ux + 4uy +5uz and B = 5ux + 4uy +
3uz. Also find B x A.
OUTPUT & COMMENTS:

SIZE OF A MATRIX:

We can find out the size of a matrix by command size.


>> size (A)
>> size (D)
>> size(A')
>> size(D')

OUTPUT & COMMENTS:

SPECIAL MATRICES:
MATLAB provides a number of useful built-in matrices of any desired size.
Try the following matrices and write down function of each of them.
>>P=ones(2,3)
>>Z=zeros(2,3)
>>X=eye(5)

OUTPUT & COMMENTS:


EXTRACTING BITS OF MATRICES:
Following commands are used to extract bits of a matrix. Try each of following
command and write down your observations

>>J = [1:4 ; 5:8 ; 9:12 ; 20 0 5 4]


>>J(2,3)
>>J(: ,3)
>>J(: ,3:3)
>>J(4, : )
>>J(2:3 , 2:3)

OUTPUT & COMMENTS:

MATRIX PRODUCTS:
The products defined for vectors also work for matrices.
Try the following commands

>>A= [5 7 9 ; 1 -3 -7]
>>B= [-1 2 5 ; 9 0 5]
>>x= [8 ; -4 ; 1]
>>A.*B
>>A*x
>>x*A

Also Try.

>>B= [0 1 ; 3 -2 ; 4 2]
>>C=A*B
>>D=B*A
>>E=B'*A'

OUTPUT & COMMENTS:


CONVERSION OF COORDINATE SYSTEM:

Cylindrical- Spherical Cylindrical-Rectangular Spherical-Rectangular

z = ρcosΦ x = rcosθ x = ρsinΦcosθ


r = ρsinΦ y = rsinθ y = ρsinΦsinθ
z=z z=z z = ρcosΦ

z2 + r2 = ρ2 x2 + y2 = r2 x2 + y2 + z2 = ρ2
tanΦ = r/z tanθ = y/x tanθ = y/x

Let us convert the Cartesian coordinate system to cylindrical coordinate system.

>>clear
>> p = 1;
>> for i=0:pi/10:2*pi
X(p)=1*cos(i)
p=p+1;
end
>> x=[x;x]
>> p=1;
>> for i=0:pi/10:2*pi
y(p)=1*sin(i)
p=p+1;
end
>> y=[y;y];
>>z=[zeros(1,21);ones(1,21)]

>>surf(x,y,z)

OUTPUT & COMMENTS:

HOMEWORK ASSIGNMENT:
Write down a program which (verify results using MATLAB built-in functions):

 Transform Cartesian coordinates to spherical


 Transform Polar of Cylindrical coordinates to Cartesian
 Transform Spherical coordinates to Polar or Cylindrical
Transform Cylindrical coordinate

You might also like