Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.soft-sys.math.mathematica > #9660 > unrolled thread
| Started by | Bob Hanlon <hanlonr357@gmail.com> |
|---|---|
| First post | 2012-04-15 07:21 +0000 |
| Last post | 2012-04-15 07:21 +0000 |
| Articles | 1 — 1 participant |
Back to article view | Back to comp.soft-sys.math.mathematica
This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by
below is the oldest one visible, not the original post.
Re: Why the loop gain no result? Bob Hanlon <hanlonr357@gmail.com> - 2012-04-15 07:21 +0000
| From | Bob Hanlon <hanlonr357@gmail.com> |
|---|---|
| Date | 2012-04-15 07:21 +0000 |
| Subject | Re: Why the loop gain no result? |
| Message-ID | <jmdsti$adf$1@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@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 top | Article view | comp.soft-sys.math.mathematica
csiph-web