Taking derivatives of functions

Mathematica

The D operator evaluates (partial) derivatives. For single derivatives, use the function and variable of interest for arguments. For multiple derivatives, the second argument becomes a list containing the variable and the number of derivatives to perform. Mixed derivatives may be performed by including more argument pairs.

In[9]:= D[ Sin[ x*y ], x ]

Out[9]= y Cos[x y]

In[10]:= D[ Sin[x*y], {x, 2} ]

            2
Out[10]= -(y  Sin[x y])

In[11]:= D[ Sin[x*y], {x, 2}, y ]

              2
Out[11]= -(x y  Cos[x y]) - 2 y Sin[x y]

Many times we are interested in the value of our derivative evaluated at some point. We can evaluate the "last answer" at given x and y coordinates using the /. operator. Simply list the variables and their values separated by right arrows.

In[12]:= % /. { x->1, y->Pi}

           2
Out[12]= Pi

Maxima

The diff operator evaluates (partial) derivatives and works for single-variable or multiple-variable expressions. For single derivatives, use the function and variable of interest for arguments. For multiple derivatives, the third argument is the number of derivatives to perform. Mixed derivatives may be performed by including more variable/number arguments.

(%i1) diff( sin(x*y), x );

(%o1)                             y COS(x y)
(%i2) diff( sin(x*y), x, 2 );

                                    2
(%o2)                            - y  SIN(x y)
(%i3) diff( sin(x*y), x, 2, y, 1 );

                                            2
(%o3)                   - 2 y SIN(x y) - x y  COS(x y)

Many times we are interested in the value of our derivative evaluated at some point. We can evaluate the "last answer" at given x and y coordinates by appending a comma and defining each variable.

(%i4) %, x=1, y=%Pi;

                                        2
(%o4)                                %PI

Axiom

The D() operator evaluates (partial) derivatives and works for single-variable or multiple-variable expressions. For single derivatives, use the function and variable of interest for arguments. For multiple derivatives, the third argument is the number of derivatives to perform. Mixed derivatives may be performed by using lists of variable/number arguments.

(2) -> D( sin(x*y), x )

   (2)  y cos(x y)
                                                     Type: Expression Integer
(3) -> D( sin(x*y), x, 2 )
(3) ->
           2
   (3)  - y sin(x y)
                                                     Type: Expression Integer
(4) -> D( sin(x*y), [x,y], [2,1] )

                           2
   (4)  - 2y sin(x y) - x y cos(x y)
                                                     Type: Expression Integer

Octave

Octave is not an algebra system, so it does not compute derivatives algebraically.

Identifying critical points

Mathematica

Although function assignment ( f[x_,y_] := …) seems the obvious way to go here, it is actually easier to just use regular assignments with the equal operater. The Solve[] function is used to find the actual critical points.

In[1]:= f = x^4 - 5*x^2 + y^2 + 3*x + 2

                     2    4    2
Out[1]= 2 + 3 x - 5 x  + x  + y

In[2]:= fx = D[f,x]; fxx = D[fx,x]

                  2
Out[2]= -10 + 12 x

In[3]:= fy = D[f,y]; fyy = D[fy,y]; fxy = D[fx,y]

Out[3]= 0

In[4]:= d = fxx*fyy - (fxy)^2

                     2
Out[4]= 2 (-10 + 12 x )

In[5]:= Solve[ {fx==0, fy==0}, {x,y} ]

Out[5]= {{y -> 0, x ->

                           1/3
       (-27 + I Sqrt[2271])                   5
>      ----------------------- + ---------------------------},
                  2/3                                    1/3
               2 3               (3 (-27 + I Sqrt[2271]))

>    {y -> 0, x ->

                                             1/3
       -((1 + I Sqrt[3]) (-27 + I Sqrt[2271])   )
>      ------------------------------------------ -
                            2/3
                         4 3

              5 (1 - I Sqrt[3])
>       -----------------------------},
                                  1/3
        2 (3 (-27 + I Sqrt[2271]))

>    {y -> 0, x ->

                                             1/3
       -((1 - I Sqrt[3]) (-27 + I Sqrt[2271])   )
>      ------------------------------------------ -
                            2/3
                         4 3

              5 (1 + I Sqrt[3])
>       -----------------------------}}
                                  1/3
        2 (3 (-27 + I Sqrt[2271]))

In[6]:= N[%]

                                            -16
Out[6]= {{y -> 0., x -> 1.40177 - 1.11022 10    I},

                                         -16
>    {y -> 0., x -> 0.312168 + 1.11022 10    I},

                                         -16
>    {y -> 0., x -> -1.71394 - 1.11022 10    I}}

To determine whether they are maxima or minima or neither, we apply the 2nd derivative test using the descriminant d defined above and the /. substitution operator. We can save ourselves some time by evaluating the derivative and the descriminant at the same time in a list.

In[7]:= {fxx,d} /. { y->0, x->1.40177}

Out[7]= {13.5795, 27.159}

In[8]:= {fxx,d} /. { y->0, x->0.312168}

Out[8]= {-8.83061, -17.6612}

In[9]:= {fxx,d} /. { y->0, x->-1.71394}

Out[9]= {25.2511, 50.5022}

We see that the first and third points mark minimums and the second is not the location of an extremum. It probably wouldn’t hurt to quickly check a plot (or contour map) to see if that really looks correct.

In[10]:= plot3d( f, [x, -2, 2], [y, -2, 2], [grid,20,20] );

Maxima

Although function assignment ( f(x,y) := …) seems the obvious way to go here, it is actually easier to just use regular assignments with the colon operater. The solve() function is used to find the actual critical points.

(C1) f : x^4 - 5*x^2 + y^2 + 3*x + 2;

                            2    4      2
(D1)                       y  + x  - 5 x  + 3 x + 2
(C2) fx : diff(f,x); fxx : diff(fx,x);

                                   3
(D2)                            4 x  - 10 x + 3
(C3)
                                      2
(D3)                              12 x  - 10
(C4) fy : diff(f,y); fyy : diff(fy,y); fxy : diff(fx,y);

(D4)                                  2 y
(C5)
(D5)                                   2
(C6)
(D6)                                   0
(C7) D : fxx*fyy - (fxy)^2;

                                       2
(D7)                            2 (12 x  - 10)
(C8) solve( [fx=0, fy=0], [x,y] );


Unexpected error. Treat results with caution.
Unexpected error. Treat results with caution.
Unexpected error. Treat results with caution.
(D8) [[x = 0.31216818857507, y = 0.0], [x = 1.401771336553945, y = 0.0],

                                            [x = - 1.713939393939394, y = 0.0]]

That output should give you pause. We should probably check that these values really look like zeros of fx and fy. We can do that via the comma operator, and we can check both derivatives at once by evaluating them in a list (marked by square brackets).

(C9) [fx,fy], x=0.31216818857507, y=0;

(D9)                     [- 2.039836646816617E-9, 0]
(C10) [fx,fy], x=1.401771336553945, y=0;

(D10)                      [1.204678101274226E-6, 0]
(C11) [fx,fy], x=-1.713939393939394, y=0;

(D11)                      [1.06675571487358E-6, 0]

To determine whether they are maxima or minima or neither, we apply the 2nd derivative test using the descriminant D defined above.

(C12) [fxx,D], x=0.31216818857507, y=0;

(D12)             [- 8.830612264501115, - 17.66122452900223]
(C13) [fxx,D], x=1.401771336553945, y=0;

(D13)                [13.5795545598108, 27.1591091196216]
(C14) [fxx,D], x=-1.713939393939394, y=0;

(D14)               [25.25105895316804, 50.50211790633608]

We see that the first point is not the location of an extremum, and the other two points mark minimums. It probably wouldn’t hurt to quickly check a plot (or contour map) to see if that really looks correct.

(C15) plot3d(f,[x,-2,2], [y,-2,2]);

Axiom

Finding critical points in Axiom is performed essentially the same way as in Mathematica and Maxima.

(1) -> f := x^4 - 5*x^2 + y^2 + 3*x + 2
(1) ->
         2    4     2
   (1)  y  + x  - 5x  + 3x + 2
                                                     Type: Polynomial Integer
(2) -> fx := D(f,x); fxx := D(fx,x)
(2) ->
           2
   (2)  12x  - 10
                                                     Type: Polynomial Integer
(3) -> fy := D(f,y); fyy := D(fy,y); fxy := D(fx, y);
(3) ->
   (3)  0
                                                     Type: Polynomial Integer
(4) -> solve( [fx=0, fy=0], 2.e-20 )
(4) ->
   (4)
   [[x= 0.3121681883 440739442,y= 0.0], [x= 1.4017712478 412977443,y= 0.0],
    [x= - 1.7139394361 853716885,y= 0.0]]
                                    Type: List List Equation Polynomial Float

Calculate the discriminant, and evaluate using the eval() command.

(5) -> D := fxx*fyy - fxy^2
(5) ->
           2
   (5)  24x  - 20
                                                     Type: Polynomial Integer
(6) -> eval([D,fxx],%%(4)(1))
(6) ->
   (6)  [- 17.6612245324 63490636,- 8.8306122662 317453178]
                                                  Type: List Polynomial Float
(7) -> eval([D,fxx],%%(4)(2))
(7) ->
   (7)  [27.1591031505 8917566,13.5795515752 9458783]
                                                  Type: List Polynomial Float
(8) -> eval([D,fxx],%%(4)(3))
(8) ->
   (8)  [50.5021213818 7431497,25.2510606909 3715749]
                                                  Type: List Polynomial Float

We see that the first point is not the location of an extremum, and the other two points mark minimums.