How to plot Vector in 3d using two angles and initial point

40 views (last 30 days)
Hi! Could you please help me
How to put 3d vector if i know initial point coordinates and two angles
I tries this one, but still could not understand where is my phi and theta on 3d according to matlab plotting
x0=1.5; %initial x position
y0=1.5; %initial y position
z0=3.0;
r = sqrt(x0^2 + y0^2 + z0^2);
x1 = r * sin(Phi0) * cos(Theta0);
y1 = r * sin(Phi0) * sin(Theta0);
z1 = r * cos(Phi0);
line([x0 x1],[y0,y1],[z0,z1]);
xlabel X; ylabel Y; zlabel Z
grid on;
axis equal
%I also have tried this one
%x1 =r * sind(theta) * cosd(phi); % !!!convert to cart mine last one
%y1 = r* sind(theta) * sind(phi);
%z1 = r* cosd(theta);
Here his how my angles should be

Accepted Answer

Askic V
Askic V on 13 Dec 2022
Edited: Askic V on 13 Dec 2022
I must admit something is confusing me in your post.
If you have a point in 3D space and the second point is in origin, then you have all you need to represent a vector (two points are enough to determine its length and direction). So you can plot it just by using these two points (quiver3)
On the other hand, if you have az and el angles measured exactly the same way you posted on the picture then, you can calculate a vector axis components in the following way (assuming vector starts in origin and ends in points marked with star, with length of r).
The vector has there parts vx*i + vy*j + vz*k, where i, j and k are unit axis vectors.From your picture it is clear that projection to the xy plane is: r*sin(theta), because, prjection to the z axis is r*cos(theta).
In the xy plane, the x (i) component is r*sin(theta)*sin(phi), and y component is r*sin(theta)*cos(phi).
To me, much sense has to find a components of a vector given vector length and these two angles, for example:
[vx,vy,vz] = find_vector(2, pi/6, pi/30)
vx = 0.1045
vy = 0.1810
vz = 1.9890
function [vx,vy,vz] = find_vector(vec_length, azim, el)
phi = azim;
theta = el;
r = vec_length;
vx = r * sin(phi) * sin(theta);
vy = r * cos(phi) * sin(theta);
vz = r * cos(theta);
quiver3(0,0,0,vx,vy,vz,'off');
view(45,20)
end
  6 Comments
Aknur
Aknur on 22 Dec 2022
Edited: Aknur on 22 Dec 2022
Dear, @Askic V thank you for your valuable feedback. I understand your idea and the code almost. As I have point which is start point of vector and two angles. I tried to do it this way. To find unit vector which will be calculated accoridng these formulas
x1 = sind(theta) * sind(phi);
y1 = sind(theta) * cosd(phi);
z1 = cosd(theta);
But when I rotate ny cube to check if the angles show correct angles it does not work properly. For example when I put theta = 0, and phi = 30. The figure should show me that theta angle which is along z axis should be equal 0, but it shows like theta =30 and phi =45 according to the figure of matlab. Could you please to look on my code
I really became confused with angles and how to plot theta and phi. And dont know from where matlab default starts 0 degree of theta and phi
%figure
%pax = polaraxes;
Room = [
3 3 0;
0 3 0;
0 3 3;
3 3 3;
0 0 3;
3 0 3;
3 0 0;
0 0 0];
% edges by looking for all combinations of points on cube that
% differ by only one coordinate
sections_per_edge = 1;
weights = ((1:sections_per_edge-1) / sections_per_edge).';
edges = []; % indices into Room
points = [];
n = size(Room, 1);
for i = 1:n-1
pointA = Room(i, :);
for j = i+1:n
pointB = Room(j, :);
if nnz(pointA - pointB) == 1
edges = [edges; i, j];
points = [points; weights * pointA + (1 - weights) * pointB];
end
end
end
plot3(Room(:,1), Room(:,2), Room(:,3), '.b')
hold on
plot3(points(:,1), points(:,1), points(:,1), '.r', 'markersize', 10)
hold on
line([Room(edges(:,1), 1), Room(edges(:,2), 1)].', ...
[Room(edges(:,1), 2), Room(edges(:,2), 2)].', ...
[Room(edges(:,1), 3), Room(edges(:,2), 3)].', 'color', 'k')
plot3(1.5, 1.5, 3.0, '*', 'markersize', 15)
plot3(1.5, 0, 0, 'o', 'markersize', 7, 'MarkerFaceColor', 'black')
plot3(0, 1.5, 0, 'o', 'markersize', 7, 'MarkerFaceColor', 'black')
plot3(3, 1.5, 0,'o', 'markersize', 7, 'MarkerFaceColor', 'black')
plot3(1.5, 3, 0, 'o', 'markersize', 7, 'MarkerFaceColor', 'black')
theta = 0;
phi = 30;
%elev = (90 - Theta);
%az = 60;
x0=1.5; %initial x position
y0=1.5; %initial y position
z0=3.0;
x1 = sind(theta) * sind(phi);
y1 = sind(theta) * cosd(phi);
z1 = cosd(theta);
plot3 ([x0 x1],[y0 y1],[z0 z1]);
xlabel X; ylabel Y; zlabel Z
grid on;
axis equal
%axis=gca;
%ax.PhiLim = [0 360]
%Ax.PhiDir = 'clockwise';
%Ax.PhiZeroLocation = 'top';
%ax.ThetaLim = [0 180]
%Ax.ThetaDir = 'clockwise';
%Ax.ThetaZeroLocation = 'top';
This picture in general what I need, but confused with angles. They (the unit vector) do not show correct direction according to the figure. I hope I were able to explain
Thank you for time and effort

Sign in to comment.

More Answers (1)

Walter Roberson
Walter Roberson on 13 Dec 2022
You have initial coordinates, and you calculate radius based on those coordinates. That is equivalent to saying that the initial point is on the surface of the sphere around the origin.
Then you calculate coordinates based on that radius and two angles. That is like asking "what point at the same radius as the initial point, is at this particular angle on that implied sphere?"
That is a valid thing to calculate, but is that what you want to calculate?
I suspect that what you are looking for is to find a vector at a particular spatial angle, that goes through the given point, and to sketch a segment of that vector,
  1 Comment
Aknur
Aknur on 13 Dec 2022
@Walter Roberson thank you for your feedback and explanation. Yes you are right. I want to find a vector which starts from initial position at a particular spatial angle, and sketch segment of that vector.
As I am right these two angles Phi and Theta gave me direction in 3d. And also I need to define x1, y1, z1 - not absolutely end point of vector (but one which will be in side of coordinates( 3,3,3) if possible

Sign in to comment.

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!