Defining two functions at once?
- To: mathgroup at smc.vnet.net
- Subject: [mg70730] Defining two functions at once?
- From: AES <siegman at stanford.edu>
- Date: Wed, 25 Oct 2006 01:39:55 -0400 (EDT)
- Organization: Stanford University
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?
- Follow-Ups:
- Re: Defining two functions at once?
- From: "Chris Chiasson" <chris@chiasson.name>
- Re: Defining two functions at once?
- From: Andrzej Kozlowski <akoz@mimuw.edu.pl>
- Re: Defining two functions at once?