# Implementing L'Hospital Rule?

**URL:** <https://community.wolfram.com/t/implementing-lhospital-rule/20627>\
**Category:** Questions\
**Tags:** calculus\
**Created:** [August 28, 2022, 11:16pm UTC](https://community.wolfram.com/t/implementing-lhospital-rule/20627 "2022-08-28T23:16:39Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![rainierme](https://avatars.discourse-cdn.com/v4/letter/r/77aa72/32.png) [@rainierme](https://community.wolfram.com/u/rainierme)\
**Post date:** [August 28, 2022, 11:16pm UTC](https://community.wolfram.com/t/implementing-lhospital-rule/20627/1 "2022-08-28T23:16:39Z")

</div>

I’ve been trying to implement the function for applying the L’Hospital Rule after the test conditions have been satisfied. One would think just this would be enough to cut the mustard:

```
lhospital = D[Numerator[#], #2]/ D[Denominator[#], #2] &

```

But, there seems to be simplification & substitution going on in the engine for this to work. I’ve posted the answer to some other site, & somebody came up with another snippet:

```
lhospital = 
  Function[{f, x}, 
   Divide @@ D[NumeratorDenominator[Unevaluated[f]], x], HoldFirst];

```

Though some cases still seep through (say, trigonometric ones). He then added this command for the engine:

```
SetSystemOptions["SimplificationOptions" -> {"AutosimplifyTrigs" -> False}];

```

So now it works for the majority of examples I posed, yet the function has stopped working to what `lhospital = D[Numerator[#], #2]/ D[Denominator[#], #2] &  
used to work in. Particularly these bunch of fractional terms I am trying to tame:

```
-((ao^3 (r2 r3 rb rc + 
        r1 (r4 ra rc + r3 rb rc + 
           r3 ra (rb + rc))) rgsink vref + (r1 + r2) (r3 + 
        r4) (ra rc rgsink + rb rc rgsink + ra rb (rc + rgsink)) vth + 
     ao (r2 (r4 ra rb rgsink vref + r3 rb rc rgsink vth + 
           r3 ra (rb rc vth + rc rgsink vth + 
              rb rgsink (vref + vth))) + 
        r1 (2 r3 rb rc rgsink vth + r4 rb rc rgsink vth + 
           r4 ra (rb rc vth + rc rgsink vth + 
              rb rgsink (vref + vth)) + 
           r3 ra (2 rb rc vth + 2 rc rgsink vth + 
              rb rgsink (vref + 2 vth)))) + 
     ao^2 (r2 (r3 rb rc + r4 (ra + rb) rc + 
           r3 ra (rb + rc)) rgsink vref + 
        r1 (r4 (rb rc + ra (rb + rc)) rgsink vref + 
           r3 (ra rc rgsink (vref + vth) + rb rc rgsink (vref + vth) +
               ra rb (rc vth + 
                 rgsink (2 vref + vth))))))/(r2 r4 (-((-1 + 
             ao^2) ra rc rgsink) - (-1 + ao^2) rb rc rgsink + 
        ra rb (rc + rgsink - ao rgsink)) + 
     r2 r3 ((1 + ao - ao^2) ra rc rgsink - (-1 + ao) (1 + 
           ao)^2 rb rc rgsink + (1 + ao) ra rb (rc + rgsink - 
           ao rgsink)) + (1 + 
        ao) r1 r3 ((1 + ao - ao^2) ra rc rgsink + (1 + ao - 
           ao^2) rb rc rgsink + (1 + ao) ra rb (rc + rgsink - 
           ao rgsink)) + 
     r1 r4 ((1 + ao - ao^2) rb rc rgsink + (1 + 
           ao) ra (-((-1 + ao^2) rc rgsink) + 
           rb (rc + rgsink - ao rgsink)))))

```

---

<div class="post-metadata">

**Author:** ![danl](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/danl/32/41267_2.png) [@danl](https://community.wolfram.com/u/danl)\
**Post date:** [August 30, 2022, 2:40pm UTC](https://community.wolfram.com/t/implementing-lhospital-rule/20627/2 "2022-08-30T14:40:34Z")

</div>

What code did you apply to this expression? In particular, what is the second argument (the variable of interest)?

---

<div class="post-metadata">

**Author:** ![rainierme](https://avatars.discourse-cdn.com/v4/letter/r/77aa72/32.png) [@rainierme](https://community.wolfram.com/u/rainierme)\
**Post date:** [August 30, 2022, 10:04pm UTC](https://community.wolfram.com/t/implementing-lhospital-rule/20627/3 "2022-08-30T22:04:00Z")

</div>

Its to handle expressions such as this, in respect to variable `ao`:

```
solu7 = -((ao^3 (r2 r3 rb rc + 
r1 (r4 ra rc + r3 rb rc + 
r3 ra (rb + rc))) rgsink vref + (r1 + r2) (r3 + 
r4) (ra rc rgsink + rb rc rgsink + ra rb (rc + rgsink)) vth + 
ao (r2 (r4 ra rb rgsink vref + r3 rb rc rgsink vth + 
r3 ra (rb rc vth + rc rgsink vth + 
rb rgsink (vref + vth))) + 
r1 (2 r3 rb rc rgsink vth + r4 rb rc rgsink vth + 
r4 ra (rb rc vth + rc rgsink vth + 
rb rgsink (vref + vth)) + 
r3 ra (2 rb rc vth + 2 rc rgsink vth + 
rb rgsink (vref + 2 vth)))) + 
ao^2 (r2 (r3 rb rc + r4 (ra + rb) rc + 
r3 ra (rb + rc)) rgsink vref + 
r1 (r4 (rb rc + ra (rb + rc)) rgsink vref + 
r3 (ra rc rgsink (vref + vth) + rb rc rgsink (vref + vth) +
ra rb (rc vth + 
rgsink (2 vref + vth))))))/(r2 r4 (-((-1 + 
ao^2) ra rc rgsink) - (-1 + ao^2) rb rc rgsink + 
ra rb (rc + rgsink - ao rgsink)) + 
r2 r3 ((1 + ao - ao^2) ra rc rgsink - (-1 + ao) (1 + 
ao)^2 rb rc rgsink + (1 + ao) ra rb (rc + rgsink - 
ao rgsink)) + (1 + 
ao) r1 r3 ((1 + ao - ao^2) ra rc rgsink + (1 + ao - 
ao^2) rb rc rgsink + (1 + ao) ra rb (rc + rgsink - 
ao rgsink)) + 
r1 r4 ((1 + ao - ao^2) rb rc rgsink + (1 + 
ao) ra (-((-1 + ao^2) rc rgsink) + 
rb (rc + rgsink - ao rgsink)))))

```

The funny thing is with the function definition I’m working with:

```
lhospital = 
Function[{f, x}, 
Switch[Head[Unevaluated[f]], Times, 
Divide @@ D[NumeratorDenominator[Unevaluated[f]], x]], HoldAll]

```

(I used Switch, so I can keep adding more cases as more heads needs to be handled or extended to)

This works:

```
lhospital[-((ao^3 (r2 r3 rb rc + 
r1 (r4 ra rc + r3 rb rc + 
r3 ra (rb + rc))) rgsink vref + (r1 + r2) (r3 + 
r4) (ra rc rgsink + rb rc rgsink + ra rb (rc + rgsink)) vth + 
ao (r2 (r4 ra rb rgsink vref + r3 rb rc rgsink vth + 
r3 ra (rb rc vth + rc rgsink vth + 
rb rgsink (vref + vth))) + 
r1 (2 r3 rb rc rgsink vth + r4 rb rc rgsink vth + 
r4 ra (rb rc vth + rc rgsink vth + 
rb rgsink (vref + vth)) + 
r3 ra (2 rb rc vth + 2 rc rgsink vth + 
rb rgsink (vref + 2 vth)))) + 
ao^2 (r2 (r3 rb rc + r4 (ra + rb) rc + 
r3 ra (rb + rc)) rgsink vref + 
r1 (r4 (rb rc + ra (rb + rc)) rgsink vref + 
r3 (ra rc rgsink (vref + vth) + rb rc rgsink (vref + vth) +
ra rb (rc vth + 
rgsink (2 vref + vth))))))/(r2 r4 (-((-1 + 
ao^2) ra rc rgsink) - (-1 + ao^2) rb rc rgsink + 
ra rb (rc + rgsink - ao rgsink)) + 
r2 r3 ((1 + ao - ao^2) ra rc rgsink - (-1 + ao) (1 + 
ao)^2 rb rc rgsink + (1 + ao) ra rb (rc + rgsink - 
ao rgsink)) + (1 + 
ao) r1 r3 ((1 + ao - ao^2) ra rc rgsink + (1 + ao - 
ao^2) rb rc rgsink + (1 + ao) ra rb (rc + rgsink - 
ao rgsink)) + 
r1 r4 ((1 + ao - ao^2) rb rc rgsink + (1 + 
ao) ra (-((-1 + ao^2) rc rgsink) + 
rb (rc + rgsink - ao rgsink))))), ao]

```

But this doesn’t:

```
lhospital[solu7, ao]

Power::infy: Infinite expression 1/0 encountered.
ComplexInfinity

```

When numerator & denominator both still have `ao` as polynomials (therefore going to infinity).
