3
$\begingroup$

I'm new to Mathematica and I'm having trouble plotting the electric field which I calculated in the framework of Mie theory. I'm not sure which kind of plot is appropriate here.

Note, my electric field is the sum of a series of vector spherical harmonics and also contains an imaginary component. I'm not sure if this affects the plot. As you can see, my plot doesn't show anything.

I've attached my code and would really appreciate any advice. Thanks.

(* Spherical Vector Harmonics *)
divNt = D[x*SphericalHankelH1[n, t], t] /. t -> x ;
Nr = Table[Cos[\[Phi]] n (n + 1) Sin[\[Theta]]*pin*SphericalHankelH1[n, x]/x, {n, 1, nstop}];
Nt = Table[Cos[\[Phi]]*\[Tau]n*(divNt/x), {n, 1, nstop}];
Np = Table[-Sin[\[Phi]]*pi*(divNt/x), {n, 1, nstop}];
Mt = Table[Cos[\[Phi]]*pi*SphericalHankelH1[n, x], {n, 1, nstop}]; 
Mp = Table[-Sin[\[Phi]]*\[Tau]*SphericalHankelH1[n, x], {n, 1, nstop}];

(* E-field*)
Esr = Total[En*(I*an*Nr - bn*0)];
Est = Total[En*(I*an*Nt - bn*Mt)];
Esp = Total[En*(I*an*Np - bn*Mp)]; 
Es = Sqrt[(Esr)^2 + (Est)^2 + (Esp)^2];

(* Plotting E field *)
ContourPlot[Es[\[Theta], \[Phi]], {\[Theta], 0, \[Pi]}, {\[Phi], 0, 2 \[Pi]}];
$\endgroup$

1 Answer 1

7
$\begingroup$

Welcome to MMA SE! There are several things preventing this from working.

  • The ; after ContourPlot will suppress the output; simply remove it.
  • Es is not defined as a function, and so the syntax Es[x,y] will not evaluate to what you expect. To fix this, simply plot Es (no arguments), or (better practice) turn all of your symbols into functions via the syntax f[x_] := <expression involving x>; see this. (Also see this for the difference between = and :=; for speed you might want to use the trickier = for function definitions so that you're not evaluating each right-hand-side from scratch each time. Good variable hygiene—ClearAll[x]s or Blocks, as in Block[{x, y}, f[x_, y_] = <expression involving x, y>]—then becomes essential. So, trickier, but for heavy computation, maybe useful; see if the simpler := works first, though.)
  • En, an, bn, \[Tau]n, \[Tau], nstop, and x appear not to have definitions. (Note that \[Tau]n is completely unrelated to \[Tau]—it's just a different symbol name. Not sure if this is relevant to your use case.) This will prevent them from evaluating to anything numerical.
  • If Es is complex-valued, it won't plot. Try instead Sqrt[Abs[Esr]^2 + Abs[Est]^2 + Abs[Esp]^2]
  • pi is not Mathematica's built-in symbol for $\pi$; instead, it is Pi or \[Pi]. In general, all built-in symbols start with a capital letter. (Of course, if the pi in your code is actually just another variable with no definition given here, there's no problem!)

Good luck, and feel free to comment or update your question if problems still arise or if you have any questions on what I've said!

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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