# Tutorial

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


```{admonition} 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)$:


```python
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`:
```


```python
derivative = sym.diff(expression, x)
derivative
```




$\displaystyle \frac{16 a - 16 b - 64 x}{\left(b - 4 x\right)^{4}} + \frac{16 \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:


```python
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$:


```python
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:


```python
sym.factor(first_equation)
```




$\displaystyle \frac{16 \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`:


```python
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 \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$:


```python
second_equation = sym.Eq(second_derivative.subs({x: 0}), -1)
second_equation
```




$\displaystyle \frac{64 \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$:


```python
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$:


```python
second_equation = second_equation.subs({a: b / 3})
second_equation
```




$\displaystyle - \frac{192}{b^{4}} = -1$




```python
sym.solveset(second_equation, b)
```




$\displaystyle \left\{- 2 \sqrt{2} \sqrt[4]{3}, 2 \sqrt{2} \sqrt[4]{3}, - 2 \sqrt{2} \sqrt[4]{3} i, 2 \sqrt{2} \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:


```python
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} \sqrt[4]{3}}{3}\right) + \left(- 16 x + \frac{4 \sqrt{2} \sqrt[4]{3}}{3}\right) \left(- 4 x + 2 \sqrt{2} \sqrt[4]{3}\right)}{\left(- 4 x + 2 \sqrt{2} \sqrt[4]{3}\right)^{4}}$



```{attention}
We are using the `sym.root` command for the generic $n$th root.
```

We can confirm our findings:


```python
sym.diff(expression, x).subs({x: 0})
```




$\displaystyle 0$




```python
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.


```python
sym.limit(expression, x, 0)
```




$\displaystyle \frac{\sqrt{3}}{36}$




```python
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$.


```python
sym.factor(sym.integrate(expression, x))
```




$\displaystyle \frac{x \left(6 x - \sqrt{2} \sqrt[4]{3}\right)}{12 \left(4 x^{3} - 6 \sqrt{2} \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:


```python
sym.factor(sym.integrate(expression, (x, 5, 20)))
```




$\displaystyle - \frac{5 \left(- 5000 \sqrt{2} \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} \sqrt[4]{3}\right) \left(-500 - 30 \sqrt{3} + \sqrt{2} \cdot 3^{\frac{3}{4}} + 150 \sqrt{2} \sqrt[4]{3}\right)}$



9f3b2e447831e3043d3bca1d259b7ad465fe910f
ipynb
Jupyter Notebook
book/tools-for-mathematics/03-calculus/tutorial/.main.md.bcp.ipynb
daffidwilde/pfm
dcf38faccee3c212c8394c36f4c093a2916d283e
MIT
book/tools-for-mathematics/03-calculus/tutorial/.main.md.bcp.ipynb
daffidwilde/pfm
dcf38faccee3c212c8394c36f4c093a2916d283e
MIT
book/tools-for-mathematics/03-calculus/tutorial/.main.md.bcp.ipynb
daffidwilde/pfm
dcf38faccee3c212c8394c36f4c093a2916d283e
MIT
Qwen/Qwen-72B
1. YES
2. YES