-
Notifications
You must be signed in to change notification settings - Fork 1
/
dsph2dcart.m
51 lines (45 loc) · 1.46 KB
/
dsph2dcart.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
function [dx,dy,dz]=dsph2dcart(lon,cola,dlon,dcola,dr)
% [dx,dy,dz]=dsph2dcart(lon,cola,dlon,dcola,dr)
%
% Calculates the vectorial components in spherical coordinates into
% vectorial components in cartesian coordinates. Orthogonality is achieved
% by using position on the unit sphere. The transformation matrix is
% automatically orthogonal.
%
% INPUT:
%
% lon longitudinal position of the vector (0<=lon<2*pi)
% cola latitudinal position of the vector (0<=cola<=pi)
% dlon longitudinal component of the vector
% dcola colatitudinal component of the vector
% dr radial component of the vector
%
% OUTPUT:
%
% dx x-component of the vector
% dy y-component of the vector
% dz z-component of the vector
%
% Last modified by plattner-at-alumni.ethz.ch, 1/29/2014
lonsize=size(lon);
lon=lon(:);
cola=cola(:);
dlon=dlon(:);
dcola=dcola(:);
dr=dr(:);
dx=nan(size(lon));
dy=nan(size(lon));
dz=nan(size(lon));
for i=1:length(dlon)
% See e.g. http://en.wikipedia.org/wiki/Vector_fields_in_cylindrical_and_spherical_coordinates
D=[sin(cola(i))*cos(lon(i)) sin(cola(i))*sin(lon(i)) cos(cola(i));
cos(cola(i))*cos(lon(i)) cos(cola(i))*sin(lon(i)) -sin(cola(i));
-sin(lon(i)) cos(lon(i)) 0 ];
dcart=D'*[dr(i);dcola(i);dlon(i)];
dx(i)=dcart(1);
dy(i)=dcart(2);
dz(i)=dcart(3);
end
dx=reshape(dx,lonsize(1),lonsize(2));
dy=reshape(dy,lonsize(1),lonsize(2));
dz=reshape(dz,lonsize(1),lonsize(2));