By convention, 0/0 evaluates to NaN.
To be absolutely formal (if a bit kludgy) about using L’Hospital's rule in this situation:
fderiv = @(f,x) (f(x+eps)-f(x))/eps;
xderiv = @(x) ((x+eps)-x)/eps;
for n = 0:7
x(n+1)=sin((pi*n)/2)./(pi*n);
if isnan( x(n+1) )
x(n+1) = fderiv(@sin, pi*n/2)./(xderiv(pi*n));
end
end
There are better and more numerically stable derivative expressions (such as the five-point algorithm), but this will do for an illustration.
Also, using L'Hospital's rule,
will be 1 at n=0, however:
with the fderiv call then becoming:
x(n+1) = (fderiv(@sin, pi*n)/2)./(xderiv(pi*n));
will be 0.5 for n=0.
Best Answer