Re: Defining two functions at once?
- To: mathgroup at smc.vnet.net
- Subject: [mg70765] Re: Defining two functions at once?
- From: Jens-Peer Kuska <kuska at informatik.uni-leipzig.de>
- Date: Thu, 26 Oct 2006 02:39:22 -0400 (EDT)
- References: <ehmttr$sd0$1@smc.vnet.net>
Hi,
and it does not help to define a single function
that return a vector ??
FAndG[x_,y_]:=Module[{myEqns,myValues,mySolns},
myEqns = {f1 - g1 == x1 + 3y1, f1 + g1 == 2x1 - 7y1};
myValues = {x1 -> x, y1 -> y};
mySolns = FindRoot[ myEqns /. myValues, {f1, 0} , {g1, 0}];
{f1} /. mySolns]
f[x_,y_]:=FandG[x,y][[1]]
g[x_,y_]:=FandG[x,y][[2]]
Regards
Jens
AES wrote:
> I want to define two functions at once, where both of their values
> depend on the same two variables, and both of their values come out of a
> single two-dimensional FindRoot which I'd rather not evaluate twice.
>
> The following approach seems to work fine for a *single* function
>
> f[x_, y_] := Module[{},
> myEqns = {f1 - g1 == x1 + 3y1, f1 + g1 == 2x1 - 7y1};
> myValues = {x1 -> x, y1 -> y};
> mySolns = FindRoot[ myEqns /. myValues, {f1, 0} , {g1, 0}];
> f1 /. mySolns]
>
> But if I try to define two functions at once by replacing the first and
> last lines with
>
> {f[x_, y_], g[x_,y_]} := Module[{},
> myEqns = {f1 - g1 == x1 + 3y1, f1 + g1 == 2x1 - 7y1};
> myValues = {x1 -> x, y1 -> y};
> mySolns = FindRoot[ myEqns /. myValues, {f1, 0} , {g1, 0}];
> {f1, g1} /. mySolns]
>
> I get a message about "shapes not being the same". In fact, if I just
> make the first and last lines even a single element list, e.g.
>
> {f[x_, y_]} := Module[{},
> myEqns = {f1 - g1 == x1 + 3y1, f1 + g1 == 2x1 - 7y1};
> myValues = {x1 -> x, y1 -> y};
> mySolns = FindRoot[ myEqns /. myValues, {f1, 0} , {g1, 0}];
> {f1} /. mySolns]
>
> this doesn't work either.
>
> If a module supposedly returns the result of its (compound) expression,
> and the final term in that expression is a list, shouldn't the final
> example work? More important (to me anyway): Is there a simple way to
> define two functions that use a shared FindRoot evaluation in a way that
> (a) evaluates the FindRoot only once, and (b) involves only a single
> compound expression of some sort?
>