# Limit problem calculating directional derivative

Given $f(x,y)=1-x^2-y^2$, find the directional derivative at the point $(x_0,y_0)$ in the direction of the unit vector $\vec u$.

I am experiencing some strange behavior with the Limit function. I quit the kernel and re-evaluate the notebook, but here is what happens.

Clear[f, x0, y0, a, b, u];
f[x_, y_] = 1 - x^2 - y^2;
a = Cos[Pi/3];
b = Sin[Pi/3];
u = {a, b};
{x0, y0} = .2 u;


Then, the limit definition is: $$D_uf(x_0,y_0)=\lim_{h\to 0}\frac{f(x_0+h a, y_0+h b)-f(x_0,y_0)}{h}$$ However, I am getting a strange answer.

Limit[(f[x0 + h a, y0 + h b] - f[x0, y0])/h, h -> 0]


Output:

\[Infinity]


Now, consider:

Table[(f[x0 + h a, y0 + h b] - f[x0, y0])/
h, {h, {1., .1, .01, .001, 0.0001}}]


Output:

{-1.4, -0.5, -0.41, -0.401, -0.4001}


Another definition is $D_uf(x_0,y_0)=\nabla f(x_0,y_0)\cdot \vec u$.

Grad[f[x, y], {x, y}].u;
% /. {x -> x0, y -> y0}


Output:

-0.4


See? The correct answer is $-0.4$, but the limit definition is not working for some reason? I hope this isn't a typo or some silly thing.

## 1 Answer

The problem is that you're mixing exact and machine numbers in the definition of the function f. The machine numbers create a small nonzero constant term in the numerator of the Limit, which is the cause of the infinite result as you divide by h and take h -> 0.

The fix is to use {x0,y0}= u/5 instead of 0.2.

However, if you do need to work with machine numbers, you could do this:

Needs["NumericalCalculus"]

{x0,y0}= .2u;

NLimit[(f[x0 + h a, y0 + h b] - f[x0, y0])/h, h -> 0]

(* ==> -0.4 *)


Numerical limits as done in NLimit account for the presence of the kind of roundoff errors that you're seeing.

• Also, you should use SetDelayed in your function definition: f[x_,y_] := 1 - x^2 - y^2. Commented Jul 21, 2015 at 5:53
• @MariusLadegårdMeyer That's true, unless x and y are Cleared beforehand.
– Jens
Commented Jul 21, 2015 at 5:54
• @Jens. Thanks for the quick answer. I performed each of your suggestions and both work, but this is a bizarre event. I have never experienced this before. This probably will be rare with the students, but when it happens, oh my goodness. Tough one to explain. Commented Jul 21, 2015 at 6:13
• @MariusLadegårdMeyer. I am currently writing on notebook introducing the directional derivative for my students in the fall. I think this event, though bizarre, is actually (as you say) a fortunate event, because this example is going into the notebook with an explanation to the students on how to handle the situation. Thanks for all the help. Commented Jul 21, 2015 at 6:28
• Looking at the result of Simplify[(f[x0 + h a, y0 + h b] - f[x0, y0])/h]` should be revealing. Commented Jul 21, 2015 at 14:28