MathGroup Archive 2005

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

Search the Archive

Re: Redefining a function with a rule for coefficients

  • To: mathgroup at smc.vnet.net
  • Subject: [mg59415] Re: Redefining a function with a rule for coefficients
  • From: Wonseok Shin <wssaca at gmail.com>
  • Date: Tue, 9 Aug 2005 03:30:46 -0400 (EDT)
  • References: <dd4fd7$htb$1@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

On 2005-08-07 01:05:59 -0700, Wonseok Shin <wssaca at gmail.com> said:

> Suppose that there are two functions f and g:
> f[x_, y_] := A x + B y;	
> g[x_, y_] := A x - B y;
> where the coefficients A and B are unknown.
> 
> We know some kind of boundary condition f[1, 1] == 3 and g[1, 1] == -1. 
>   I want to solve for coefficients A and B using this boundary 
> condition, and to put them back into f and g.
> 
> Of course, solving for A and B is very straightforward:
> coeff = Solve[{f[1, 1] == 3, g[1, 1] == -1}, {A, B}]
> 
> This gives the solution in the form:
> {{A -> 1, B -> 2}}
> which is the rule assigned in the variable 'coeff.'
> 
> The next process is, of course, to put 'coeff' into f and g, and to 
> redefine them to be x - 2y and x + 2y.  What is the most standard way 
> of doing this?
> 
> Here is my solution:
> f[x_, y_] = f[x, y] /. coeff[[1]];
> g[x_, y_] = g[x, y] /. coeff[[1]];
> 
> Note that I used Set (=) instead of SetDelayed (:=).
> 
> It works but looks clumsy, and has a potential error.  Look at the 
> following codes:
> In[1]:=
> f[x_, y_] := A x + B y;
> g[x_, y_] := A x - B y;
> 
> In[3]:=
> x = 1;
> ?f
> 
> Global`f
> f[x_, y_] := A x + B y
> 
> (* The assignment x = 1 does not affect the definition of f[x_, y_]. *)
> 
> In[5]:=
> coeff = Solve[{f[1, 1] == 3, g[1, 1] == -1}, {A, B}]
> 
> Out[5]=
> {{A -> 1, B -> 2}}
> 
> In[6]:=
> f[x_, y_] = f[x, y] /. coeff
> 
> Out[6]=
> 1 + 2 y
> 
> In Out[6] our expectation is x + 2 y, but since we've assigend 1 to x, 
> this specific value is used for x in the Set procedure in In[4].  Using 
> SetDelayed (:=) instead of Set (=) generates more serious problem, 
> because it causes an infinite recursion when we evaluate f, for 
> example, at (x, y) = (1, 1).
> 
> Since determining coefficients of functions using a boundary condition 
> is very common situation, I believe there exists some standard and 
> elegant way to do this.
> 
> Thanks,

Hi, I'm the one who raised the original question.
It seems that I found out the most elegant way to solve my problem.  
Here it is:

In[1]:=
f[x_, y_] := A x + B y;
g[x_, y_] := A x - B y;
coeff = Solve[{f[1, 1] == 3, g[1, 1] == -1}, {A, B}]

Out[3]=
{{A -> 1, B -> 2}}

In[4]:=
x=1; y=1;

In[5]:=
DownValues[f] = DownValues[f] /. coeff;
DownValues[g] = DownValues[g] /.coeff;

In[7]:=
?f

Global`f
f[x_, y_] := 1 x + 2 y

In[8]:=
?g

Global`g
g[x_, y_] := 1 x - 2 y

(* The definitions of functions f and g are not affected by the 
assignment x = 1 and y = 1.  Moreover, each function keeps only one 
definition.  If we try something like f[x_, y_] = f[x, y] /. coeff[[1]] 
as I wrote in the original posting, the function f has two definitions. 
 You can see it by running "?f".)

-- 
Wonseok Shin
wssaca at gmail.com


  • Prev by Date: Re: Re: Some bugs in Mathematica
  • Next by Date: PlotVectorField
  • Previous by thread: Re: Redefining a function with a rule for coefficients
  • Next by thread: Re: Simplify Oddity