Problems encountered when using sph2cart

8 views (last 30 days)
Here is the code:
load angles.mat
load vertices.mat
[vectors(:,1), vectors(:,2), vectors(:,3)] = sph2cart(angle_phi_theta(:,1), angle_phi_theta(:,2), 1);
quiver3(vertex_segment(:,1),vertex_segment(:,2),vertex_segment(:,3),...
vectors(:,1),vectors(:,2),vectors(:,3),0.8,'Color','green','LineWidth',1);hold on;
The results:
Problem: I think the resulting vectors should converge uniformly or diverge uniformly, but why do they alternate like this? What went wrong?

Accepted Answer

Star Strider
Star Strider on 15 Apr 2025
Edited: Star Strider on 15 Apr 2025
Your data do not vary much. Also, what are the uints for ‘angle_phi_theta’ ? They appear to be more like degrees than radians that all MATLAB trigonometric functions (except those ending in ‘d’ such as sind) require. Using the deg2rad function produces a smooth, continuous surface for ‘vectors’, and a different result for the quiver plot. I am not sure that is an improvement, or the result you want.
load angles.mat
load vertices.mat
whos
Name Size Bytes Class Attributes angle_phi_theta 375x2 6000 double ans 1x36 72 char vertex_segment 375x3 9000 double
format long
angle_phi_theta
angle_phi_theta = 375×2
-9.148764617364000 0.407847307973360 -9.123309592460078 0.407853188681914 -9.101024233864036 0.407858337123113 -9.092495752495266 0.407860307402597 -9.083035544599770 0.407862492932790 -9.062827352976990 0.407867161499636 -9.036664952651956 0.407873205628524 -9.031272314098036 0.407874451454674 -8.997910473048979 0.407882158823446 -8.993195935993631 0.407883247992225
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
vertex_segment
vertex_segment = 375×3
24.276068429529410 -0.448447928746336 81.067888845006465 24.259140628673865 -1.111978356700450 81.053336662519229 24.230347879342876 -1.686194075643361 81.035379587412891 24.222789097806036 -1.838657154480922 81.017396569952453 24.205319637130444 -2.010328693931287 81.065814520761450 24.154085264800283 -2.518686225537345 81.100737089879914 24.071652394713709 -3.188041341858980 81.134735008918099 24.062239752877428 -3.256346875155522 81.164042492094680 23.921659335817342 -4.142426303775671 81.159891955460267 23.910847013653022 -4.207351211131320 81.151844177732812
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
angle_phi_theta = deg2rad(angle_phi_theta)
angle_phi_theta = 375×2
-0.159676065062961 0.007118278369530 -0.159231791067210 0.007118381007257 -0.158842838184722 0.007118470864507 -0.158693988104642 0.007118505252372 -0.158528876328942 0.007118543397069 -0.158176176849251 0.007118624878932 -0.157719556823354 0.007118730368882 -0.157625437525218 0.007118752112650 -0.157043163554385 0.007118886631612 -0.156960879360062 0.007118905641193
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
[vectors(:,1), vectors(:,2), vectors(:,3)] = sph2cart(angle_phi_theta(:,1), angle_phi_theta(:,2), 1);
figure
surf(vectors, EdgeColor='interp')
grid on
colormap(turbo)
figure
quiver3(vertex_segment(:,1),vertex_segment(:,2),vertex_segment(:,3),...
vectors(:,1),vectors(:,2),vectors(:,3),0.8,'Color','green','LineWidth',1);hold on;
.
  4 Comments
bird
bird on 17 Apr 2025
Thank you very much for your answer. I have found the reason for the question, because I was busy with other things and did not reply in time. The main reason is that in the calculation of sph2cart, the Angle azimuth corresponding to the vector is sorted in counter-clockwise order, so the corresponding vertex_segment should also be sorted in counter-clockwise order. If clockwise ordering occurs, problems like mine will occur. Therefore, the vertex_segment need to be sorted in reverse order before calculation:
clear all
clc
close all
load angles.mat
load vertices.mat
vertex_segment=flipud(vertex_segment); % important
[vectors(:,1), vectors(:,2), vectors(:,3)] = sph2cart(angle_phi_theta(:,1), angle_phi_theta(:,2), 1);
quiver3(vertex_segment(:,1),vertex_segment(:,2),vertex_segment(:,3),...
vectors(:,1),vectors(:,2),vectors(:,3),0.8,'Color','green','LineWidth',1);hold on;
Star Strider
Star Strider on 17 Apr 2025
As always, my pleasure!
Thank you for clarifying that.
The counter-clockwise direction is the default mathematically, and MATLAB follows that convention. That ‘vertex_segment’ was oriented in the opposite direction is also the reason I could not get my simulations to work, since all my ‘vectors’ matrices were also counter-clockwise.
.

Sign in to comment.

More Answers (1)

Mathieu NOE
Mathieu NOE on 15 Apr 2025
hello
I could not find any quiver issue (tried to reproduce it my way) , but at the end I suspected maybe you need to invert the data order (between azimut and elevation) when you use sph2cart (we don't know how your data angle_phi_theta is generated )
clear all
clc
close all
load angles.mat
load vertices.mat
% [vectors(:,1), vectors(:,2), vectors(:,3)] = sph2cart(angle_phi_theta(:,1), angle_phi_theta(:,2), 1);
[vectors(:,1), vectors(:,2), vectors(:,3)] = sph2cart(angle_phi_theta(:,2), angle_phi_theta(:,1), 1);
% TMW quiver
figure
quiver(vertex_segment(:,1),vertex_segment(:,2),...
vectors(:,1),vectors(:,2),0.8,'Color','green','LineWidth',1);hold on;
% alternative quiver
figure
plot(vertex_segment(:,1),vertex_segment(:,2));
hold on;
scal_factor = 3;
for k = 1:numel(vectors(:,1))
dx = vertex_segment(k,1)+ scal_factor*[0 vectors(k,1)];
dy = vertex_segment(k,2)+ scal_factor*[0 vectors(k,2)];
plot(dx,dy,'r');
end
% final plot
figure
quiver3(vertex_segment(:,1),vertex_segment(:,2),vertex_segment(:,3),...
vectors(:,1),vectors(:,2),vectors(:,3),0.8,'Color','green','LineWidth',1);hold on;
quiver3(vectors(:,1),vectors(:,2),vectors(:,3),...
vertex_segment(:,1),vertex_segment(:,2),vertex_segment(:,3),0.8,'Color','green','LineWidth',1);hold on;
  1 Comment
bird
bird on 17 Apr 2025
Thank you very much for your answer, but this is not the cause of my trouble, but I have found the real problem, I have already replied in the last answer, thank you again for your enthusiasm.

Sign in to comment.

Categories

Find more on Startup and Shutdown in Help Center and File Exchange

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!