Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.soft-sys.math.mathematica > #9660

Re: Why the loop gain no result?

From Bob Hanlon <hanlonr357@gmail.com>
Newsgroups comp.soft-sys.math.mathematica
Subject Re: Why the loop gain no result?
Date 2012-04-15 07:21 +0000
Organization Steven M. Christensen and Associates, Inc and MathTensor, Inc.
Message-ID <jmdsti$adf$1@smc.vnet.net> (permalink)
References <201204140710.DAA27683@smc.vnet.net>

Show all headers | View raw


>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@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
>

Back to comp.soft-sys.math.mathematica | Previous | Next | Find similar | Unroll thread


Thread

Re: Why the loop gain no result? Bob Hanlon <hanlonr357@gmail.com> - 2012-04-15 07:21 +0000

csiph-web