# Plotting the electric field and potential of a dipole

This is a function that takes a function, turn it into a vector, then plots the vector and its contour.

plot[ϕ_] :=
Module[{Efield = -{D[ϕ, x], D[ϕ, y]}, plot1, plot2},
plot1 = ContourPlot[ϕ, {x, -2, 2}, {y, -2, 2},
ContourShading -> False, DisplayFunction -> Identity];
plot2 = VectorPlot[Efield, {x, -2, 2}, {y, -2, 2},
VectorScale -> Small, DisplayFunction -> Identity];
Show[plot1, plot2, DisplayFunction -> $DisplayFunction]] ϕ = 1/Sqrt[x^2 + (y - 0.5)^2] - 1/Sqrt[x^2 + (y + 0.5)^2]; plot[ϕ]  This is my output: However the plot supposed to look like this: Why is my output so different and how can I fix it? I have been studying notes from an undergrad course that used Mathematica 5, so my knowledge of it is rather outdated. • try StreamPlot instead of VectorPlot However, it seems unlikely that you get the desired plot since you have maxima in the places that vectorplot correctly shows but your "supposed" plot omits. – tsuresuregusa May 11 '17 at 23:44 • @tsuresuregusa Then what might be wrong with VectorPlot for this problem? – Αλέξανδρος Ζεγγ Mar 11 '19 at 14:43 ## 1 Answer Thanks for tsuresuregusa's advice, I changed VectorPlot to StreamPlot. It produced the correct output (which is the electric field of a dipole). plot[ϕ_] := Module[{Efield = -{D[ϕ, x], D[ϕ, y]}, plot1, plot2}, plot1 = ContourPlot[ϕ, {x, -2, 2}, {y, -2, 2}, ContourShading -> False, DisplayFunction -> Identity]; plot2 = StreamPlot[Efield, {x, -2, 2}, {y, -2, 2}, VectorScale -> Small, DisplayFunction -> Identity]; Show[plot1, plot2, DisplayFunction ->$DisplayFunction]]
ϕ = 1/Sqrt[x^2 + (y - 0.5)^2] - 1/Sqrt[x^2 + (y + 0.5)^2];
plot[ϕ] • Since you mentioned that you were adapting version 5 code, here's a modern version of your code: plot[ϕ_] := Show[ContourPlot[ϕ, {x, -2, 2}, {y, -2, 2}, ContourShading -> False], StreamPlot[-D[ϕ, {{x, y}}] // Evaluate, {x, -2, 2}, {y, -2, 2}, VectorScale -> Small]] – J. M.'s technical difficulties May 11 '17 at 23:58
• Thank you J.M. The piece of code I used were for demonstrating modular programming, I guess it was actually unnecessary in this case. – Henry Wang May 12 '17 at 0:07