A simple example to illustrate my confusion:
In[1]: f[x_] := x^2
In[2]: g := f'[x]
So now I have:
In[3]: g
out[3]: 2x
as expected
So how come I get the following when trying to pass an argument to g:
In[4]: g[2]
Out[4]: 2x[2]
Instead of the output I want:
*Ou[4]: 4
I tried
In: g[x_] := f'[x]
But it seems to think I'm trying to assign a function to Times.
"SetDelayed::write: Tag Times in (2 x)[y_] is Protected. >>"
Thank you very much for any help :)
Then
g[x_]:=f'[x]
But really, f'[x] is already a perfectly good function itself.
f[x_]:=x^2
f'[x]
f'[2]
Etc.
Helen Read
University of Vermont
g := f'[x]
(No more than f would be had you started with f := x^2.)
Whenever you now call g, you get the value of f[x], namely, 2x.
If you really want to define g that way, then you can use a rule and
replacement:
g /. x-> 2
But it's probably better to do things like this:
Clear[g]
g[x_] = f'[x] (* note Set, =, not SetDelayed, := *)
g[2]
4
Or, if you prefer a more functional approach:
Clear[g]
g = f'
g[2]
4
On 1/11/2011 6:58 AM, Just A Stranger wrote:
> When I apply a function that outputs another function, how come I can not
> pass arguments to the new function? Am I missing some principle about how
> Mathematica functions work? Could someone please point me to the relevant
> documentation?
>
>
> A simple example to illustrate my confusion:
>
> In[1]: f[x_] := x^2
> In[2]: g := f'[x]
>
> So now I have:
>
> In[3]: g
> out[3]: 2x
>
> as expected
>
> So how come I get the following when trying to pass an argument to g:
>
> In[4]: g[2]
> Out[4]: 2x[2]
>
> Instead of the output I want:
>
> *Ou[4]: 4
>
>
> I tried
>
> In: g[x_] := f'[x]
>
> But it seems to think I'm trying to assign a function to Times.
> "SetDelayed::write: Tag Times in (2 x)[y_] is Protected.>>"
>
> Thank you very much for any help :)
>
>
--
Murray Eisenberg mur...@math.umass.edu
Mathematics & Statistics Dept.
Lederle Graduate Research Tower phone 413 549-1020 (H)
University of Massachusetts 413 545-2859 (W)
710 North Pleasant Street fax 413 545-1801
Amherst, MA 01003-9305
I'll sink my teeth into the documentation. :)
In your first example, you are mixing math with programming. When you define
g:=f'[x], you define g as an expression, not a function. Therefore, it will
always use <x>, and can not be passed arguments. You can do this, however:
In[3]:= g /. x -> 2
Out[3]= 4
But this doesn't make g a function. What you could also do is this:
In[4]:=
Clear[g];
g = f';
In[6]:= g[2]
Out[6]= 4
Which is probably what you wanted. What happened here is that f' has been
converted into a pure function (
http://reference.wolfram.com/mathematica/ref/Function.html), and that pure
function has been assigned to g.
You second example would work, had you removed the initial definition for
<g>. The reason for the error message is a bit subtle (while assignment
operator Set ( = ) does not evaluate its l.h.s. in its entirety, it
evaluates its head, which is g, which is why the it attempted to assign to
2x[y_] and barked). Anyway, after you clear g, your code works:
In[8]:=
Clear[g];
g[x_] := f'[x];
g[2]
Out[10]= 4
As for the references to functions and variables: there are 2 main types of
functions in Mathematica - pure functions (which I already mentioned), and
functions defined by patterns (like your function). The majority of
user-defined functions are of the second type, while pure functions are
typically (but not always) used for somewhat different purposes (as building
blocks for functional programs). For the functions defined by patterns, one
important thing to know is that they are actually not functions in the sense
of more traditional programming languages, but transformation rules.
Therefore, if you want to better understand Mathematica functions, you need
to understand transformation rules. You could read on them more at
http://reference.wolfram.com/mathematica/tutorial/TransformationRulesAndDefinitionsOverview.html
and for functions also here:
http://reference.wolfram.com/mathematica/tutorial/FunctionsAndProgramsOverview.html
You may also find useful a chapter in my book where I discuss these topics:
http://www.mathprogramming-intro.org/book/chapterNode5.html
Hope this helps.
Regards,
Leonid
I guess, you tried g[x_]:=f'[x] while g hold still 2*x. Try Clean[g]
before assigning a new value.
I would use
f[x_] := x^2;
g = Derivative[1][f];
g[z]
--> 2 z
Peter
What you apparently meant is
f[x_]:=x^2
g[x_]:=f'[x]
or you could replace the second statement with
g = f'
2 #1 &
(The last line is output of the line before it.)
Bobby
On Tue, 11 Jan 2011 05:58:59 -0600, Just A Stranger
<forpeople...@gmail.com> wrote:
> When I apply a function that outputs another function, how come I can not
> pass arguments to the new function? Am I missing some principle about how
> Mathematica functions work? Could someone please point me to the relevant
> documentation?
>
>
> A simple example to illustrate my confusion:
>
> In[1]: f[x_] := x^2
> In[2]: g := f'[x]
>
> So now I have:
>
> In[3]: g
> out[3]: 2x
>
> as expected
>
> So how come I get the following when trying to pass an argument to g:
>
> In[4]: g[2]
> Out[4]: 2x[2]
>
> Instead of the output I want:
>
> *Ou[4]: 4
>
>
> I tried
>
> In: g[x_] := f'[x]
>
> But it seems to think I'm trying to assign a function to Times.
> "SetDelayed::write: Tag Times in (2 x)[y_] is Protected. >>"
>
> Thank you very much for any help :)
>
>
--Dave