Prev: Trouble with coupled quadratic equations where the solutions are
Next: Simplify Exponential Output
From: eric g on 13 Mar 2010 07:57 Hello Group, I know I should avoid For cycles in mathematica, but I am C person... how to do this without For (*--------initialization------------------*) 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]}] (*---------------displaying--------------------*) ParametricPlot[circles, {t, 0, 2 Pi}, PlotStyle -> Black]
From: Guido Tripaldi on 14 Mar 2010 06:12 In this case just using "Table" (since the For cycle is just used in this case to increment an index), and without the need to null-initialize the array (since it is dynamically created during evalutation): (*--------initialization------------------*) n == 10^2; xoi == RandomReal[{-10, 10}, {n}]; yoi == RandomReal[{-10, 10}, {n}]; ri == RandomReal[{0, 10}, {n}]; n == 10^2; circles == Table[{xoi[[i]] + ri[[i]]*Cos[t], yoi[[i]] + ri[[i]]*Sin[t]}, {i, n}]; (*---------------displaying--------------------*) \ ParametricPlot[circles, {t, 0, 2 Pi}, PlotStyle -> Black] using Table you gain also a little extra performance: Timing[For[i == 1, i <== n, i++, circles[[i]] == {xoi[[i]] + ri[[i]]*Cos[t], yoi[[i]] + ri[[i]]*Sin[t]}]] {0.001701, Null} Timing[circles == Table[{xoi[[i]] + ri[[i]]*Cos[t], yoi[[i]] + ri[[i]]*Sin[t]}, {i, n}];] {0.001162, Null} Cheers, G Il giorno 13/mar/2010, alle ore 13.57, eric g ha scritto: > Hello Group, > > I know I should avoid For cycles in mathematica, but I am C person... > how to do this without For > > (*--------initialization------------------*) > 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]}] > > (*---------------displaying--------------------*) > ParametricPlot[circles, {t, 0, 2 Pi}, PlotStyle -> Black] > > > --- Guido Tripaldi
From: Bill Rowe on 14 Mar 2010 06:13 On 3/13/10 at 7:57 AM, eric.phys(a)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]
From: DC on 14 Mar 2010 06:14 n = 5; centers = RandomReal[{-10, 10}, {n, 2}]; radii = RandomReal[{0, 10}, {n}]; circles = {centers[[#, 1]] + radii[[#]] Cos[t], centers[[#, 2]] + radii[[#]] Sin[t]} & /@ Range[n]; ParametricPlot[circles, {t, 0, 2 Pi}, PlotStyle -> Black] -Francesco On 03/13/2010 12:57 PM, eric g wrote: > Hello Group, > > I know I should avoid For cycles in mathematica, but I am C person... > how to do this without For > > (*--------initialization------------------*) > 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]}] > > (*---------------displaying--------------------*) > ParametricPlot[circles, {t, 0, 2 Pi}, PlotStyle -> Black] > > >
From: Bob Hanlon on 14 Mar 2010 06:16 n = 20; xoi = RandomReal[{-10, 10}, n]; yoi = RandomReal[{-10, 10}, n]; ri = RandomReal[{0, 10}, n]; circles = Thread[{ xoi + ri*Cos[t], yoi + ri*Sin[t]}]; ParametricPlot[circles, {t, 0, 2 Pi}] Bob Hanlon ---- eric g <eric.phys(a)gmail.com> wrote: ============= Hello Group, I know I should avoid For cycles in mathematica, but I am C person... how to do this without For (*--------initialization------------------*) 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]}] (*---------------displaying--------------------*) ParametricPlot[circles, {t, 0, 2 Pi}, PlotStyle -> Black]
|
Next
|
Last
Pages: 1 2 Prev: Trouble with coupled quadratic equations where the solutions are Next: Simplify Exponential Output |