Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

FindRoot with a vector of unknowns

76 views
Skip to first unread message

Sam Takoy

unread,
Feb 19, 2012, 6:29:17 AM2/19/12
to
Hi,

Is there an elegant way to implement what I am trying to do here, that
is solve for a vector of unknowns:

FindRoot[x - {1, 2, 3} == {0, 0, 0}, {x, {1, 1, 1}}]

I can do this writing a loop, but hoping for a "vectorized" solution.

Thanks,

Sam

Bob Hanlon

unread,
Feb 20, 2012, 2:46:43 AM2/20/12
to
Solve[#, x][[1]] & /@ Thread[x - {1, 2, 3} == {0, 0, 0}]

{{x -> 1}, {x -> 2}, {x -> 3}}

(Reduce[#, x] // ToRules) & /@ Thread[x - {1, 2, 3} == {0, 0, 0}]

{{x -> 1}, {x -> 2}, {x -> 3}}

FindRoot[#, {x, 1}] & /@ Thread[x - {1, 2, 3} == {0, 0, 0}]

{{x -> 1.}, {x -> 2.}, {x -> 3.}}


Bob Hanlon

Bill Rowe

unread,
Feb 20, 2012, 2:52:22 AM2/20/12
to
On 2/19/12 at 6:28 AM, sam....@yahoo.com (Sam Takoy) wrote:

>Is there an elegant way to implement what I am trying to do here,
>that is solve for a vector of unknowns:

>FindRoot[x - {1, 2, 3} == {0, 0, 0}, {x, {1, 1, 1}}]

>I can do this writing a loop, but hoping for a "vectorized"
>solution.

Elegant is subjective and using MapThread to solve this may not
meet your notion of "vectorized". But here is how to to what you
seem to want using MapThread

In[1]:= MapThread[
FindRoot[(x - #1) == #2, {x, #3}] &, {{1, 2, 3}, {0, 0, 0},
{1, 1,
1}}]

Out[1]= {{x->1.},{x->2.},{x->3.}}


Murray Eisenberg

unread,
Feb 20, 2012, 2:53:54 AM2/20/12
to
Assuming, as you have indicated, that you want to solve several given
equations separately for the _same_ variable, the following will work
(although I'm not sure how "elegant" it is):

eqns = Thread[x - {1, 2, 3} == {0, 0, 0}];
starts = {1, 1, 1};
MapThread[FindRoot, {eqns, List[x, #] & /@ starts}]

On 2/19/12 6:28 AM, Sam Takoy wrote:
> Hi,
>
> Is there an elegant way to implement what I am trying to do here, that
> is solve for a vector of unknowns:
>
> FindRoot[x - {1, 2, 3} == {0, 0, 0}, {x, {1, 1, 1}}]
>
> I can do this writing a loop, but hoping for a "vectorized" solution.
>
> Thanks,
>
> Sam
>

--
Murray Eisenberg mur...@math.umass.edu
Mathematics & Statistics Dept.
Lederle Graduate Research Tower phone 413 549-1020 (H)
University of Massachusetts 413 545-2859 (W)
710 North Pleasant Street fax 413 545-1801
Amherst, MA 01003-9305

Oleksandr Rasputinov

unread,
Feb 20, 2012, 2:54:55 AM2/20/12
to
Well, this is implicitly a loop, but perhaps it's a little more elegant
than what you may have been thinking of:

MapThread[
FindRoot[x - #1 == #2, {x, #3}] &,
{{1, 2, 3}, {0, 0, 0}, {1, 1, 1}}
]

giving:

Szabolcs Horvát

unread,
Feb 20, 2012, 2:57:59 AM2/20/12
to
The documentation explicitly mentions that FindRoot supports vector
variables. So what's wrong here? You'll immediately see the problem if
you evaluate the equation alone:

In[6]:= x - {1, 2, 3} == {0, 0, 0}

Out[6]= {-1 + x, -2 + x, -3 + x} == {0, 0, 0}

Plus[] has attribute Listable which means that it will auto-thread over
lists.

## What is the workaround?

One way is defining a function to use in FindRoot:

f[x : {__?NumericQ}] := x - {1, 2, 3}

FindRoot[f[x], {x, {1, 1, 1}}]

(* ==> {x -> {1., 2., 3.}} *)

The special pattern used in the definition of f[] is essential: it
prevents f[] from evaluating when symbols (not numbers) are passed to it.


## This is good when f[] is a big function, but are there easier
workarounds for tiny functions this this one?

I am not aware of any, but I'd very much like to see some! (Looking
forward to the answers)



--
Szabolcs Horvát
Visit Mathematica.SE: http://mathematica.stackexchange.com/

Ray Koopman

unread,
Feb 21, 2012, 6:16:47 AM2/21/12
to
On Feb 19, 3:29 am, Sam Takoy <sam.ta...@yahoo.com> wrote:
> Hi,
>
> Is there an elegant way to implement what I am trying to do here,
> that is, solve for a vector of unknowns:
>
> FindRoot[x - {1, 2, 3} == {0, 0, 0}, {x, {1, 1, 1}}]
>
> I can do this writing a loop, but hoping for a "vectorized" solution.
>
> Thanks,
>
> Sam

I usually use different symbols for the vector and its elements.

In[1]:= Clear[x]; X = Array[x,3];
FindRoot[X - {1,2,3} == {0,0,0}, Thread@{X,1}]

Out[2]= {x[1] -> 1., x[2] -> 2., x[3] -> 3.}

0 new messages