Re: Redefining a function with a rule for coefficients
- To: mathgroup at smc.vnet.net
- Subject: [mg59386] Re: Redefining a function with a rule for coefficients
- From: "Jens-Peer Kuska" <kuska at informatik.uni-leipzig.de>
- Date: Mon, 8 Aug 2005 03:34:33 -0400 (EDT)
- Organization: Uni Leipzig
- References: <dd4fd7$htb$1@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
Hi, f[A_,B_][x_,y_]:= A x + B y g[A_,B_][x_,y_]:= A x - B y coeff = Solve[{f[A, B][1, 1] == 3, g[A, B][1, 1] == -1}, {A, B}]; res={f[A, B], g[A, B]} /. coeff[[1]] Plot3D[res[[1]][x, y], {x, 0, 1}, {y, 0, 1}] Regards Jens "Wonseok Shin" <wssaca at gmail.com> schrieb im Newsbeitrag news:dd4fd7$htb$1 at smc.vnet.net... | 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 |