You want to make sure that the differential operator is executed AFTER f is
substituted but BEFORE the substitution for x has been made. I.e., when you
call Nf(sin,3), you want to make sure that at one point, diff(sin(x),x) (and
NOT diff(sin(8),8) or diff(f(8),8)) gets evaluated.
I'm sure there's a more elegant way, but the following does the trick and is
not terribly ugly:
(%i1)
Nf(f,a):=block([u,v],fd(x):=''(diff(f(x),x)),u:ev(fd(v),diff),ev(u,v=a+5))$
(%i2) Nf(sin,3);
(%o2) cos(8)
(%i3) Nf(sin,x);
(%o3) cos(x + 5)
The idea is to evaluate fd(x) first with a dummy (non-numeric) argument that
causes the differentiation to be carried out, and only then substitute the
actual argument that could be a number or expression that cannot serve as
the second argument to diff().
Come to think of it, there is a more elegant way, one that doesn't even use
a "local" function:
(%i4) Nf(f,a):=block([x,fd],fd:diff(f(x),x),ev(fd,x=a+5))$
(%i5) Nf(sin,3);
(%o5) cos(8)
The reason why this works is that fd being an assignment gets evaluated with
f having its proper value, but x being a dummy parameter, so the
differentiation is carried out with respect to x BEFORE the call is made to
ev() and the substitution takes place.
By the way, I put "local" in quotes above is because even though you defined
fd() inside Nf(), the definition will not be local; after the first call to
Nf(), fd() will exist in the global name space, which may not be what you
want.
Viktor
-----Original Message-----
From: maxima-admin@math.utexas.edu [mailto:maxima-admin at math] On
Behalf Of jean-denis fouks
Sent: Thursday, December 02, 2004 6:49 AM
To: Maxima@math.utexas.edu
Subject: using functions as arguments
Hi everybody,
Some arguments of a user-defined function can be functions:
----------------------------------------
C1) double(f,a):=2*f(2*a);
(D1) double(f, a) := 2 f(2 a)
(C2) double(sin,%pi/3);
(D2) SQRT(3)
----------------------------------------
A function can be defined as the derivative of another one:
----------------------------------------
(C3) sin2x2(x):=2*sin(x2);
2
(D3) sin2x2(x) := 2 SIN(x )
(C4) df(x):=''(diff(sin2x2(x),x));
2
(D4) df(x) := 4 x COS(x )
(C5) df(3);
(D5) 12 COS(9)
----------------------------------------
The previous construction can be used in the definition of a function:
----------------------------------------
(C6) N(a):=block(dg(x):=''(diff(sin2x2(x),x)),dg(2*a));
2
(D6) N(a) := BLOCK(dg(x) := 4 x COS(x ), dg(2 a))
(C7) N(3);
(D7) 24 COS(36)
-----------------------------------------
So i don't understand why this does not work:
-----------------------------------------
(C8) Nf(f,a):=block(fd(x):=''(diff(f(x),x)),fd(a+5));
d
(D8) Nf(f, a) := BLOCK(fd(x) := -- (f(x)), fd(a + 5))
dx
(C9) Nf(sin,3);
Attempt to differentiate with respect to a number:
8
#0: fd(x=8)
#1: Nf(f=SIN,a=3)
-- an error. Quitting. To debug this try DEBUGMODE(TRUE);)
-------------------------------------------
Can anybody give me the correct syntax.
Thanks in advance,
JDF
_______________________________________________
Maxima mailing list
Maxima@www.math.utexas.edu
http://www.math.utexas.edu/mailman/listinfo/maxima