MathGroup Archive 2010

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

Search the Archive

Re: loop for with plot

  • To: mathgroup at
  • Subject: [mg110042] Re: loop for with plot
  • From: Helen Read <hpr at>
  • Date: Sun, 30 May 2010 23:44:42 -0400 (EDT)
  • References: <httfog$sds$>
  • Reply-to: HPR <read at>

On 5/30/2010 6:47 AM, maria giovanna dainotti wrote:
> 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

For (and similarly Do) by default returns Null as output. If you want to 
see the plot at each iteration in the loop, you need to explicitly print 
it, like this.

For[e3 = 0, e3 < 4, e3++,
  Print[Plot[2*R3*Sin[ArcCos[x/R3]]*e3, {x, -2 R3, 2 R3}]]]

That said, it would be much easier to simply make a table. Also, it is 
good practice to begin names of variables and functions with lower case 
letters, to avoid inadvertent conflicts with built-in functions and 
symbols. And lastly, if you intend to vary your parameters, it is worth 
learning to define functions in Mathematica, so that your variables are 
actually variables, instead of redefining constants each time you want 
to make a change.

I would do something like the following. (I've renamed your R3 and e3 as 
a and b respectively.)

f[x_, a_, b_] := 2 a b Sin[ArcCos[x/a]]

Now make a table of plots, for b from 1 to 4, with a=6.

Table[Plot[f[x, 6, b], {x, -10, 10}], {b, 1, 4}]

Or make a plot of a table of functions, if you'd like them all together.

Plot[Table[f[x, 6, b], {b, 1, 4}], {x, -10, 10}]

Now try varying a. I'll label each plot with the value of a.

Table[Plot[Table[f[x, a, b], {b, 1, 4}], {x, -10, 10},
   PlotLabel -> a], {a, 1,5}]

Table[Plot[Table[f[x, a, b], {b, 1, 4}], {x, -10, 10},
   PlotLabel -> a], {a, 2, 10, 2}]

Helen Read
University of Vermont

  • Prev by Date: Re: Styling single bar in BarChart
  • Next by Date: Re: loop for with plot
  • Previous by thread: Re: loop for with plot
  • Next by thread: Re: loop for with plot