How to plot vectors pointing outwards from origin in a 3D matrix?

5 次查看(过去 30 天)
Something similar to the picture below. An origin point is specified and vectors are plotted outwards.
I need it in the format of three 3D matrices that each contain the component of the vector at the position. (Hx, Hy, Hz).
quiver3(X,Y,Z,Hx,Hy,Hz), ideally would plot something like the image below. (X,Y,Z are the positions, and Hx,Hy,Hz are vector components)

采纳的回答

GreenEye
GreenEye 2021-2-24
编辑:GreenEye 2021-2-24
To anyone from the future, I answered my own question, as is often the case...
Using the spherical coordinate system
n = 32;
nc = round((n+1)/2);
vec = (1:n)-nc;
[x,y,z] = meshgrid(vec);
r = sqrt(x.^2 + y.^2 + z.^2);
psi = atan2(y,x);
theta = acos(z ./ r);
theta(isnan(theta)) = 0;
Mx = sin(theta).*cos(psi);
My = sin(theta).*sin(psi);
Mz = cos(theta);
quiver3(x,y,z,Mx,My,Mz)

更多回答(1 个)

Shadaab Siddiqie
Shadaab Siddiqie 2021-2-23
From my understanding you want to plot vectors in a cube with the axis pointing opposite to the center of the cube. Here is the code which might help you.
X = -3:3; Y = -3:3;Z= -3:3;
[X,Y,Z] = meshgrid(X,Y,Z);
quiver3(X,Y,Z,X,Y,Z)
In this case the position of the vector (line) is considered as the direction because of the problem statement.
  1 个评论
GreenEye
GreenEye 2021-2-24
Hi. This is close to what I want. Ideally there would be 3 matrices, each containing the vector component in them. For example, Hx(1,1,1) would contain the x component of the vector at the point (1,1,1). Hy(1,1,1) would contain the y component of the vector at the point (1,1,1). The vectors should have a length of 1.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Surface and Mesh Plots 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by