TITLE: Computing Directional Derivatives
# ## EXERCISES

### Problem 1:

Use the definition of directional derivative to compute the
directional derivative of the function,
> with(linalg):f := (x,y) -> x/y: 'f(x,y)' = f(x,y);
f(x, y) = x/y
# at the point,
> r := vector([6,-2]):
# in the direction of the vector,
> v := vector([-1,3]):
#

### Solution 1

Let's call Dvf the directional derivative. From the definition
we have,
> Dvf := Limit('(f(r+h*v)-f(r))/h',h=0);
f(r + h v) - f(r)
Dvf := Limit -----------------
h -> 0 h
# where in our case,
> p := evalm(r+h*v): Dvf := Limit((f(p[1],p[2])-f(r[1],r[2]))/h,h=0);
6 - h
--------- + 3
- 2 + 3 h
Dvf := Limit -------------
h -> 0 h
# which simplifies to,
> Dvf := Limit(simplify((f(p[1],p[2])-f(r[1],r[2]))/h),h=0);
8
Dvf := Limit ---------
h -> 0 - 2 + 3 h
# and the directional derivative is then,
> Dvf := limit((f(p[1],p[2])-f(r[1],r[2]))/h,h=0);
Dvf := -4
# Ofcourse the formula with the grad gives the same answer,
> formula := innerprod(grad(f(x,y),[x,y]), v);
y + 3 x
formula := - -------
2
y
> answer := subs({x=6,y=-2},formula);
answer := -4
#

### Problem 2:

Find the maximum rate of change of the function,
> g := (x,y) -> sqrt(x^2+2*y): 'g(x,y)' = g(x,y);
2 1/2
g(x, y) = (x + 2 y)
# at the point r=(4,10) and the direction at which
the maximum rate of change occurs.

### Solution 2:

As we have seen before, the maximum rate of change occurs
in the direction of the gradient at the point. The gradient is,
> grad_g := grad(g(x,y),[x,y]);
x 1
grad_g := [ -------------, ------------- ]
2 1/2 2 1/2
(x + 2 y) (x + 2 y)
# which at the point of interest simplifies to,
> direction := map(simplify,subs({x=4,y=10},evalm(grad_g)));
direction := [ 2/3, 1/6 ]
# The rate of change of a function in a given direction is nothing but the
directional derivative in that direction. When the direction
is given by the grad however, then the rate becomes the
magnitud of the gradient (recall that the directional derivative
is always equal to the projection of the grad onto that direction). Thus,
> rate := sqrt((2/3)^2+(1/6)^2);
1/2
rate := 1/6 17
#

### Problem 3

Imagine your self climbing a mountain of shape,
> z := exp(-((x-1)^2+y^2)/2)+ 2*exp(-((x-2)^2+(y+2)^2)/2);
2 2 2 2
z := exp(- 1/2 (x - 1) - 1/2 y ) + 2 exp(- 1/2 (x - 2) - 1/2 (y + 2) )
> gif(plot3d(z,x=-4..4,y=-4..4,axes=frame),`mountain.gif`);
# If you are standing at the point

(2,0, exp(-1/2) + 2 exp(-2))

in which direction should you proceed initially in order to
reach the top of the mountain fastest? At what angle above the
horizontal will you be climbing?

### Solution 3:

The steepest ascend direction is given by the direction of the gradient,
> grad_z := grad(z,[x,y]):
# which at the point of interest takes the value,
> grad_z := map(simplify, subs({x=2,y=0},evalm(grad_z)));
grad_z := [ - exp(-1/2), - 4 exp(-2) ]
# Notice that this is a vector on the xy plane. To find the angle
at which you'll be climbing we need to get the direction in 3D.
But when moving on the xy plane in the grad_z direction the
hight "z" will be changing at the rate given by the directional
derivative of z in that particular direction. This is just
what we mean by directional derivative. Hence, the vector
(in 3D) showing the direction of steepest ascent at the point,
(2,0,exp(-1/2)+2exp(-2)) on the surface of the mountain is,
> u := vector([grad_z[1],grad_z[2],innerprod(grad_z,grad_z)]);
2 2
u := [ - exp(-1/2), - 4 exp(-2), exp(-1/2) + 16 exp(-2) ]
# and the angle that this vector makes with the xy plane is,
> theta := angle(u, vector([grad_z[1],grad_z[2],0]));
2 2 1/2
(exp(-1/2) + 16 exp(-2) )
theta := arccos(-----------------------------------------------------------)
2 2 2 2 2 1/2
(exp(-1/2) + 16 exp(-2) + (exp(-1/2) + 16 exp(-2) ) )
# which is approximately
> theta_approx := evalf(theta);
theta_approx := .6826030919
# or in degrees this is,
> evalf(convert(theta_approx,degrees),4);
39.12 degrees
# You should stop and reflect upon the several subtle points touched
in the solution of this problem.
>