MATLAB Answers

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

28 views (last 30 days)
GreenEye on 20 Feb 2021
Edited: GreenEye on 24 Feb 2021
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) ##### 0 CommentsShowHide -1 older comments

Sign in to comment.

### Accepted Answer

GreenEye on 24 Feb 2021
Edited: GreenEye on 24 Feb 2021
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)
##### 0 CommentsShowHide -1 older comments

Sign in to comment.

### More Answers (1)

Shadaab Siddiqie on 23 Feb 2021
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 CommentShowHide None
GreenEye on 24 Feb 2021
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.

Sign in to comment.

### Community Treasure Hunt

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

Start Hunting!