Question: creating functions with derivatives in them. How do I stop "invalid input"?

I wish to define a function which is the derivative of another function.

> f:=(x)->x^2:

> g:=(x)->diff(f(x),x):

> g(x);

2 x

> f(2);

4

> g(2);
Error, (in g) invalid input: diff received 2, which is not valid for its 2nd argument

 

I cannot find a way in which I can define the function g, using the functional operator, so that I can actually evaluate g(x).

 

How can I do this?

 

Please Wait...