Vector Fields
MAT329
Andy Long

The first vector field is a conservative field -- since it can be written as the gradient of 
x^2-y^2.
The second vector field is not. Try to write it as a gradient of a scalar function f(x,y).

Clear[f1,g1,h,cP,sP,gP1,gP2,gP3];

cons=VectorDensityPlot[{2x,-2y},{x,-3,3},{y,-3,3}]
VectorDensityPlot[{y,-x},{x,-3,3},{y,-3,3}]

I’m trying to plot the potential as a contour, and the gradient vectors on top:

poten=ContourPlot[(x^2-y^2)/2,{x,-3,3},{y,-3,3}]
Show[{poten,cons}]

Not working!:(

z[x_,y_]:=x^2-y^2
cons=VectorDensityPlot[{2x,-2y},{x,-3,3},{y,-3,3},MaxRecursion->4,ColorFunctionScaling->False,ColorFunction->Function[{x,y},Hue[z[x,y],{-3,3},{-3,3}]]]

This is the 3D vector field of a conservative field -- since it can be written as the gradient of (x^2+y^2+z^2)/2:

VectorPlot3D[{x,y,z},{x,-3,3},{y,-3,3},{z,-3,3},VectorColorFunction->"DeepSeaColors"]

http://mathematica.stackexchange.com/questions/34867/plotting-is-anyone-familiar-with-gradientfieldplot-command

(* To draw the charge distributions,we can use Graphics. *)
gP1=Graphics[{PointSize[Large],Point[{14,10}]}];
gP2=Graphics[{PointSize[Large],Point[{7,10}],Point[{21,10}]}];
gP3=Graphics[{Thickness[Large],Line[{{8,7},{20,7}}],Line[{{8,13},{20,13}}]}];

For the plots, we first need to look up the expressions for the potentials of a point charge and a charged line segment.

h[x_,y_]=1/Sqrt[x^2+y^2];
h[x_,y_,L_]=Log[(Sqrt[(x-L/2)^2+y^2]-(x-L/2))/(Sqrt[(x+L/2)^2+y^2]-(x+L/2))];

(* Then we need to move these expressions around to represent our charge distributions.*)

f1[x_,y_]=h[x-14,y-10];

(* We can use ContourPlot on the f functions to get the equipotential lines.Note that ContourPlot uses ContourStyle instead of PlotStyle to change the color of the lines.*)

cP=ContourPlot[f1[x,y],{x,0,28},{y,0,20},Contours->4,ContourShading->None,ContourStyle->Blue,FrameTicks->{Range[0,28,2],Range[0,20,2]}];

(* The field lines are perpendicular to the equipotential lines,so they are related to the gradient of the potential functions. *)

g1[x_,y_]=D[f1[x,y],{{x,y}}];

(* We can plot the field lines using StreamPlot. Here is where getting to know the options pays off. First,we don't want arrowheads on our lines,so we put StreamStyle->"Line".In your picture,you only have 8 streamlines,so we try StreamPoints->8 which may give unsatisfactory results depending on the plot (it worked great for PlotRange->{{0,20},{0,20}}).So,we take more control and indicate exactly which points we want to start our streams.Note that StreamPlot uses StreamColorFunction instead of PlotStyle to change the color of the lines. *)

sP=StreamPlot[g1[x,y],{x,0,28},{y,0,20},StreamStyle->"Line",StreamPoints->({14,10}+{Cos[#],Sin[#]}&/@Range[0,7Pi/4,Pi/4]),StreamColorFunction->Function[{x,y},Darker@Yellow]];

Finally,we combine the plots. I start with the contour plot because it has the plot range and ticks defined.

Show[cP,sP,gP1,AspectRatio->Automatic]

f1[x_,y_]=h[x-7,y-10]-h[x-21,y-10];
g1[x_,y_]=D[f1[x,y],{{x,y}}];
cP=ContourPlot[f1[x,y],{x,0,28},{y,0,20},Contours->14,ContourShading->None,ContourStyle->Blue,FrameTicks->{Range[0,30,2],Range[0,20,2]}];
sP=StreamPlot[g1[x,y],{x,0,28},{y,0,20},StreamStyle->"Line",StreamPoints->80,StreamColorFunction->Function[{x,y},Darker@Yellow]];
Show[cP,sP,gP2,AspectRatio->Automatic]

http://mathematica.stackexchange.com/questions/18758/how-do-i-draw-lines-perpendicular-to-contour-lines-on-listplot3d

data=Table[10+(3 ((20-j)^2-(j+5)^2))/(75+(i-10)^2+2 (j-10)^2),{j,0,20},{i,0,20}];
f=ListInterpolation[data,{{0,20},{0,20}}];

pot=Plot3D[f[x,y],{x,0,20},{y,0,20},MeshFunctions->{#3&},Mesh->14,FaceGrids->All];
vf=StreamPlot[Evaluate[-D[f[x,y],{{x,y}}]],{x,0,20},{y,0,20},StreamStyle->"Line"];
Show[pot,Graphics3D[vf[[1]]/.{{x_Real,y_Real}:>{x,y,f[x,y]}}]]

vf=StreamPlot[Evaluate[-D[f[x,y],{{x,y}}]],{x,0,20},{y,0,20},StreamStyle->ColorData[1][3]];
cp=Show[ContourPlot[f[x,y],{x,0,20},{y,0,20},Contours->14],vf];
Row[{Show[pot,ImageSize->300],Show[cp,ImageSize->250]}]