Redefining a function with a rule for coefficients
- To: mathgroup at smc.vnet.net
- Subject: [mg59376] Redefining a function with a rule for coefficients
- From: Wonseok Shin <wssaca at gmail.com>
- Date: Sun, 7 Aug 2005 03:47:08 -0400 (EDT)
- Sender: owner-wri-mathgroup at wolfram.com
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,
--
Wonseok Shin
wssaca at gmail.com
- Follow-Ups:
- Re: Redefining a function with a rule for coefficients
- From: Pratik Desai <pdesai1@umbc.edu>
- Re: Redefining a function with a rule for coefficients
- From: Zhengji Li <zhengji.li@gmail.com>
- Re: Redefining a function with a rule for coefficients