Mathematica 9 is now available
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: [mg126083] Re: Why the loop gain no result?
  • From: Bob Hanlon <hanlonr357 at gmail.com>
  • Date: Sun, 15 Apr 2012 03:19:22 -0400 (EDT)
  • Delivered-to: l-mathgroup@mail-archive0.wolfram.com
  • References: <201204140710.DAA27683@smc.vnet.net>

>From the documentation: "Unless an explicit Return is used, the value
returned by For is Null." Use Print

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

{{x->1},{x->1}}

{{x -> 1/2 - I/2}, {x -> 1/2 + I/2}}

{{x -> (1/3)*(1 - I*Sqrt[2])}, {x -> (1/3)*(1 + I*Sqrt[2])}}

{{x -> (1/4)*(1 - I*Sqrt[3])}, {x -> (1/4)*(1 + I*Sqrt[3])}}

Or accumulate the results

sol1 = {}; For[a = 1, a < 5, a++,
 AppendTo[sol1, Solve[a*x^2 - 2 x + 1 == 0, x]]]; sol1

{{{x -> 1}, {x -> 1}}, {{x -> 1/2 - I/2}, {x -> 1/2 + I/2}},
   {{x -> (1/3)*(1 - I*Sqrt[2])}, {x -> (1/3)*(1 + I*Sqrt[2])}},
   {{x -> (1/4)*(1 - I*Sqrt[3])}, {x -> (1/4)*(1 + I*Sqrt[3])}}}

Table is more straightforward.

sol2 = Table[Solve[a*x^2 - 2 x + 1 == 0, x], {a, 4}]

{{{x -> 1}, {x -> 1}}, {{x -> 1/2 - I/2}, {x -> 1/2 + I/2}},
   {{x -> (1/3)*(1 - I*Sqrt[2])}, {x -> (1/3)*(1 + I*Sqrt[2])}},
   {{x -> (1/4)*(1 - I*Sqrt[3])}, {x -> (1/4)*(1 + I*Sqrt[3])}}}

Alternatively,

Clear[a]

f[a_] = Solve[a*x^2 - 2 x + 1 == 0, x]

{{x -> (1 - Sqrt[1 - a])/a}, {x -> (1 + Sqrt[1 - a])/a}}

sol1 == sol2 == f /@ Range[4]

True


Bob Hanlon


On Sat, Apr 14, 2012 at 3:10 AM, Huafeng Cao <geotom.cao at gmail.com> 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!
>
>
>
>
>
> Geotom.Cao
> GUCAS
>



  • Prev by Date: Re: What does NotebookDirectory[] do for .m files?
  • Next by Date: Re: computing derivatives and limits
  • Previous by thread: Re: Why the loop gain no result?
  • Next by thread: Re: Why the loop gain no result?