1
$\begingroup$

I'm running this code to plot an equation but it simply won't work

B = 0.029*Log[r]*Cos[θ][Sin[θ]]^2;

f[x_, y_, z_] = -Grad[Simplify[TransformedField["Spherical" -> "Cartesian", B, {r, θ,   ϕ} -> {x, y, z}]], {x, y, z}]

VectorPlot3D[f[x, y, z], {x, 10, 20}, {y, 10, 20}, {z, 10, 20}]

When I use an equation like

 B = -((0.0003*Cos[θ] (Sin[θ])^2)/r^2)

The plot works, but it doesn't work with the other equation I want.. why?

$\endgroup$
  • $\begingroup$ Remove the square brackets from [Sin[theta]]^2 in your definition of B. Square brackets are only used for function calls in MMA, not to alter operation precedence. $\endgroup$ – MarcoB Apr 20 '16 at 23:13
1
$\begingroup$

As MarcoB pointed out above in the comments, there is an extra set of square brackets in [Sin[θ]]^2 expression. The correct B function should be B = 0.029 Log[r] Cos[θ] Sin[θ]^2;

Square brackets in Mathematica are reserved for enclosing arguments of functions. Here is a link to Wolfram's documentation on howto/UseBracketsAndBracesCorrectly.

More generally though, I often have syntax errors that prevent plots from showing up, because the plot function doesn't evaluate to a numerical expression. Whenever this happens, I evaluate a point in the plot domain to help debug. For example, in this case, evaluating f[10, 10, 10] or f[10, 10, 10]//N returns a list with square brackets in the output that points to the problem. Once you remove the extra square brackets, this evaluates to a numerical expression.

$\endgroup$

Not the answer you're looking for? Browse other questions tagged or ask your own question.