Re: Defining two functions at once?

• To: mathgroup at smc.vnet.net
• Subject: [mg70750] Re: Defining two functions at once?
• From: bghiggins at ucdavis.edu
• Date: Thu, 26 Oct 2006 02:38:44 -0400 (EDT)
• References: <ehmttr\$sd0\$1@smc.vnet.net>

```Not sure if this will help you :

f[x_, y_] := {f3[x, y], g3[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]

In[2]:=f[3,5]

Out[2]={-5.5,-23.5}

In[6]:={f3[3,5],g3[3,5]}

Out[6]={-5.5,-23.5}

Of course with this approach the arguments of the functions f2 and g3
are not pattern matched

Cheers,

Brian

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?

```

• Prev by Date: Solving a PDE with fixed endpoints
• Next by Date: MathModelica System Designer Professional now available
• Previous by thread: Re: Defining two functions at once?
• Next by thread: Re: Defining two functions at once?