0% found this document useful (0 votes)
17 views6 pages

Derivatives

Derivative

Uploaded by

drmpsd
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views6 pages

Derivatives

Derivative

Uploaded by

drmpsd
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Differentiation With wxMaxima

1 Basic Introduction

The command to differentiate any expression in Maxima is diff() and it has the following syntax:

diff(expression, variable, [order-of-derivative (only if greater than 1)])

So let's say we want to take the first three derivatives of an 3 sin(x):

--> diff(3*sin(x),x);

(%o1) 3 cos (x)

--> diff(3*sin(x),x,2);

(%o2) −3 sin (x)

--> diff(3*sin(x),x,3);

(%o3) −3 cos (x)

If we have already defined a function f(x), then we can differentiate using the f(x) symbol:

(%i5) f(x):=3*sin(x)$
diff(f(x),x); diff(f(x),x,2); diff(f(x),x,3);

(%o3) 3 cos (x)

(%o4) −3 sin (x)

(%o5) −3 cos (x)

NOTE: We cannot omit the (x) in the expression f(x), since:

(%i6) diff(f,x);

(%o6) 0

Maxima thinks that you want the derivative of the symbolic expression f, which has no x dependence,
and therefore yields 0 upon differentiation.

The ev(,nouns) trick works with diff() too:

(%i9) f(x):=3*x^2*tan(5*x^2)$
D:'diff(f(x),x,2)$
D=ev(D,nouns);

d2 2 2
(%o9) (3x2 tan (5x2 )) = 600x4 sec (5x2 ) tan (5x2 ) + 6 tan (5x2 ) + 150x2 sec (5x2 )
dx2

2 Catching the Output in a New Function

Let's say we want to differentiate a function and then find where its derivative is zero, and also we want to calculate some values of the derivative. We
need to be able to "catch" the derivative of that function as a new function. Here's how we do that:

[Link] 27/08/24, 10 04 PM
Page 1 of 6
:
(%i10) f(x):= sqrt(x)*exp(x^2+2*x);

(%o10) f(x) := √−
x exp(x2 + 2x)

(%i11) define(fp(x),diff(f(x),x));

2
%ex +2x
(%o11) fp(x) := √−
2
x (2x + 2) %ex +2x +
2√−x

While this is a little ungainly, most all CASes (like Mathematica and Maple) do this.

So why can't we just define g(x) := diff(f(x),x)? Let's try it:

(%i13) f(x); g(x):=diff(f(x),x);

(%o12) √−
2
x %ex +2x

d
(%o13) g(x) := f(x)
dx

(%i14) g(x);

2
%ex +2x
(%o14) √−
2
x (2x + 2) %ex +2x +
2√−x

Wait... that looks pretty good. Infact, there doesn't seem to be anything wrong, until we ask a very simple question:

(%i15) g(1);

diff: second argument must be a variable; found 1\ensuremath{\neq}0: g(x=1) -- an error. To debug this try: debugmode(true);

Ahk! What happend is that with g(x):=diff(f(x),x) when we ask for g(1)
Maxima interprets this as: diff(f(1),1) and since the second argument of the diff() command must be a variable,
the request fails. So, the correct syntax is:

(%i19) define(g(x),diff(f(x),x))$ g(1);

9%e3
(%o19)
2

3 Graphing derivatives

Now that we can catch derivatives in their own functions, we can do all the usual things we would like to with them.
For example, we could graph them along side their functions:

(%i22) f(x):=sin(x);define(g(x),diff(f(x),x)); define(h(x),diff(f(x),x,2));

(%o20) f(x) := sin (x)

(%o21) g(x) := cos (x)

(%o22) h(x) := − sin (x)

(%i23) wxplot2d([f(x),g(x),h(x)],[x,0,2*%pi],[legend,"sin(x)","Derivative", "Second Derivative"]);

(%t23)

[Link] 27/08/24, 10 04 PM
Page 2 of 6
:
(%o23)

(%i24) kill(all);

(%o0) done

Let's try another example in which we plot the first three derivatives of:

(%i2) f(x):=sqrt(cos(x)+2);

−−−−−−−−−
(%o2) f(x) := √cos (x) + 2

(%i7) define(g(x),diff(f(x),x)); define(h(x),diff(f(x),x,2));define(i(x),diff(f(x),x,3));

sin (x)
(%o5) g(x) := − −−−−−−−−−
2√cos (x) + 2

sin (x)2 cos (x)


(%o6) h(x) := − 3
− −−−−−−−−−
4(cos (x) + 2) 2 2√cos (x) + 2

3sin (x)3 sin (x) 3 cos (x) sin (x)


(%o7) i(x) := − 5
+ −−−−−−−−− − 3
8(cos (x) + 2) 2 2√cos (x) + 2 4(cos (x) + 2) 2

(%i8) wxplot2d([f(x),g(x),h(x),i(x)],
[x,-%pi,%pi],
[legend,"Function","d1","d2","d3"],
[title, "Three Derivatives of A Function"]);

(%t8)

[Link] 27/08/24, 10 04 PM
Page 3 of 6
:
(%o8)

(%i9) kill(all);

(%o0) done

3.1 A Streamlined Way to Get Higher Order Derivatives

A different way to get higher order derivatives is built on exploiting a feature of


the programming language that Maxima is built on. Lisp (the programming language) allows
a user to define an array (or list of objects) based on a rule or algorithm. This is
miles away from C/C++, but it allows us a tremendous amount of flexibility. We'll start an empty list:

(%i37) f=[];

(%o37) f = []

Now, at the 0th position, we'll put the function we want to differentiate:

(%i38) f[0](x):=x*cos(exp(x+1));

(%o38) f0 (x) := x cos (exp(x + 1))

O.k., so here's the magic, we will define the nth element of the list to be the nth derivative of the
0th element!

(%i39) define(f[n](x),diff(f[0](x),x,n));

dn
(%o39) fn (x) := (x cos (%ex+1 ))
dxn

Now, let's do some differentiating!

(%i43) f[0](x);f[1](x);f[2](x);f[3](x);

(%o40) x cos (%ex+1 )

d
(%o41) (x cos (%ex+1 ))
dx

d2
(%o42) (x cos (%ex+1 ))
dx2

d3
(%o43) (x cos (%ex+1 ))
dx3

[Link] 27/08/24, 10 04 PM
Page 4 of 6
:
Wait! Stop! While symbolically that's right, that's not very useful! We want the actual expanded out expressions! Let's get one using the ev() command.
The issue is that the recursion in the definition is at the symbolic level, and we need to force
the evaluation of the diff operator!

(%i44) ev(f[1](x),diff);

(%o44) cos (%ex+1 ) − x %ex+1 sin (%ex+1 )

(%i45) ev(f[2](x),diff);

(%o45) −x %ex+1 sin (%ex+1 ) − 2%ex+1 sin (%ex+1 ) − x %e2x+2 cos (%ex+1 )

There's no reason to go in order!

(%i47) ev(f[6](x),diff);

(%o47) −15x %e5x+5 sin (%ex+1 ) − 6%e5x+5 sin (%ex+1 ) + 90x %e3x+3 sin (%ex+1 ) + 150%e3x+3 sin (%ex+1 ) − x %ex+1 sin (%ex+1 ) − 6%ex+1 sin (

We are still stuck with the issue of not having an easy to use function for, say, the 4th or 8th derivative. Here is where define() comes to the rescue!

(%i54) define(g[4](x),ev(f[4](x),diff))$
define(g[10](x),ev(f[10](x),diff))$

And now we can compute away!

(%i52) ratsimp(g[4](log(%pi)-1));

(%o52) (7π 2 − π 4 ) log (π) + π 4 + 5π 2

(%i55) ratsimp(g[10](log(%pi)-1));

(%o55) (π 10 − 750π 8 + 22827π 6 − 34105π 4 + 511π 2 ) log (π) − π 10 + 390π 8 + 3633π 6 − 43595π 4 + 2039π 2

(%i56) kill(all);

(%o0) done

4 Getting Numerical Values

What if you want a decimal approximation for your derivatives? We simply wrap the exact value in a float() command:

(%i1) f(x):=3*cos(3*x^2 - exp(sqrt(x)));

(%o1) f(x) := 3 cos (3x2 − exp(√−


x ))

(%i2) define(g(x),diff(f(x),x));

(%o2) g(x) := −3 (
%e√x
− 6x) sin (%e√x − 3x2 )
2√−
x

(%i3) g(2);

%e√2
(%o3) −3 ( 3
− 12) sin (%e√2 − 12)
22

(%i4) float(g(2));

(%o4) −31.62025556620378

[Link] 27/08/24, 10 04 PM
Page 5 of 6
:
If you are only going to be asking for one value from the derivative, then it may be faster to simply substitute it
directly as a float value:

(%i5) f(x):=(x-1)*exp(-x/2);

−x
(%o5) f(x) := (x − 1) exp( )
2

(%i7) subst(x=2, diff(f(x),x));subst(x=2.0,diff(f(x),x));

%e−1
(%o6)
2
(%o7) 0.18393972058572

If you are going to be asking for alot of decimal output from your functions, it might be a good
idea to force the float() command into the definition:

(%i8) f(x);
x
(%o8) (x − 1) %e− 2

(%i9) define(g(x),float(diff(f(x),x)));

1 0.5 (x − 1.0)
(%o9) g(x) := x − x
2.718281828459045 2 2.718281828459045 2

(%i12) g(1);g(2);g(3/2);

(%o10) 0.60653065971263

(%o11) 0.18393972058572

(%o12) 0.35427491455576

Created with wxMaxima.

[Link] 27/08/24, 10 04 PM
Page 6 of 6
:

You might also like