3 Answers
3

This is something I have used for my classes. Over time, I've tried to make it more and more user friendly, but that's also made it a little longish. I'll post the complete set of functions, with apologies if it's a bit unwieldy...

As you'll see, I found it does indeed work better in my use cases if I normalize the field, so that we advance along the field lines in more balanced steps. The hardest part in applying these functions is to choose the appropriate seed points.

The main function is fieldLinePlot, but I split it into two functions to be more modular. Also, the problem of where to start drawing the field lines is treated separately because it depends a lot on the particular application.

fieldSolve[f,x,x0,Subscript[t, max]] symbolically takes a vector field f with respect to the vector variable x, and then finds a vector curve r[t] starting at the point x0 satisfying the equation dr/dt = α f[r[t]] for t=0...tmax. Here α = 1/|f[r[t]]| for normalization. To get verbose output add debug=True to the parameter list.

fieldLinePlot[field,varlist,seedList] plots 3D field lines of a vector field (first argument) that depends on the symbolic variables in varlist. The starting points for these variables are provided in seedList.

Each element of seedList={{p1, T1},{p2, T2}...} is a tuple where pi is the starting point of the $i^\mathrm{th}$ field line and Ti is the length of that field line in both directions from Pi.

The field lines can be given a color that scales with the field strength (the norm of the vector field along the lines), by specifying a ColorFunction in fieldLinePlot. For example, if the vector field has been defined as a function f2 of variables x,y,z, then you could add the option ColorFunctionScaling -> False, ColorFunction -> Function[{x,y,z,u}, Quiet@Hue[Clip[ Norm[f2[x,y,z]],{0,20}]/20]] as I mention in the comment section.

In this new edit, I added the ability to encode the field strength in the thickness of the field lines instead. This required adding a new option "TubeFunction" which works similarly to ColorFunction. It is a function of the three coordinates x,y,z and returns the radius of the tube representing the field line at that point. To calculate this radius in the examples below, I take the (unscaled) value of the field and get its Norm. Then I scale and constrain it to a reasonable range so that the thickness variations of the field lines don't look too grotesque:

BTW, is there any simple way to change thickness of the tube depending on field strength?
–
RuslanFeb 8 at 14:17

1

@Ruslan That would take some modifications. But you can already display the field strength by using colors, without any modifications to the current code. Just use it with some additional options. E.g., for the Coulomb field, define it as a function f2[x_,y_,z] and then add this to the fieldLinePlot: ColorFunctionScaling -> False, ColorFunction -> Function[{x,y,z,u}, Quiet@Hue[Clip[ Norm[f2[x,y,z]],{0,20}]/20]] The factor 20 determines the cutoff for coloring near singularities. If the thickness were used for field strength, it would look very thick near the charges...
–
JensFeb 8 at 17:50

Of course, I meant that the thickness would have a maximum like clipping does in your color suggestion.
–
RuslanFeb 8 at 18:48

As for the colouring, I think more understandable would be to use GrayLevel[1-#]& instead of Hue.
–
RuslanFeb 8 at 18:53

1

@Ruslan I've edited the code so it can now do line thickness variations.
–
JensFeb 9 at 6:03

You can get the coordinates of the polyhedron easier (and more readable) by using the built-in parameter VertexCoordinates (... /@ PolyhedronData["Icosahedron", "VertexCoordinates"] ...).
–
DavidJan 26 '12 at 4:29

One more thing, the field lines are the integral curves of the vector field, i.e. solutions of $p'(t) = X(p(t))$, there is no division by a norm. (This equation holds on any differentiable manifold, and most of them don't even have a norm.)
–
DavidJan 26 '12 at 4:43

Mathematica is a registered trademark of Wolfram Research, Inc. While the mark is used herein with the limited permission of Wolfram Research, Stack Exchange and this site disclaim all affiliation therewith.