Tutorial#

We will solve the following problem using a computer to assist with the technical aspects:

Problem

Consider the function \(f(x)= \frac{24 x \left(a - 4 x\right) + 2 \left(a - 8 x\right) \left(b - 4 x\right)}{\left(b - 4 x\right)^{4}}\)

  1. Given that \(\frac{df}{dx}|_{x=0}=0\), \(\frac{d^2f}{dx^2}|_{x=0}=-1\) and that \(b>0\) find the values of \(a\) and \(b\).

  2. For the specific values of \(a\) and \(b\) find:

    1. \(\lim_{x\to 0}f(x)\);

    2. \(\lim_{x\to \infty}f(x)\);

    3. \(\int f(x) dx\);

    4. \(\int_{5}^{20} f(x) dx\).

Sympy is once again the library we will use for this.

We will start by creating a variable expression that has the value of the expression of \(f(x)\):

import sympy as sym

x = sym.Symbol("x")
a = sym.Symbol("a")
b = sym.Symbol("b")
expression = (24 * x * (a - 4 * x) + 2 * (a - 8 * x) * (b - 4 * x)) / ((b - 4 * x) ** 4)
expression
\[\displaystyle \frac{24 x \left(a - 4 x\right) + \left(2 a - 16 x\right) \left(b - 4 x\right)}{\left(b - 4 x\right)^{4}}\]

now we can will use sympy.diff to calculate the derivative. This tool takes two inputs:

  • the first is the expression we are differentiating. Essentially this is the numerator of \(\frac{df}{dx}\).

  • the first is the variable we are differentiating for. Essentially this is the denominator of \(\frac{df}{dx}\).

Attention

We have imported import sympy as sym so we are going to write sym.diff:

derivative = sym.diff(expression, x)
derivative
\[\displaystyle \frac{16 a - 16 b - 64 x}{\left(b - 4 x\right)^{4}} + \frac{16 \cdot \left(24 x \left(a - 4 x\right) + \left(2 a - 16 x\right) \left(b - 4 x\right)\right)}{\left(b - 4 x\right)^{5}}\]

Let us factorise that to make it slightly clearer:

sym.factor(derivative)
\[\displaystyle \frac{16 \left(- 3 a b - 12 a x + b^{2} + 16 b x + 16 x^{2}\right)}{\left(- b + 4 x\right)^{5}}\]

We will now create the first equation, which is obtained by substituting \(x=0\) in to the value of the derivative and equating that to \(0\):

first_equation = sym.Eq(derivative.subs({x: 0}), 0)
first_equation
\[\displaystyle \frac{32 a}{b^{4}} + \frac{16 a - 16 b}{b^{4}} = 0\]

We will factor that equation:

sym.factor(first_equation)
\[\displaystyle \frac{16 \cdot \left(3 a - b\right)}{b^{4}} = 0\]

Now we are going to create the second equation, substituting \(x=0\) in to the value of the second derivative. We calculate the second derivative by passing a third (optional) input to sym.diff:

second_derivative = sym.diff(expression, x, 2)
second_derivative
\[\displaystyle \frac{64 \left(-1 - \frac{8 \left(- a + b + 4 x\right)}{b - 4 x} + \frac{10 \cdot \left(12 x \left(a - 4 x\right) + \left(a - 8 x\right) \left(b - 4 x\right)\right)}{\left(b - 4 x\right)^{2}}\right)}{\left(b - 4 x\right)^{4}}\]

We equate this expression to \(-1\):

second_equation = sym.Eq(second_derivative.subs({x: 0}), -1)
second_equation
\[\displaystyle \frac{64 \cdot \left(\frac{10 a}{b} - 1 - \frac{8 \left(- a + b\right)}{b}\right)}{b^{4}} = -1\]

Now to solve the first equation to obtain a value for \(a\):

sym.solveset(first_equation, a)
\[\displaystyle \left\{\frac{b}{3}\right\}\]

Now to substitute that value for \(a\) and solve the second equation for \(b\):

second_equation = second_equation.subs({a: b / 3})
second_equation
\[\displaystyle - \frac{192}{b^{4}} = -1\]
sym.solveset(second_equation, b)
\[\displaystyle \left\{- 2 \sqrt{2} \cdot \sqrt[4]{3}, 2 \sqrt{2} \cdot \sqrt[4]{3}, - 2 \sqrt{2} \cdot \sqrt[4]{3} i, 2 \sqrt{2} \cdot \sqrt[4]{3} i\right\}\]

Recalling the question we know that \(b>0\) thus: \(b = 2\sqrt{2}\sqrt[4]{3}\) and \(a=\frac{2\sqrt{2}\sqrt[4]{3}}{3}\).

We will substitute these values back and finish the question:

expression = expression.subs(
    {a: 2 * sym.sqrt(2) * sym.root(3, 4) / 3, b: 2 * sym.sqrt(2) * sym.root(3, 4)}
)
expression
\[\displaystyle \frac{24 x \left(- 4 x + \frac{2 \sqrt{2} \cdot \sqrt[4]{3}}{3}\right) + \left(- 16 x + \frac{4 \sqrt{2} \cdot \sqrt[4]{3}}{3}\right) \left(- 4 x + 2 \sqrt{2} \cdot \sqrt[4]{3}\right)}{\left(- 4 x + 2 \sqrt{2} \cdot \sqrt[4]{3}\right)^{4}}\]

Attention

We are using the sym.root command for the generic \(n\)th root.

We can confirm our findings:

sym.diff(expression, x).subs({x: 0})
\[\displaystyle 0\]
sym.diff(expression, x, 2).subs({x: 0})
\[\displaystyle -1\]

Now we will calculate the limits using sym.limit, this takes 3 inputs:

  • The expression we are taking the limit of.

  • The variable that is changing.

  • The value that the variable is tending towards.

sym.limit(expression, x, 0)
\[\displaystyle \frac{\sqrt{3}}{36}\]
sym.limit(expression, x, sym.oo)
\[\displaystyle 0\]

Now we are going to calculate the indefinite integral using sympy.integrate. This tool takes 2 inputs as:

  • the first is the expression we’re integrating. This is the \(f\) in \(\int_a^b f dx\).

  • the second is the remaining information needed to calculate the integral: \(x\).

sym.factor(sym.integrate(expression, x))
\[\displaystyle \frac{x \left(6 x - \sqrt{2} \cdot \sqrt[4]{3}\right)}{12 \cdot \left(4 x^{3} - 6 \sqrt{2} \cdot \sqrt[4]{3} x^{2} + 6 \sqrt{3} x - \sqrt{2} \cdot 3^{\frac{3}{4}}\right)}\]

If we want to calculate a definite integral then instead of passing the single variable we pass a tuple which contains the variable as well as the bounds of integration:

sym.factor(sym.integrate(expression, (x, 5, 20)))
\[\displaystyle - \frac{5 \left(- 5000 \sqrt{2} \cdot \sqrt[4]{3} - 1200 \sqrt{3} + 75 \sqrt{2} \cdot 3^{\frac{3}{4}} + 119997\right)}{2 \left(-32000 - 120 \sqrt{3} + \sqrt{2} \cdot 3^{\frac{3}{4}} + 2400 \sqrt{2} \cdot \sqrt[4]{3}\right) \left(-500 - 30 \sqrt{3} + \sqrt{2} \cdot 3^{\frac{3}{4}} + 150 \sqrt{2} \cdot \sqrt[4]{3}\right)}\]