Re: FindRoot with a vector of unknowns
- To: mathgroup at smc.vnet.net
- Subject: [mg125077] Re: FindRoot with a vector of unknowns
- From: Szabolcs Horvát <szhorvat at gmail.com>
- Date: Mon, 20 Feb 2012 02:53:00 -0500 (EST)
- Delivered-to: l-mathgroup@mail-archive0.wolfram.com
- References: <jhqmed$f70$1@smc.vnet.net>
On 2/19/2012 1:29 PM, 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.
>
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/