Re: plotting many curves
- To: mathgroup at smc.vnet.net
- Subject: [mg108333] Re: plotting many curves
- From: Bill Rowe <readnews at sbcglobal.net>
- Date: Sun, 14 Mar 2010 05:13:28 -0500 (EST)
On 3/13/10 at 7:57 AM, eric.phys at gmail.com (eric g) wrote: >I know I should avoid For cycles in mathematica, but I am C >person... how to do this without For >n = 10^2; >xoi = RandomReal[{-10, 10}, {n}]; >yoi = RandomReal[{-10, 10}, {n}]; >ri = RandomReal[{0, 10}, {n}]; >n=10^2; >Clear[circles]; >circles = Table[Null, {n}]; >For[i = 1, i <= n, i++, >circles[[i]] = {xoi[[i]] + ri[[i]]*Cos[t], yoi[[i]] + ri[[i]]*Sin[t]}] >ParametricPlot[circles, {t, 0, 2 Pi}, PlotStyle -> Black] Since all of the function you use to define each circle have the attribute listable, no explicit loop is needed. That is: circles = Transpose@{xoi + ri Cos[t], yoi + ri Sin[t]}; can be used to replace all of the code you use to set up the For loop and the For loop itself. Note, there are further reductions in the amount of code that could be done. The data used to create for the circles can be created in one call. That is n = 10^2; {xo1, yoi, ri} = RandomReal[{-10, 10}, {3, n}]; circles = Transpose@{xoi + ri Cos[t], yoi + ri Sin[t]}; ParametricPlot[circles, {t, 0 2 Pi}, PlotStyle->Black] Will create the same type of plot. You might note, I allow ri to take on negative values. But since you have the ParametricPlot set to go from 0 to 2 Pi, there will be no difference in the resulting plot. That is ParametricPlot[{.5 + Cos[t], .3 + Sin [t]}, {t, 0, 2 Pi}, PlotStyle -> Black] produces exactly the same plot as ParametricPlot[{.5 - Cos[t], .3 - Sin [t]}, {t, 0, 2 Pi}, PlotStyle -> Black]