From: Allamarein on 25 Feb 2010 07:44 I want to define f[x_]=a x^2 and after g[a_,f_]= 2a+f so g is an "a" "x" function and where "f" is the previous function. Giving to g a value for "a" and "x", I'd get a correct "g" result. Which is it the correct listed?
From: Christoph Lhotka on 25 Feb 2010 17:37 hi, as far as I understand your post, f[x_]:=a x^2, then g[a_,x_]:=2a*f[x] and g[a,x] should return, what you desire, chr! Allamarein wrote: > I want to define > f[x_]=a x^2 > and after > g[a_,f_]= 2a+f > so g is an "a" "x" function and where "f" is the previous function. > Giving to g a value for "a" and "x", I'd get a correct "g" result. > Which is it the correct listed? > > > >
From: Bill Rowe on 26 Feb 2010 04:08 On 2/25/10 at 7:45 AM, matteo.diplomacy(a)gmail.com (Allamarein) wrote: >I want to define f[x_]=a x^2 and after g[a_,f_]= 2a+f so g is an "a" >"x" function and where "f" is the previous function. Giving to g a >value for "a" and "x", I'd get a correct "g" result. Which is it the >correct listed? There are several ways to achieve what you want. The smallest change to your code needed to obtain the desired result would be to define the functions as: f[x_]=a x^2; g[a_,x_}= 2 a + f[x]; But, usually (not always) it is better to use SetDelayed (:=) rather than Set (=) so that evaluate occurs when the function is used rather than when it is defined. That would be done as follows: f[x_]:= a x^2 g[a_, x_]= 2 a + f[x]; Note, I've used Set not SetDelayed in the definition of g. Since f is defined in terms of a single argument x, it is necessary to evaluate f[x] when defining g. Otherwise, f will not see the value of a provided to g. While the above works, I would do the following: f[x_, a_]:= a x^2 g[x_, a_]:= 2 a f[x, a] This causes evaluation to occur when g is evaluated and makes both f and g depend on arguments passed to them. Making functions depend on things not directly passed to them often causes unexpected results when the function is used elsewhere in a notebook. And this kind of problem can be difficult to find.
|
Pages: 1 Prev: minimize's constraint Next: Manipulate with variable number of controls |