How can i use L'hospital rule i.e sin(x)/x for x tends to zero?

I have a simple program
n=[0:7] x=sin((pi*n)/2)./(pi*n)
which returns,
n =
0 1 2 3 4 5 6 7
x =
NaN 0.3183 0.0000 -0.1061 -0.0000 0.0637 0.0000 -0.0455
Only the problem is that,according to L'Hospital rule,value of x for n=0 should be 0.5 instead getting NaN.. How can i get this? Is there any command?

 Respuesta aceptada

Star Strider
Star Strider el 23 de Sept. de 2012
Editada: Star Strider el 23 de Sept. de 2012
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; % Derivative of a function
xderiv = @(x) ((x+eps)-x)/eps; % Derivative of an expression
for n = 0:7
x(n+1)=sin((pi*n)/2)./(pi*n);
if isnan( x(n+1) ) % Test for ‘NaN’
x(n+1) = fderiv(@sin, pi*n/2)./(xderiv(pi*n)); % L'Hospital's Rule
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,
x=sin((pi*n)/2)./(pi*n)
will be 1 at n=0, however:
x=(sin(pi*n)/2)./(pi*n)
with the fderiv call then becoming:
x(n+1) = (fderiv(@sin, pi*n)/2)./(xderiv(pi*n));
will be 0.5 for n=0.

2 comentarios

Thanks for the answer..And also for the correction...I am very happy..
It is my pleasure to help!

Iniciar sesión para comentar.

Más respuestas (1)

Wayne King
Wayne King el 23 de Sept. de 2012
Editada: Wayne King el 23 de Sept. de 2012
You can do this:
x = -5:0.001:5;
y = sin(x)./x;
plot(x,y)
Since the increment in the x-variable is 0.001 and the range is 10, the total number of x-samples is 10^4, 0 will be at index 5001. Look at the y-values in a neighborhood of 0.
y(4950:5050)
You can see that the left-hand and right-hand limits are 1.
If you have the Symbolic Toolbox.
ezplot('sin(x)/x',[-5,5])

Categorías

Más información sobre Startup and Shutdown en Centro de ayuda y File Exchange.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by