# Solutions

## Question 1

> 1. For each of the following functions calculate $\frac{df}{dx}$,
>    $\frac{d^2f}{dx^2}$ and $\int f(x) dx$.

> $f(x) = x$


```python
import sympy as sym

x = sym.Symbol("x")
expression = x
sym.diff(expression, x)
```




$\displaystyle 1$




```python
sym.diff(expression, x, 2)
```




$\displaystyle 0$




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




$\displaystyle \frac{x^{2}}{2}$



> $f(x) = x ^{\frac{1}{3}}$


```python
expression = x ** (sym.S(1) / 3)
sym.diff(expression, x)
```




$\displaystyle \frac{1}{3 x^{\frac{2}{3}}}$




```python
sym.diff(expression, x, 2)
```




$\displaystyle - \frac{2}{9 x^{\frac{5}{3}}}$




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




$\displaystyle \frac{3 x^{\frac{4}{3}}}{4}$



> $f(x) = 2 x (x - 3) (\sin(x) - 5)$


```python
expression = 2 * x * (x - 3) * (sym.sin(x) - 5)
sym.diff(expression, x)
```




$\displaystyle 2 x \left(x - 3\right) \cos{\left(x \right)} + 2 x \left(\sin{\left(x \right)} - 5\right) + \left(x - 3\right) \left(2 \sin{\left(x \right)} - 10\right)$




```python
sym.diff(expression, x, 2)
```




$\displaystyle 2 \left(- x \left(x - 3\right) \sin{\left(x \right)} + 2 x \cos{\left(x \right)} + 2 \left(x - 3\right) \cos{\left(x \right)} + 2 \sin{\left(x \right)} - 10\right)$




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




$\displaystyle - \frac{10 x^{3}}{3} - 2 x^{2} \cos{\left(x \right)} + 15 x^{2} + 4 x \sin{\left(x \right)} + 6 x \cos{\left(x \right)} - 6 \sin{\left(x \right)} + 4 \cos{\left(x \right)}$



> $f(x) = 3  x ^ 3 + 6 \sqrt{x} + 3$


```python
expression = 3 * x ** 3 + 6 * sym.sqrt(x) + 3
sym.diff(expression, x)
```




$\displaystyle 9 x^{2} + \frac{3}{\sqrt{x}}$




```python
sym.diff(expression, x, 2)
```




$\displaystyle 3 \left(6 x - \frac{1}{2 x^{\frac{3}{2}}}\right)$




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




$\displaystyle 4 x^{\frac{3}{2}} + \frac{3 x^{4}}{4} + 3 x$



## Question 2

> `2`. Consider the function $f(x)=2x+1$. By differentiating _from first
> principles_ show that $f'(x)=2$.

Using the definition of the derivative:


```python
h = sym.Symbol("h")
expression = 2 * x + 1
sym.limit((expression - expression.subs({x: x - h})) / h, h, 0)
```




$\displaystyle 2$



## Question 3

> `3`. Consider the second derivative $f''(x)=6x+4$ of some cubic function $f(x)$.

> `1`. Find $f'(x)$

We know the derivative will be the integral of the second derivative with a
constant:


```python
c1 = sym.Symbol("c1")

second_derivative = 6 * x + 4
derivative = sym.integrate(second_derivative, x) + c1
derivative
```




$\displaystyle c_{1} + 3 x^{2} + 4 x$



> `2`. You are given that $f(0)=10$ and $f(1)=13$, find $f(x)$.

We know that the cubic will be the integral of the derivative with constant:


```python
c2 = sym.Symbol("c2")

cubic = sym.integrate(derivative, x) + c2
cubic
```




$\displaystyle c_{1} x + c_{2} + x^{3} + 2 x^{2}$



We substitute $x=0$:


```python
cubic.subs({x: 0})
```




$\displaystyle c_{2}$



This gives $c_2=10$. We substitute that back in to our expression for the cubic:


```python
cubic = cubic.subs({c2: 10})
cubic
```




$\displaystyle c_{1} x + x^{3} + 2 x^{2} + 10$



and now substitute $x=1$:


```python
cubic.subs({x: 1})
```




$\displaystyle c_{1} + 13$



which gives $c_1=0$ which we substitute back in to our expression for the cubic:


```python
cubic = cubic.subs({c1: 0})
cubic
```




$\displaystyle x^{3} + 2 x^{2} + 10$



> `3`. Find all the stationary points of $f(x)$ and determine their nature.

The stationary points are the points that give $\frac{df}{dx}=0$:


```python
stationary_points = sym.solveset(sym.diff(cubic, x), x)
stationary_points
```




$\displaystyle \left\{- \frac{4}{3}, 0\right\}$



We determine the nature of these turning points by considering the sign of $\frac{d^2f}{dx^2}$ at each point.


```python
second_derivative.subs({x: -4 / sym.S(3)})
```




$\displaystyle -4$



This is negative, so it is a local maximum.


```python
second_derivative.subs({x: 0})
```




$\displaystyle 4$



This is positive, so it is a local minimum.

## Question 4

> `4`. Consider the function $f(x)=\frac{2}{3}x ^ 3 + b x ^ 2 + 2 x + 3$, where
> $b$ is some undetermined coefficient.

> `1`. Find $f'(x)$ and $f''(x)$


```python
b = sym.Symbol("b")
expression = sym.S(2) / 3 * x ** 3 + b * x ** 2 + 2 * x + 3
derivative = sym.diff(expression, x)
derivative
```




$\displaystyle 2 b x + 2 x^{2} + 2$




```python
second_derivative = sym.diff(expression, x, 2)
```

> `2`. You are given that $f(x)$ has a stationary point at $x=2$. Use this
> information to find $b$.

We solve the equation that arises when substituting $x=2$ in to the derivative:


```python
equation = sym.Eq(derivative.subs({x: 2}), 0)
equation
```




$\displaystyle 4 b + 10 = 0$




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




$\displaystyle \left\{- \frac{5}{2}\right\}$



> `3`. Find the coordinates of the other stationary point.

We substitute this value of $b$ in to the expression:


```python
b_value = -sym.S(5) / 2
expression = expression.subs({b: b_value})
expression
```




$\displaystyle \frac{2 x^{3}}{3} - \frac{5 x^{2}}{2} + 2 x + 3$



and the derivative and then solve the equation:


```python
derivative = derivative.subs({b: b_value})
sym.solveset(derivative)
```




$\displaystyle \left\{\frac{1}{2}, 2\right\}$



> `4`. Determine the nature of both stationary points.

Substituting both values in to the second derivative:


```python
second_derivative = second_derivative.subs({b: b_value})
second_derivative.subs({x: sym.S(1) / 2})
```




$\displaystyle -3$



This is negative so it is a local maxima.


```python
second_derivative.subs({x: 2})
```




$\displaystyle 3$



This is positive so it is a local minima.

## Question 5

> `5`. Consider the functions $f(x)=-x^2+4x+4$ and $g(x)=3x^2-2x-2$.

> `1`. Create a variable `turning_points` which has value the turning points of
> $f(x)$.


```python
f = -(x ** 2) + 4 * x + 4
derivative = sym.diff(f, x)
turning_points = sym.solveset(derivative, x)
```

> `2`. Create variable `intersection_points` which has value of the points where
> $f(x)$ and $g(x)$ intersect.


```python
g = 3 * x ** 2 - 2 * x - 2
equation = sym.Eq(f, g)
intersection_points = sym.solveset(equation, x)
intersection_points
```




$\displaystyle \left\{\frac{3}{4} - \frac{\sqrt{33}}{4}, \frac{3}{4} + \frac{\sqrt{33}}{4}\right\}$



> `3`. Using your answers to parts 2., calculate the area of the region between
> $f$ and $g$. Assign this value to a variable `area_between`.

The area between $f$ and $g$ corresponds to the integral of $\pm (f - g)$
between the points of intersection. We here use $f - g$, if the outcome was
negative we would take the opposite.


```python
area_between = sym.integrate(
    f - g, (x, sym.S(3) / 4 - sym.sqrt(33) / 4, sym.S(3) / 4 + sym.sqrt(33) / 4)
)
sym.simplify(area_between)
```




$\displaystyle \frac{11 \sqrt{33}}{4}$



c8a4b94be54c59c092791b904d2d0057710729dd
ipynb
Jupyter Notebook
book/tools-for-mathematics/03-calculus/solutions/.main.md.bcp.ipynb
11michalis11/pfm
c91b1eda70d7cde3fbe065db4667f84853947850
MIT
2020-09-24T21:02:41.000Z
2020-10-14T08:37:21.000Z
book/tools-for-mathematics/03-calculus/solutions/.main.md.bcp.ipynb
11michalis11/pfm
c91b1eda70d7cde3fbe065db4667f84853947850
MIT
2020-09-21T15:54:23.000Z
2021-12-19T23:26:15.000Z
book/tools-for-mathematics/03-calculus/solutions/.main.md.bcp.ipynb
11michalis11/pfm
c91b1eda70d7cde3fbe065db4667f84853947850
MIT
2020-10-02T09:21:27.000Z
2021-07-08T14:46:27.000Z
Qwen/Qwen-72B
1. YES
2. YES