Reputation: 69
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
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
Does anyone have any ideas? I would be grateful!
Upvotes: 2
Views: 2621
Reputation: 3476
As my previous attempt was unsuccessful, I'll give it another try. :) I believe you are very close to the solution. There's two things that I noticed.
First, I believe you have a typo in the MATLAB equation for EX
. Shouldn't it be EX =( p .* 3 .* X .* Z ./ R.^5 )./ (4.*pi.*e );
to correspond to your written equation?
Second, the figure you show as example seems to show the direction (but not magnitude) of the field, judging from the equal-length vectors. The original vectors vary quite a bit in magnitude, and thus don't really all show up in the plot. This is due to them decreasing as function of 1/r^3
or 1/r^5
- so quickly to make seeing any of the smaller ones compared to those closer to the origin impossible.
So I decided to try with the fixed equation for EX
, and normalizing the vectors [EX(jj) EZ(jj)]
to unit length before plotting them (also changed slightly plotting range):
p = 1;
e = 8.85*10^(-12);
x =linspace(-0.5 , 0.5, 50);
z = linspace(-0.5 , 0.5, 50);
[X, Z ] = meshgrid(x,z );
R=sqrt(X.^2+Z.^2) ;
EX =( p .* 3 .* X .* Z ./ R.^5 )./ (4.*pi.*e );
EZ = p./( 4 .* pi .* e ) .* ( 3.* Z.^2 ./R.^5 -1./ R.^3);
%// normalize the vectors so the arrows are visible
V = [EX(:) EZ(:)];
Vn = bsxfun(@rdivide, V, sqrt(sum(V.^2,2)));
Exn = reshape(Vn(:,1), size(EX));
Ezn = reshape(Vn(:,2), size(EZ));
quiver ( X , Z , Exn , Ezn ) ;
This is the end result, slightly zoomed in - better?
By the way, you can control the resolution of the plot by tuning the vectors you construct the grid from. Hopefully you will be able to find a suitable choice!
Upvotes: 1