Reputation: 1
how can we take the derivative of something then use it in an expression like
g := t->diff(f(t),t);
this fails because maple does not first take the derivative then apply t, but applies the value of t then tries to differentiate with respect to that value symbolically.
To solve this I usually have to precompute the differential, then copy and paste it. The problem with this method is that any time the original function changes, everything needs to be redone, which can be time consuming.
Is there a better way?
Upvotes: 1
Views: 75
Reputation: 7271
Using the D
operator you can assign the "derivative" of the operator f
to g
, without having first assigned anything to f
.
You can subsequently assign and reassign different operators to f
, after which g
can be called. Calling g
in the example below will not cause f
to be called with any symbolic argument.
restart;
g := D(f);
g := D(f)
f := x->sin(x)+x^2:
g(x);
cos(x) + 2 x
g(2);
cos(2) + 4
f := x->tan(x)+x^3:
g(x);
2 2
1 + tan(x) + 3 x
g(2);
2
13 + tan(2)
Another way to do it is as follows. Note that with this methodology every call to g
will cause Maple to call diff
on the symbolic local T
. If -- after assigning to f
-- you call g
many times then that will be more expensive (even if just for the overhead of checking a memoized result and making the extra function call to diff
). Also, your f
may be something which is not even set up to return appropriately for a non-numeric argument, so the call to f(T)
might be problematic.
restart;
g := proc(t) local T; eval(diff(f(T),T),T=t); end proc:
f := x->sin(x)+x^2:
g(x);
cos(x) + 2 x
g(2);
cos(2) + 4
f := x->tan(x)+x^3:
g(x);
2 2
1 + tan(x) + 3 x
g(2);
2
13 + tan(2)
The D
operator is pretty useful. You could check out its help page.
Upvotes: 0