# 06 Calculus with Sympy and Numpy  
# Review Exercises - Solutions


## Review Exercise 1 : Differentiation

$$f(x) = cos(x) + bx + c$$

Use symbolic math to find the second derivative of $f(x)$


```python

```


```python
# Review Exercise 1
# Example Solution
import sympy as sp
from sympy import diff, symbols

b, c, x = symbols('b c x')

f = sp.cos(x) + b*x + c

print(diff( f, x, 2 ) )
```

    -cos(x)


## Review Exercise 2 : Integration
$$f(x) = cos(x) + bx + c$$

Find: $\int_{0}^{1} f(x) \; dx$:

1. Symbolically
2. Non-symbolically (where $b=2$, $c=5$)


```python

```


```python
# Review Exercise 2 : Symbolically
# Example Solution 

from sympy import integrate
from sympy import pprint

b, c, x = symbols('b, c, x')
f = sp.cos(x) + b*x + c
pprint(integrate(f, (x, 0, 1)))
```

    b             
    ─ + c + sin(1)
    2             



```python

```


```python
# Review Exercise 2 : Non Symbolically
# Example Solution 
import numpy as np
from scipy.integrate import quad

# first create a function to describe the integrand
def integrand(x):
    return np.cos(x) + 2*x + 5


# create two variables to store the integral and the error
ans, err = quad(integrand, 0, 1)


print(ans)
print(f'The estimated error is {err}')
```

    6.841470984807897
    The estimated error is 7.595558609638671e-14



```python

```

## Review Exercise 3 : Estimating the integral of exerimental data

Use the trapezoid method to integrate the array of raw experimental `y` data with respect to indpendent variable `x`. 

```
x = np.array([0, 10, 15, 30, 35, 50, 75, 80])

y = np.array([5, 6, 1, 1.5, 2, 5, 6, 1])
```


```python
# Review Exercise 3
```


```python
# Review Exercise 3
# Example Solution

x = np.array([0, 10, 15, 30, 35, 50, 75, 80])

y = np.array([5, 6, 1, 1.5, 2, 5, 6, 1])

print(np.trapz(y, x))
```

    307.5


## Review Exercise 4: Solving ordinary differntial equations. 

During braking, a car’s velocity is given by $v = v_{0} e^{−t/R}$.

$v = \frac{dD}{dt}$

Initial condition: distance, $D(0) = 0$

Solve the equtaion using symbolic mathematics to show that the distance travelled while braking as a function of time can be expressed as:

$$D(t) =  Rv_0 - R v_0 e^{−t/R} $$

a) Define variables as symbols sybolically. Define function `D` (distance).

b) Use `Eq` from the `sympy` package to define the differential equation. 

c) Use `dsolve` to find the general solution to the equation by solving the equation symbolically. 

d) Find the value of the constant of ingtegration $C1$. 

e) Substitute $C1$ into the general solution to find the exact solution to the equation.  


```python
# Review Exercise 4
# Example Solution
import sympy as sp
from sympy import symbols, Eq, Function, dsolve, exp, solve
from IPython.display import display

# a) define symbols
t, v0, R, C1 = symbols("t v0 R C1")
D = Function("D")

# b)define differential equation
eqn = Eq(D(t).diff(t), v0*exp(-t/(R)))
display(eqn)

# c) find general solution
gen_sol = dsolve(eqn, D(t))
display(gen_sol)

# d) find C1
cnts = gen_sol.subs([(t,    0),       # x = 0 --> subs 0 for x
                     (D(0), 0)])      # f(0) = 0 --> subs 0 for f(0)

display(cnts)

C_1 = solve([cnts], (C1))

display(C_1)

# e) Substitute C1 into the general solution to find exact solution
ode_sol = gen_sol.subs(C_1)
display(ode_sol)
```


$\displaystyle \frac{d}{d t} D{\left(t \right)} = v_{0} e^{- \frac{t}{R}}$



$\displaystyle D{\left(t \right)} = C_{1} - R v_{0} e^{- \frac{t}{R}}$



$\displaystyle 0 = C_{1} - R v_{0}$



    {C1: R*v0}



$\displaystyle D{\left(t \right)} = R v_{0} - R v_{0} e^{- \frac{t}{R}}$



```python
# Review Exercise 4
```

## Review Exercise 5 : Solving ordinary differntial equations. 

$\frac{dy}{dx} + y = x$

Initial value: $y(0) = 1$

Find the numerical solution for $y$ using `odeint` for the range $x = [0, 5]$




```python
# Review Exercise 5
```


```python
# Review Exercise 5
# Example solution
import numpy as np
import matplotlib.pyplot as plt
from scipy.integrate import odeint
%matplotlib inline

xs = np.linspace(0,5,100)
y0 = 1.0  # initial condition

# Define a function which calculates the derivative
def dy_dx(y, x):
    return x - y

y = odeint(dy_dx, y0, xs)
```


```python

```

e44d6288c8f4121ac5ba135f54bb806ad97195d5
ipynb
Jupyter Notebook
ReviewQuestions_ExampleSolutions/06_Calculus_SympyNumpy__ClassMaterial.ipynb
hphilamore/UoB_PythonForEngineers_2020
27eb7e07edecd2003d4672c83ebc6c355d92b46b
MIT
ReviewQuestions_ExampleSolutions/06_Calculus_SympyNumpy__ClassMaterial.ipynb
hphilamore/UoB_PythonForEngineers_2020
27eb7e07edecd2003d4672c83ebc6c355d92b46b
MIT
ReviewQuestions_ExampleSolutions/06_Calculus_SympyNumpy__ClassMaterial.ipynb
hphilamore/UoB_PythonForEngineers_2020
27eb7e07edecd2003d4672c83ebc6c355d92b46b
MIT
Qwen/Qwen-72B
1. YES
2. YES