Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion Groups
Mathematics
General TopicsResearchOperations ResearchStatisticsMathematical LogicNumerical AnalysisUndergraduate MathAlgebra HelpRecreational Math
Math Software
MapleMathematicaMATLABScilabSASSPSS

Math Forum / Math Software / Mathematica / February 2010



Tip: Looking for answers? Try searching our database.

Functions

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Allamarein - 25 Feb 2010 12:44 GMT
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?
Bill Rowe - 26 Feb 2010 09:08 GMT
>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.
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2010 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.