# Differentiation
This notebook will take you through mathematical differentiation, and the way that it relates the previously discussed Position, Velocity and Acceleration.


```python
from sympy import *
from sympy.plotting import plot
```

## Calculus

Differentiation is one half of the mathematical practice of calculus. This is all to do with rates of change, and areas covered. The two are fundamentally related, which will become apparent as we move through the notebook.

To start with, lets have our customary stolen graph to talk around.



Most of the above graph should be familiar. An _x_ and _y_ axis, a line on the graph, a region of space outlined by the _x_ axis, vertical lines at _a_ and _b_ and the aformentioned line. And finally an example line drawn to represent the slope.

## Part 1: Differentiations, or _Fancy Slopes_

One of the new symbols is the dy/dx part. This is basically saying, [The Rate of change of y with respect to x at that point in x]. As you can see there is a straight line which can touch a point on the line, and show the gradient of the line _at that point_. The key difference here from the gradients you've worked with before is that these can change. As you move along x, there is a different straight line which can describe the gradient at that point. 

So dy/dx is a variable with x, just like y is. You could draw out the line for dy/dx manually, if you were to put a line at every point on the graph and work out the gradient at that point. 

However that sounds like a lot of work. And we are lazy. so we can just get python to do it for us. 

Start by considering a simple example. the quadratic graph.


```python
x = symbols('x')
# This is to tell python that x is no longer just an object, or a name for something. 
# It is an algebraic symbol, like we use in equations

y = (x*x - 1) 
p1 = plot(y, ylabel = "y")
```


    <Figure size 640x480 with 1 Axes>



```python
p2 = plot(diff(y, x), ylabel = "dy/dx")
```

Hopefully you can see that there is a sector of the quadratic graph where the gradient is positive, and increasing. and another section which is negative and decreasing. Those gradients are then mirrored in the 'derivative' of the quadratic function.

### Exercise 1.1

Play around with the provided quadratic equation, to prove to yourself that the intercept term is irrelevant.

## Example 1.2: dydx as a function

In the above, we have used `diff(y,x)` as a way of saying dy/dx. This then gives us the rate of change of y with respect to x. we jumped ahead to a graph, since that made the discussion easier. But the actual output for the function is a bit simpler.


```python
diff(y, x)
```




$\displaystyle 2 x$



`diff(y,x)` actually provides us with the function to calculate the gradient at every point on the graph. In the case of `2x` then the gradient at the point `x = 1` is 2. If the gradient was `4.5x` then the gradient at the point `x = 1` is 4.5

### Exercise 1.2  Gradient of an arbitrary function. 

Take the example of the function `y = x^3 - x^2`. What does it look like. and what is the equation of its gradient


```python
## Defining the function
y_test = x*x*x - 7*x*x  
```


```python
## Plotting the function
test_plot = plot(y_test, xlim = [-5, 10], ylim = [-200, 200])
```


```python
## Calculating the derivative function
diff(y_test, x)
```




$\displaystyle 3 x^{2} - 14 x$




```python
## plotting it to prove it to ourselves
test_diff_plot = plot(diff(y_test, x), xlim = [-5, 10])
```

## Example 1.2

How does this relate to our examples before.

Critically, the x axis here does not have to be the position in x. it could also be time. 

if you had an equation. y = 2t. where y is the position along a line, and t is the time. then you have an equation which can be differentiated.


```python
t = symbols('t')
# like with x. we need to tell python that t is no longer a normal name for something
```


```python
example_y = 2*t
example_y_plot = plot(example_y)
```

In this example, we have a plot of the position. but we are able to plot the velocity. by simply differentiating the equation for position


```python
plot(diff(example_y, t), ylim = [0, 4], axis_center = (0, 0))
```

we could also try to get the acceleration by differentiating again. However....


```python
diff(example_y, t, 2)
```




$\displaystyle 0$



The acceleration here is zero. Which is OK, since we can tell that the velocity is constant, which would imply no accelerations

### Playground

Now we want to play around with plotting functions and their differentials


```python
y_test = 3*x + 20
y_plot = plot(y_test, axis_center = (0, 0))
```


```python
dydx_plot = plot(diff(y_test, x), axis_center = (0, 0))
```


```python
d2ydx2_plot = plot(diff(y_test, x, 2), axis_center = (0, 0))
```


```python

```


```python

```

125db0b39f787024c2bba529cd36ce0a54e427af
ipynb
Jupyter Notebook
tutorials/04-Differentiation.ipynb
JDOsborne1/python-orrery-lesson
edc3f538fcb33eb57dddf1eb87e9106db18eb028
MIT
tutorials/04-Differentiation.ipynb
JDOsborne1/python-orrery-lesson
edc3f538fcb33eb57dddf1eb87e9106db18eb028
MIT
2020-04-30T17:47:41.000Z
2020-04-30T19:11:31.000Z
tutorials/04-Differentiation.ipynb
JDOsborne1/python-orrery-lesson
edc3f538fcb33eb57dddf1eb87e9106db18eb028
MIT
Qwen/Qwen-72B
1. YES
2. YES