0
$\begingroup$

I have the function of an electric dipole expressed in cartesian coordinates and I want to create the vector field using Matlab .

The function is $$E_z= \frac{p}{4\pi\epsilon_0} \cdot \left(\frac{3z^2}{r^5}- \frac{1}{r^3}\right) $$ and $$E_x=\frac{p}{4\pi\epsilon_0} \frac{3xz}{r^5}\;. $$ .

The code I've come up with is :

clear;
clc;
p = 1;
e = 8.85*10^(-12);
x  =linspace(-5 , 5, 50);
z = linspace(-5 , 5 ,50);
[X, Z ] = meshgrid(x,z );
R=sqrt(X.^2+Z.^2) ; 
EX =( p .* 3 .* X .* Z )./ (4.*pi.*e ./ R.^5);
EZ = p./( 4 .* pi .* e ) .* ( 3.* Z.^2 ./R.^5 -1./ R.^3);
quiver ( X , Z , EX , EZ ) ; 

But it doesn't give me the output I want which looks like this enter image description here

Does anyone have any ideas? I would be grateful!

$\endgroup$
2
$\begingroup$

For plotting, it is easier in my opinion to not use meshgrid if you want to scale the arrows. You have a vector field $(E_X, E_Z)$ and you can simply normalize it like in the code below:

clear;
clc;
p = 1;
e = 8.85*10^(-12);
x  =linspace(-5 , 5, 20);
z = linspace(-5 , 5 ,20);

for i = 1:length(x)
    for k = 1:length(z)
        R=sqrt(x(i)^2 + z(k)^2) ; 
        EX =( p * 3 * x(i) * z(k) ) / (4 * pi * e / R^5);
        EZ = p / ( 4 * pi * e ) * ( 3 * z(k)^2 / R^5 - 1 / R^3);
        ex(i, k) = EX / sqrt(EX^2 + EZ^2);
        ez(i, k) = EZ / sqrt(EX^2 + EZ^2);
    end
end
scaleFactor = 0.5; 
quiver ( x , z , ex , ez, scaleFactor, 'LineWidth', 2); 
axis([-5 5 -5 5])

To make the arrows look nicer, you can play with Matlab's plotting manipulators.

$\endgroup$
0
$\begingroup$

Here's how I did it. Normalize everything so the arrows are same size, and remove NaN's at the origin.

p = 1;
eps0 = 8.854e-12;

x = linspace(-5,5,20);
z = x;

[xx,zz] = meshgrid(x,z);

rr = sqrt(xx.^2 + zz.^2);

ex = p/(4*pi*eps0) .* (3.*xx.*zz)./(rr.^5);
ez = p/(4*pi*eps0) .* (3.*zz.^2./rr.^5 - 1./rr.^3);

E = sqrt(ex.^2 + ez.^2);
E(isnan(E)) = max(E(:));

figure;
    quiver(x,z,ex./E,ez./E)
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.