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

MathGroup Archive 2012

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

Search the Archive

Re: Why the loop gain no result?

  • To: mathgroup at smc.vnet.net
  • Subject: [mg126074] Re: Why the loop gain no result?
  • From: "Nasser M. Abbasi" <nma at 12000.org>
  • Date: Sun, 15 Apr 2012 03:16:16 -0400 (EDT)
  • Delivered-to: l-mathgroup@mail-archive0.wolfram.com
  • References: <jmb7v4$r2e$1@smc.vnet.net>
  • Reply-to: nma at 12000.org

On 4/14/2012 2:11 AM, Huafeng Cao wrote:
> Dear all,
>      I use the' For' loop to solving an equation, such as :
>      For[i = 1, i<  5, i++, a = i; Solve[a*x^2 - 2 x + 1 == 0, x]].The
> 'a' in the body must be updated in every loop with the 'i'. But i
> cann' t gain the result,Somewhere is disorder? I am confused.
>    Thinks in advanced!
>
>

You can use Reap/Sow to collect the result:

Rest@Reap[For[i = 1, i < 5, i++,
    a = i;
    Sow[Solve[a*x^2 - 2 x + 1 == 0, x]]
    ]]

ps. If you want to see each step, you need to use Print[] inside For:

For[i = 1, i < 5, i++,
  a = i;
  Print@Solve[a*x^2 - 2 x + 1 == 0, x]
  ]


--Nasser




  • Prev by Date: Re: Why the loop gain no result?
  • Next by Date: Re: What characters are allowed in mathematica variable names? i.e. how
  • Previous by thread: Re: Why the loop gain no result?
  • Next by thread: dynamically generating options