Re: Redefining a function with a rule for coefficients
- To: mathgroup at smc.vnet.net
- Subject: [mg59391] Re: [mg59376] Redefining a function with a rule for coefficients
- From: Pratik Desai <pdesai1 at umbc.edu>
- Date: Mon, 8 Aug 2005 03:34:38 -0400 (EDT)
- References: <200508070747.DAA18011@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
Wonseok Shin wrote: >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, > > As a work around, why not just introduce new function like 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}] // First f1[x_, y_] = f[x, y] /. {coeff[[1]], coeff[[2]]} g1[x_, y_] = g[x, y] /. {coeff[[1]], coeff[[2]]} f1[x, y] >>x + 2 y g1[x,y] >>x-2y f1[1,1] >>3 g1[1,1] >>-1 I hope this is what you are looking for, you may look at f and g as the most general form of your solution and f1 and g1 as the particular solution satisfying your BC Best regards Pratik -- Pratik Desai Graduate Student UMBC Department of Mechanical Engineering Phone: 410 455 8134
- References:
- Redefining a function with a rule for coefficients
- From: Wonseok Shin <wssaca@gmail.com>
- Redefining a function with a rule for coefficients