Services & Resources / Wolfram Forums / MathGroup Archive
-----

MathGroup Archive 2010

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: loop for with plot

  • To: mathgroup at smc.vnet.net
  • Subject: [mg110056] Re: loop for with plot
  • From: Guido Tripaldi <guido at gtripaldi.it>
  • Date: Sun, 30 May 2010 23:47:18 -0400 (EDT)

Hi Maria,

the "For" loop in Mathematica is actually an 'expression', not an 'instruction'. This means that the output doesn't depend directly from what is done within the loop, but it depends from what the expression explicitly return, and the default value returned by For is Null. To display every iteration result you have to "Print" it out:

R1 == 1.029;
R2 == 3;
R3 == 6;
e1 == 27;
e2 == 0;
e3 == 2.5;
For[e3 == 0, e3 < 4, e3++,
 Print[Plot[{2*R3*Sin[ArcCos[x/R3]]*e3}, {x, -2 R3, 2 R3}]]]

As you can see if you put the Plot[] inside a Print[], everything will work as you expected.

if you doesn't want to use the Print[], you can append each 'For' iteration result in a list, and use the content of the list (namely your plots) after the For expression:

R1 == 1.029;
R2 == 3;
R3 == 6;
e1 == 27;
e2 == 0;
e3 == 2.5;

myPlots == {}; (* we need an empty initialized list to Append items in it *)

For[e3 == 0, e3 < 4, e3++,
 AppendTo[ myPlots,
  Plot[{2*R3*Sin[ArcCos[x/R3]]*e3}, {x, -2 R3, 2 R3}]]]
myPlots (* display results *)

Perhaps better of all is to use the 'Table' instead of 'For', in output you will obtain the same 'list of plots' of above, but it is more "Mathematica Style:

R1 == 1.029;
R2 == 3;
R3 == 6;
e1 == 27;
e2 == 0;
e3 == 2.5;

myPlots == Table[Plot[{2*R3*Sin[ArcCos[x/R3]]*e3}, {x, -2 R3, 2 R3}], {e3, 0,  3}];

Grid[{myPlots}, Frame -> True] (* display results in a frame *)


cheers,
   G



Il giorno 30/mag/2010, alle ore 12.47, maria giovanna dainotti ha scritto:

> Dear Mathgroup,
>
> when I am using the loop for with plot is not working.
>
> here is the example
> R1==1.029
> R2==3
> R3==6
> e1==27
> e2==0
> e3==2.5
> For[e3==0,e3<4,e3++,Plot[{2*R3*Sin[ArcCos[x/R3]]*e3}, {x, -2R3,2 R3}]]
> How can I sort out the problem?
> Best regards
> Maria
>
>

---
Guido Tripaldi



  • Prev by Date: Re: loop for with plot
  • Next by Date: Re: Basic normal and t table questions
  • Previous by thread: Re: loop for with plot
  • Next by thread: Big memory needed