How to plot a 3D vector field

37 views (last 30 days)
Ege Keskin
Ege Keskin on 31 Jan 2019
Commented: prachi jain on 20 Jun 2019
Hello. I am trying to plot the magnetic flux density of a permanent magnet around its viscinity. the magnetic flux density at any point in space (x,y,z) has 3 components, Bx,By and Bz, where all of them are functions of x,y and z. My duty is to plot these vector fields. I have the equations written down but I couldn't gain any progress on plotting. In the end, the vector field should look like the iconic magnetic field lines of a magnet. Here is the code and the equations. Thanks for your help.
syms x y z
a = 1; %dimensions of the permanent magnet
b = 1;
c = 0.5;
Nu0 = 1.25e-6; %(m.kg)/(s^2.A^2) permeability of free space
M = 1; %tesla
F_1xyz = atan(((x+a)*(y+b)) / ((z+c)*sqrt((x+a)^2+(y+b)^2+(z+c)^2))); %F1(x,y,z)
F_1xxyz = atan(((-x+a)*(y+b)) / ((z+c)*sqrt((-x+a)^2+(y+b)^2+(z+c)^2))); %F1(-x,y,z)
F_1xxyzz = atan(((-x+a)*(y+b)) / ((-z+c)*sqrt((-x+a)^2+(y+b)^2+(-z+c)^2))); %F1(-x,y,-z)
F_1xxyyz = atan(((-x+a)*(-y+b)) / ((z+c)*sqrt((-x+a)^2+(-y+b)^2+(z+c)^2))); %F1(-x,-y,z)
F_1xxyyzz = atan(((-x+a)*(-y+b)) / ((-z+c)*sqrt((-x+a)^2+(-y+b)^2+(-z+c)^2))); %F1(-x,-y,-z)
F_1xyzz = atan(((x+a)*(y+b)) / ((-z+c)*sqrt((x+a)^2+(y+b)^2+(-z+c)^2))); %F1(x,y,-z)
F_1xyyz = atan(((x+a)*(-y+b)) / ((z+c)*sqrt((x+a)^2+(-y+b)^2+(z+c)^2))); %F1(x,-y,z)
F_1xyyzz = atan(((x+a)*(-y+b)) / ((-z+c)*sqrt((x+a)^2+(-y+b)^2+(-z+c)^2))); %F1(x,-y,-z)
F_2xyz = (sqrt((x+a)^2+(y-b)^2+(z+c)^2)+b-y) / (sqrt((x+a)^2+(y+b)^2+(z+c)^2)-b-y); %F2(x,y,z)
F_2xxyzz = (sqrt((-x+a)^2+(y-b)^2+(-z+c)^2)+b-y) / (sqrt((-x+a)^2+(y+b)^2+(-z+c)^2)-b-y);%F2(-x,y,-z)
F_2xyzz = (sqrt((x+a)^2+(y-b)^2+(-z+c)^2)+b-y) / (sqrt((x+a)^2+(y+b)^2+(-z+c)^2)-b-y); %F2(x,y,-z)
F_2xxyz = (sqrt((-x+a)^2+(y-b)^2+(z+c)^2)+b-y) / (sqrt((-x+a)^2+(y+b)^2+(z+c)^2)-b-y); %F2(-x,y,z)
F_2yyxzz = (sqrt((-y+a)^2+(x-b)^2+(-z+c)^2)+b-x) / (sqrt((-y+a)^2+(x+b)^2+(-z+c)^2)-b-x); %F2(-y,x,-z)
F_2yxz = (sqrt((y+a)^2+(x-b)^2+(z+c)^2)+b-x) / (sqrt((y+a)^2+(x+b)^2+(z+c)^2)-b-x); %F2(y,x,z)
F_2yxzz = (sqrt((y+a)^2+(x-b)^2+(-z+c)^2)+b-x) / (sqrt((y+a)^2+(x+b)^2+(-z+c)^2)-b-x); %F2(y,x,-z)
F_2yyxz = (sqrt((-y+a)^2+(x-b)^2+(z+c)^2)+b-x) / (sqrt((-y+a)^2+(x+b)^2+(z+c)^2)-b-x); %F2(-y,x,z)
Bx = ((Nu0*M)/(4*pi))*log((F_2xxyzz*F_2xyz)/(F_2xyzz*F_2xxyz)); %x component of the magnetic flux density vector at point (x,y,z)
By = ((Nu0*M)/(4*pi))*log((F_2yyxzz*F_2yxz)/(F_2yxzz*F_2yyxz)); %y component of the magnetic flux density vector at point (x,y,z)
Bz = -((Nu0*M)/(4*pi))*(F_1xxyz + F_1xxyzz + F_1xxyyz + F_1xxyyzz + F_1xyz + F_1xyzz + F_1xyyz + F_1xyyzz ); %z component of the magnetic flux density vector at point (x,y,z)

Accepted Answer

Ege Keskin
Ege Keskin on 3 Feb 2019
UPDATE: I have found a user made function that takes a 3d matrix and visualizes it. Which solved my problem instantly. The function is vis3d if anyone wants to take glimpse.
  3 Comments
Ege Keskin
Ege Keskin on 15 Mar 2019
Hello Kenneth,
Unfortunately, I cannot share my code with you since it is included in my Masters thesis. However, I can share with you the end results. This is the plot of the magnetic flux density lines of a rectangular neodymium magnet.
-Best regards..
prachi jain
prachi jain on 20 Jun 2019
Hello Kenneth,
Did you find the answer to your question?
I am doing a similar thing for my research. Please share if you were successful.
-Prachi

Sign in to comment.

More Answers (2)

KSSV
KSSV on 31 Jan 2019
YOu need to define the values of (x,y,z) sibstiutue these values in Bx, By Bz using subs and then use quiver3.
  3 Comments
KSSV
KSSV on 31 Jan 2019
read about _ndgrid_
KSSV
KSSV on 31 Jan 2019
my suggestion..you need not to use syms.

Sign in to comment.


Ege Keskin
Ege Keskin on 1 Feb 2019
I still do not have my answer. All of the examples and sketches on the internet have 2 variables. for example z = x*y. However in my case, I am dealing with functions such as Bx = x*y*z So the functions like quiver3 does not work with for me. I need something to evaluate any point in space according to three formulas which are all dependant on 3 variables x,y,z.

Categories

Find more on Vector Fields in Help Center and File Exchange

Products


Release

R2018b

Community Treasure Hunt

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

Start Hunting!