Lesson 17 of 18

L'Hôpital's Rule

L'Hôpital's Rule

When a limit has the indeterminate form 0/00/0 or /\infty/\infty, L'Hôpital's rule lets you evaluate it using derivatives:

limxaf(x)g(x)=limxaf(x)g(x)\lim_{x \to a} \frac{f(x)}{g(x)} = \lim_{x \to a} \frac{f'(x)}{g'(x)}

provided the right-hand limit exists and g(a)0g'(a) \neq 0.

Classic Examples

limx0sinxx=cos01=1\lim_{x \to 0} \frac{\sin x}{x} = \frac{\cos 0}{1} = 1

limx1x21x1=2x1x=1=2\lim_{x \to 1} \frac{x^2 - 1}{x - 1} = \frac{2x}{1}\bigg|_{x=1} = 2

limx0ex1x=e01=1\lim_{x \to 0} \frac{e^x - 1}{x} = \frac{e^0}{1} = 1

Numerical Implementation

Numerically, we approximate f(a)f'(a) and g(a)g'(a) using central differences with a small hh:

double lhopital(double (*f)(double), double (*g)(double),
                double x0, double h) {
    double df = (f(x0 + h) - f(x0 - h)) / (2.0 * h);
    double dg = (g(x0 + h) - g(x0 - h)) / (2.0 * h);
    return df / dg;
}

This avoids the f(a)/g(a)f(a)/g(a) 0/00/0 problem by working with the derivatives directly.

When to Apply It

L'Hôpital applies when you have 0/00/0 or ±/\pm\infty/\infty. It does not apply to other forms like 00 \cdot \infty or 11^\infty directly — those need algebraic manipulation first.

Your Task

Implement double lhopital(f, g, x0, h) that returns f(x0)/g(x0)f'(x_0) / g'(x_0) using central differences.

TCC compiler loading...
Loading...
Click "Run" to execute your code.