Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I'm trying to make a plot with many objects in it using a While loop.

Something like the following:

Graphics3D[
  While[ϕforwhile < Pi/3, 
    {Lighter[Blue, .1], 
       {Tube[
          Table[
            u*{Sin[Pi/2]*Cos[ϕforwhile], Sin[Pi/2]*Sin[ϕforwhile], Cos[Pi/2]},
            {u, 0, 1, 1/10}],
          .005]}};
    ϕforwhile = ϕforwhile + Pi/12;]]

Does anyone know how to make this work?

share|improve this question
2  
The important thing to realize here is that Tube does not draw a tube. It represents a tube. In other words, Mathematica's graphics language is declarative. Thus what you need to do it not evaluate those Tubes one by one but collect them in a list, and wrap that list with Graphics3D. –  Szabolcs Mar 25 at 1:27
add comment

2 Answers 2

The important thing here is that While produces no output. For that you will need to use something like the Sow Reap suggested by @rasher.

I would recommend using Table here which produces output.

Graphics3D[{Lighter[Blue, .1], 
  Table[{Tube[
     Table[u*{Sin[Pi/2]*Cos[\[Phi]forwhile], 
        Sin[Pi/2]*Sin[\[Phi]forwhile], Cos[Pi/2]}, {u, 0, 1, 
       1/10}], .005]}, {\[Phi]forwhile, 0, Pi/3, Pi/12}]}]
share|improve this answer
add comment
ϕforwhile = 0;

res = Reap[
    While[ϕforwhile < Pi/3, 
     Sow[{Lighter[
        Blue, .1], {Tube[
         Table[u*{Sin[Pi/2]*Cos[ϕforwhile], 
            Sin[Pi/2]*Sin[ϕforwhile], Cos[Pi/2]}, {u, 0, 1, 
           1/10}], .005]}}]; ϕforwhile = ϕforwhile + 
       Pi/12;]][[2]];

Graphics3D[res]

enter image description here

The While on its own will not give you what you're looking for - you need to use things like Sow and Reap, or use Table, etc. While is simply a looping construct, any results are from side-effects and explicit generation like above. Also, note the initialization of the tested symbol. Without that, your initial test fails (try doing such a test on an uninitialized symbol interactively, observe the result...)

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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