Re: Starting values with FindRoot
- To: mathgroup at smc.vnet.net
- Subject: [mg20652] Re: Starting values with FindRoot
- From: "Allan Hayes" <hay at haystack.demon.co.uk>
- Date: Sun, 7 Nov 1999 02:09:55 -0500
- References: <7vrbfp$2hg@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
Benoit:
Your data
Equations
eq = Table[z[i]^2 == 2 i, {i, 2}]
{z[1]^2 == 2, z[2]^2 == 4}
Starts
x0 = Table[{z[i], i + .5}, {i, 2}]
{{z[1], 1.5}, {z[2], 2.5}}
Some solutions
FindRoot[eq, ##] & @@ x0
{z[1] -> 1.41421, z[2] -> 2.}
FindRoot[eq, Evaluate[Sequence @@ x0]]
{z[1] -> 1.41421, z[2] -> 2.}
With[{x0 = Sequence @@ x0}, FindRoot[eq, x0]]
{z[1] -> 1.41421, z[2] -> 2.}
Alternatively, you might store the starts in a sequence
x0 = Sequence @@ Table[{z[i], i + .5}, {i, 2}]
Sequence[{z[1], 1.5}, {z[2], 2.5}]
Then:
FindRoot[eq, ##] &[x0]
{z[1] -> 1.41421, z[2] -> 2.}
FindRoot[eq, Evaluate[x0]]
{z[1] -> 1.41421, z[2] -> 2.}
With[{x0 = x0}, FindRoot[eq, x0]]
{z[1] -> 1.41421, z[2] -> 2.}
Allan
---------------------
Allan Hayes
Mathematica Training and Consulting
Leicester UK
www.haystack.demon.co.uk
hay at haystack.demon.co.uk
Voice: +44 (0)116 271 4198
Fax: +44 (0)870 164 0565
Benoit Carmichael <benoit at ecn.ulaval.ca> wrote in message
news:7vrbfp$2hg at smc.vnet.net...
>
> I am fairly new to Mathematica. I have to solve numerically a large set
> of nonlinear equations. My question is: How can I set the starting
> values without having to explicitly name every variable? Here is an
> example of what I mean: Suppose I try to solve numerically the following
> two equations:
> eq = Table[z[i]\^2 == 2 i, {i, 2}]
>
> It would be nice if starting values could be set as a List. Example:
>
> x0 = Table[{z[i], i + .5}, {i, 2}]
>
> and to solve the equations with the following statement:
>
> FindRoot[eq,x0]
>
> Unfortunetly, it does not work. Mathematica does not seem to accept
> starting values as lists. The problem appears to be the outer {}. In
> place of x0 above, Mathematica looks for something like
> {z[1],1.5},{z[2],2.5}, while x0={ {z[1],1.5},{z[2],2.5} }. There is an
> extra set of {}.
>
> I have found a solution by using a chain of FullForm[], ToString[],
> StringDrop[] and ToExpression[]. Here is my solution:
>
> ToExpression[ToString[FindRoot] <> "[" <> ToString[FullForm[eq]] <> ","
> <>
> StringDrop[StringDrop[ToString[x0], 1], -1] <> "]"]
>
> Althought this works OK, it is nevertheless a very roundabout way of
> doing a simple thing. Anyone knows a more elegant way of doing this?
>
>
>
> Thanks in advance
>
> Benoit Carmichael
> Professeur
> Departement d'economique
> Pavillon J.-A. de Seve
> Cite Universitaire
> Ste-Foy (Quebec)
> Canada, G1K 7P4
> Tel.: (418) 656-2131 #5442
>