I have to evaluate the components of a 6x6 matrix $S$. This matrix depends upon the semi-axes of an ellipsoid $a1$, $a2$ and $a3$.

For various specific cases (sphere, prolate spheroid, oblate spheroid, cylinder) I have the close-form solution for $S$.

However, in the general ellipsodal case S is calculated numerically.

Let's say that in:

  • Subsection_1: I give S for the case of sphere
  • Subsection_2: I give S for the case of prolate spheroid
  • Subsection_3: I give S for the case of oblate spheroid
  • Subsection_4: I give S for the case of cylinder
  • Subsection_5: I give for the case of a general ellipsoid

What I want is for the correct form of the function S[a1, a2, a3] to be called depending to the values of $a1$, $a2$, $a3$.

If, for instance, $a1=1$, $a2=1$, $a3=1000$, then Subsection_4 (and only this) will be executed and for the rest of the Section S will have the corresponding value for the cylinder case.

How can I accomplish this?

  • 1
    $\begingroup$ Work out the conditions that should determine the choice of subsection. The choice of method to accomplish your goal will depend on the exact form of your conditions. In general, you could consider making multiple definitions of the same function S[a1, a2, a3] using pattern conditions. When you call S, the arguments will be compared to the patterns you imposed, and only the functional form that matches the pattern will be used. $\endgroup$
    – MarcoB
    May 28, 2015 at 16:48
  • $\begingroup$ Also, what does $S$ represent? $\endgroup$
    – MarcoB
    May 28, 2015 at 16:54
  • $\begingroup$ Thanks for the reply and edits. $\endgroup$
    – Dimitris
    May 28, 2015 at 16:58
  • $\begingroup$ S represents the components of Eshelby tensor (related to a ellipsoidal inclusion) $\endgroup$
    – Dimitris
    May 28, 2015 at 16:59

2 Answers 2


On second thought, you could also use a Which statement to direct your function's flow. Consider the following sample function:


S[a1_, a2_, a3_] := Module[

  sortedargs = SortBy[-# &][{a1, a2, a3}];

   (* sphere *)
   a1 == a2 == a3, 
   Print["It's a sphere\n"]; 
   Print["Its volume is ", Volume@Ball[{0, 0, 0}, a1]];
   Return["from sphere"],

   (* oblate spheroid *)
   sortedargs[[2]] == sortedargs[[3]],
   Print["It's a prolate spheroid"];
   Print["Its major semi-axis is ", sortedargs[[1]] ];
   Return["from prolate spheroid"],

   (* prolate spheroid *)
   sortedargs[[1]] == sortedargs[[2]],
   Print["It's an oblate spheroid"];
   Print["Its minor semi-axis is ", sortedargs[[3]] ];
   Return["from oblate spheroid"],

   (* generic ellipsoid spheroid *)
   Print["It's a generic ellipsoid"];
   Print["Its semi-axes are ", sortedargs ];
   Return["from generic ellipsoid"]

Its behavior depends on the magnitudes of the three arguments:

S[100, 100, 100]

Mathematica graphics

S[10, 100, 10]

Mathematica graphics

S[10, 20, 20]

Mathematica graphics

S[1, 2, 3]

Mathematica graphics

  • 1
    $\begingroup$ Personally, I'd switch from a Which[]/Switch[]/Piecewise[]/If[] to multiple definitions if the code is sufficiently complicated for each case that trying to put them all in one definition renders the function a lot less maintainable. $\endgroup$ May 28, 2015 at 21:13
  • $\begingroup$ @Guesswhoitis. I had started going down that road, but since the choice tree here is pretty simple, and the OP's calculations straightforward, so I went for the quick and dirty route. dimitris, let us know if this adapts to your problem, and we'll try to go from there... $\endgroup$
    – MarcoB
    May 28, 2015 at 21:29
  • $\begingroup$ Thanks for the replies. @ MarcoB I will see if your approach adapts to my problem. Otherwise I will try to proceed as Guess who it is. If there is problem I will return here:-)! $\endgroup$
    – Dimitris
    May 29, 2015 at 8:03
  • $\begingroup$ Btw, I didn't notice. Marco your approach does not cover the case of cylinder. Say for a1=1, a2, a3=500 I want to avoid the lengthy numerical calculation of the S-matrix components (which it is not symmetric). I had a closed-form solution for elliptic cylinder that I want to apply. How should I modify your code? $\endgroup$
    – Dimitris
    May 29, 2015 at 8:07
  • $\begingroup$ @dimitris In your example of a cylinder above, you said that a1=1, a3=500; what values of a2 would the function receive in that case? $\endgroup$
    – MarcoB
    May 29, 2015 at 13:51

Thanks to the aid of MarcoB I found the solution.

The function has to evaluate the components of Eshelby Tensor according to the values of ellipsoidal inclusion (do not bother about the physical meaning).

Due to the lengthy approach, I uploaded the notebook in the following link



Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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