Maple: assign derivative to function

4.2k Views Asked by At

This is probably a basic Maple question. I'm trying to introduce $g$ as the derivative of $f$:

enter image description here

Somewhat puzzling, Maple now says $g$ is two times the function $x()$.

I've tried g := x -> diff(f(x),x) and g := diff(f,x), but no luck. Google tells me I can use subs(x=3,g) to evaluate the derivative at $x=3$, but that's not very practical.

Is there a way to define a Maple function as the derivative of another Maple function?

2

There are 2 best solutions below

1
On BEST ANSWER

Use D (capital D): g:= D(f); This is the functional derivative operator.

In your case:

f:=x->x^2;
g:=D(f);
g(3);
2
On

The answer worked for the OP, but I have had trouble getting "D" to behave the way I want it to. I found a solution using "unapply". If the function is named f, then

fp:= unapply(diff(f(x),x),x) 

creates a function fp that's the derivative of f. diff(f(x), x) is an expression, and unapply converts it to a function of x.

This can also be done for functions of several variables (see Maplesoft's documentation for unapply here: https://www.maplesoft.com/support/help/maple/view.aspx?path=unapply)

UPDATE: Hey there, in case anyone else feels compelled to suggest this be deleted, yes I know what "D" does. It might do what you want most of the time. But I've written code where "D" didn't behave the way I wanted to. unapply is a decent workaround. I see several other Maple users suggesting it in this Maple forum. One of them wrote "I find that in some situations D seems a little stupid, so often I prefer diff with unapply."

If you think there's something wrong with my answer, then a comment might be nice. This workaround has worked for me and others when "D" didn't.