Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Turning Derivative into Function (Newbie Question)

1,555 views
Skip to first unread message

Just A Stranger

unread,
Jan 11, 2011, 6:59:17 AM1/11/11
to
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 :)


Helen Read

unread,
Jan 11, 2011, 7:21:04 PM1/11/11
to
Clear[g]

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

Murray Eisenberg

unread,
Jan 11, 2011, 7:21:15 PM1/11/11
to
Because g is NOT a "function" when you set:

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

Just A Stranger

unread,
Jan 11, 2011, 7:22:10 PM1/11/11
to
Thanks guys! That pure function approach looks the most concise...that would
be the best approach with regards to Mathematica's design philosophy?

I'll sink my teeth into the documentation. :)

Leonid Shifrin

unread,
Jan 11, 2011, 7:22:22 PM1/11/11
to
Hi,

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

Peter Pein

unread,
Jan 11, 2011, 7:23:17 PM1/11/11
to
Hi stranger,

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

DrMajorBob

unread,
Jan 11, 2011, 7:23:40 PM1/11/11
to
You didn't make g a function, so it doesn't act like a function.

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 :)
>
>


--
DrMaj...@yahoo.com

Tomas Garza

unread,
Jan 11, 2011, 7:24:13 PM1/11/11
to
If g is to work as a function it should have an argument, which it doesn't in your first definition.When redefining it, you should first Clear the previous definition. Then you get
In[1]:= Clear[g]In[2]:= g[x_]:=f'[x]
In[3]:= g[2]Out[3]= 4
as expected.
-Tomas
> Date: Tue, 11 Jan 2011 06:58:59 -0500
> From: forpeople...@gmail.com
> Subject: Turning Derivative into Function (Newbie Question)
> To: math...@smc.vnet.net

dsn...@charter.net

unread,
Jan 12, 2011, 4:07:04 AM1/12/11
to
Define g as
g[x_]:=f'[x]


--Dave

0 new messages