Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.soft-sys.math.mathematica > #8609 > unrolled thread
| Started by | Sam Takoy <sam.takoy@yahoo.com> |
|---|---|
| First post | 2012-02-19 11:29 +0000 |
| Last post | 2012-02-21 11:16 +0000 |
| Articles | 4 — 4 participants |
Back to article view | Back to comp.soft-sys.math.mathematica
FindRoot with a vector of unknowns Sam Takoy <sam.takoy@yahoo.com> - 2012-02-19 11:29 +0000
Re: FindRoot with a vector of unknowns "Oleksandr Rasputinov" <oleksandr_rasputinov@ymail.com> - 2012-02-20 07:54 +0000
Re: FindRoot with a vector of unknowns Szabolcs Horvát <szhorvat@gmail.com> - 2012-02-20 07:57 +0000
Re: FindRoot with a vector of unknowns Ray Koopman <koopman@sfu.ca> - 2012-02-21 11:16 +0000
| From | Sam Takoy <sam.takoy@yahoo.com> |
|---|---|
| Date | 2012-02-19 11:29 +0000 |
| Subject | FindRoot with a vector of unknowns |
| Message-ID | <jhqmed$f70$1@smc.vnet.net> |
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
[toc] | [next] | [standalone]
| From | "Oleksandr Rasputinov" <oleksandr_rasputinov@ymail.com> |
|---|---|
| Date | 2012-02-20 07:54 +0000 |
| Message-ID | <jhsu8f$np7$1@smc.vnet.net> |
| In reply to | #8609 |
On Sun, 19 Feb 2012 11:29:17 -0000, Sam Takoy <sam.takoy@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
>
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:
{{x -> 1.}, {x -> 2.}, {x -> 3.}}
[toc] | [prev] | [next] | [standalone]
| From | Szabolcs Horvát <szhorvat@gmail.com> |
|---|---|
| Date | 2012-02-20 07:57 +0000 |
| Message-ID | <jhsue7$nqs$1@smc.vnet.net> |
| In reply to | #8609 |
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/
[toc] | [prev] | [next] | [standalone]
| From | Ray Koopman <koopman@sfu.ca> |
|---|---|
| Date | 2012-02-21 11:16 +0000 |
| Message-ID | <jhvuev$865$1@smc.vnet.net> |
| In reply to | #8609 |
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.}
[toc] | [prev] | [standalone]
Back to top | Article view | comp.soft-sys.math.mathematica
csiph-web