MathGroup Archive 2006

[Date Index] [Thread Index] [Author Index]

Search the Archive

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?


  • Prev by Date: Re: Solve with dot products
  • Next by Date: Re: [TS 27419]--Re:TreePlot from DiscreteMath`GraphPlot shadows TreePlot from DiscreteMath`Tree
  • Previous by thread: Correction: new procedure for converting a new recursive polynomial set into matrices
  • Next by thread: Re: Defining two functions at once?