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]



Now, consider:

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


{-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}



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.


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:


{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.

  • 1
    $\begingroup$ Also, you should use SetDelayed in your function definition: f[x_,y_] := 1 - x^2 - y^2. $\endgroup$ Jul 21 '15 at 5:53
  • $\begingroup$ @MariusLadegårdMeyer That's true, unless x and y are Cleared beforehand. $\endgroup$
    – Jens
    Jul 21 '15 at 5:54
  • $\begingroup$ @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. $\endgroup$
    – David
    Jul 21 '15 at 6:13
  • 1
    $\begingroup$ @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. $\endgroup$
    – David
    Jul 21 '15 at 6:28
  • 1
    $\begingroup$ Looking at the result of Simplify[(f[x0 + h a, y0 + h b] - f[x0, y0])/h] should be revealing. $\endgroup$
    – george2079
    Jul 21 '15 at 14:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.