# How can I show Electric Potential due to a Dipole as a 3D surface?

23 views (last 30 days)
Luca on 26 Aug 2022
Answered: David Goodmanson on 27 Aug 2022
Hi,
I'm very new to matlab and have been picking my brain over how to 3D plot the electric potetial due to a dipole.
Here is the code I have currently come up with:
---------------
clear;clf
[X,Y] = meshgrid(-2: .4: 2, -2: .4: 2)
r = sqrt(X.^2+Y.^2)
Z = (1/2)*1./(r.^2)
surf(X,Y,Z)
--------------
Where Z represents the Electric Potential scalar field. NOTE: I have purposely excluded cos(theta) in the numerator for simplicity (and because I cannot seem to successfully include it). I kind of have a few questions:
• How do I avoid from dividing by 0? (This leaves a gap in the tip of the plot)
• How can I successfully include the cosine argument ( since it is necessary to provide an accurate plot)
I would really appreciate some help.
##### 2 CommentsShowHide 1 older comment
Luca on 27 Aug 2022
It needs to be in the numerator of Z

Sign in to comment.

### Answers (1)

David Goodmanson on 27 Aug 2022
Hi Luca,
Since you can't show the values of the potential at every point in the 3d space, this is most commonly done by showing a surface of constant potential. The following code first plots a sphere of radius 1 as a demo, with x,y,z defined by the usual two angles in spherical coordinates
Ignoring some constants, the potential is phi = cos(theta)/r^2. Suppose phi = +1 is the constant value. then r = sqrt(cos(theta)). In the upper hemisphere cos(theta) is positive so the sqare root works. In the lower hemisphere cos(theta) is negative, so in order to plot that, one can use r = sqrt(abs(cos(theta))) which works, at the cost of the potential coming out at a constant -1 in the lower hemisphere, which is actually physically correct. (If you only want the upper lobe then th1 below can be given an upper limit of pi/2).
th1 = linspace(0,pi,31);
phi1 = linspace(0,2*pi,41);
[th phi] = meshgrid(th1,phi1);
R = 1; % sphere
% R = sqrt(abs(cos(th))); % uncomment this line for the dipole
x = R.*sin(th).*cos(phi);
y = R.*sin(th).*sin(phi);
z = R.*cos(th);
surf(x,y,z)
axis equal
##### 0 CommentsShowHide -1 older comments

Sign in to comment.

### Categories

Find more on Graphics Performance in Help Center and File Exchange

R2022a

### Community Treasure Hunt

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

Start Hunting!