You are on page 1of 1

% This script allows the user to input a non-variable vector

% in rectangular coordinates and obtain the Cylindrical, or


% spherical components. The user must also enter the point
% location where this transformation ocurrs; the result
% depends on the vector's observation point clear
% prompt the user for the vectors and check to see if entered
% properly, else set to 0
disp('Enter the rectangular vector (in the ');
v = input(' format [x y z])... \n > ');
if isempty(v); v = [0 0 0]; end
disp('Enter the location of the vector (in the ');
p = input(' formtat [x y z])... \n > ');
if isempty(p); p = [0 0 0]; end
disp('Cylindrical components [rho phi(rad) z];')
phi = atan2(p(2),p(1));
% create the transformation matrix
cyl_p=[cos(phi) sin(phi) 0; ... % The ellipses allow a single
% command over multiple lines
-sin(phi) cos (phi) 0; ...
0 0 1];
disp((cyl_p*v')') % the ' denotes a transpose from a row
% vector to a column vector
% The second transpose converts the column
%vector back to a row vector
disp('Spherical components [r phi(rad) theta(rad)];')
phi = atan2(p(3),sqrt(p(1)^2+p(2)^2));
theta = atan(p(2),p(1));
% create the transformation matrix
sph_p=[sin(theta)*cos(phi) sin(theta)*sin(phi) cos(theta); ...
cos(theta)*cos(phi) cos(theta)*sin(phi) -sin(theta); ...
-sin(phi) cos(phi) 0];
disp((sph_p*v')')

You might also like