How To Do Calculus with Python: Derivatives Cheat Sheet [Part 1]  by@mikesell

# How To Do Calculus with Python: Derivatives Cheat Sheet [Part 1]

### @mikesellMike

This article is really a precursor to cool things you can do with calculus such as the persuit curve which is used in air-to-air missiles, and rocket launch equations.

Let's revisit some calculus topics you most likely haven't touched on in a while and use Python to take a refresher, and go over common derivatives and rules used.

This is intended to be a calculus tutorial, but you won't use an online calculator such as Wolfram-Alpha, which requires an API. Also useful if you ever want to write your own code, or just do painless calculations. This is also a great way to check that your calculations done by hand are accurate. I will go over through differentiation rules in the easiest way possible, providing examples which you can execute using python.

The areas covered in this how to install SymPy, and go through the following:

1. Power Rule,
2. Product Rule,
3. Quotient Rule,
4. Chain Rule,
5. Exponential,
6. Partial Derivatives

I will use Lagrange's derivative notation (such as 𝑓(𝑥), 𝑓(𝑥), and so on) to express formulae as it is the easiest notation to understand whilst you code along with python. Whilst it is more common to use the Leibniz notation, (d/dx), it didn't feel natural when running differential equations in Python.

## Using SymPy

This post will use SymPy, which is a Python library. It is also possible to use the SciPy library, but SymPy prints the output in an easy to read way, and is more useful in getting a grasp of differentiation and integration.

To install it, open up the terminal and run the following command:

``pip install sympy``

I would strongly recommend writing the python code in a new Jupyter Notebook, which comes with the Anaconda distribution.

## Differentiation Rules

I will go through some differentiation rules first, as a quick refresher to some Calculus topics that you probably have forgotten a long time ago.

## Power Rule

The power rule states:

Which is self-explanatory if you have ever taken a calculus class. So let's take a simple example, and go through the steps by hand;

This is a good, easy example to test out with the SymPy library. To implement this in python, first import the library, and declare a variable that you will use within your functions. The snippet below shows how to declare a single variable function:

``````import sympy as sp

x = sp.Symbol('x')``````

The final step is to get the derivation by running the code below:

``sp.diff(x**3)``

Which outputs:

You will notice that the output equation comes out in a nice print format. If you want to get the second order differential, f''(x), you can simply include x twice within the

``diff``
command:

``sp.diff(x**3,x,x)``

Where 6x is the second order differential, or put in a hand-written way:

## Product Rule

Let's go through a quick example by hand:

So running this in python will give you the following:

``sp.diff(sp.sin(x)*(2*x**2+2))``

## Quotient Rule

The quotient rule helps allows us to efficiently find the derivative of one function divided by another function, such that:

So, let's go through a quick example by hand:

and this is what it looks like when we do this in Python:

``sp.diff((sp.sin(x))/x)``

Try this by hand, and then run it in python:

The quotient rule is very similar to the product rule, except with changing the plus to a minus, and the extra step of dividing by the g(x)^2 step.

## Chain Rule

As mentioned earlier, I have chosen to use Langrangian notation to go through these rules. The chain rule for some reason gets more over-complicated than it needs to be. The formula is shown below:

So let's go through an example by hand:

The inner function, g(𝑥) is 𝑥^2 +1 which when differentiated is 2𝑥. The outer function is f(𝑥) is (stuff)^7, which when differentiated turns to 7*(stuff)^6.

So following this logic, and here is the hand calculation:

Which is pretty straghtforward when using the chain rule formula. This is what this looks like when run in python:

``sp.diff((x**2+1)**7)``

And that is pretty much it. Try to calculate by hand the following examples if you want good practice:

and,

For more indepth explanation of the chain rule, check Aaron Schlegel's post on the Chain Rule.

## Exponential Derivatives

and, using the chain rule, while giving e an exponent of g(x),

Let's go through a quick example:

and the code for that will look like:

``sp.diff(sp.exp(3*x))``

This covers all the differentiaiton rules you would have memorised in your calculus class. With all that, let's look at something called Partial Derivatives:

A partial differential equation (PDE) differs from the Ordinary Differential equations we have looked at so far, as PDEs contain multivariable functions. The difference being is that you take derivatives of one variable at a time.

Let's start with a two variable function and find their partial derivatives:

Now how do I do this in Python you might ask? The first step is to declare your variables 𝑥 and 𝑦 like so:

``````x, y = sp.symbols('x y')

f = x**4 * y``````

In the above, I have assigned the function as a variable, so as not to rewrite this variable every time I take a derivative.

Taking the partial derivative in respect to x:

``sp.diff(f, x)``

And taking the partial derivative in respect to y:

``sp.diff(f, y)``

You can also take the derivatives with respect to many variables one after the other within the same line of code:

``sp.diff(f,x,y)``

## Another example with 3 variable functions

``````x, y, z = sp.symbols('x y z')

f = x**3 * y * z**2``````

The remaining part now is as before:

``sp.diff(f, x)``
``sp.diff(f, y)``
``sp.diff(f, z)``

And that's pretty much it on the derivatives side. However, these aren't actual functions useful in Python code, this is more of a homework thing.

## Actually putting this in Python Code!

What is the point of calculating differential equations in my Python code if I can't plug in the numbers? SymPy gives us a function to do just that!

This very handy function is called

``lambdify``
, where you substitute numbers where you have placed symbols.

Let's do a quick example:

``````f = 2*x**3+4*x
f_prime = sp.diff(f)
f_prime``````

So I have set up f_prime, but I want to substitute 𝑥 with the number 2. We now use our handy little function here:

``````f_prime = sp.lambdify(x, f_prime)
f_prime(2)``````

>> 28

And there you have it. Let's do another example with multiple symbolic variables

``````f = x**3 * y * z**2

F = sp.lambdify([x,y,z], f)

F(1,2,3)``````

>> 18

And with this, you now have a python library for finding derivatives and turning them into functions.

Have fun!

## Future Articles

Integrals deserve an article of their own, and will be the part 2, followed by Series Expansion and SymPy plots.

After that, I will only do articles on the applications of calculus such as the persuit curve, rocket launch equations, orbital mechanics, or anything useful for Kerbal Space Program's kOS mod, which allows you to automate specific tasks. This is probably the easiest way to immediately apply calculus functions with Python.

## Referenced Websites

These are the the websites which I have heavily referenced to make this revision cheat sheet. I would recommend visiting these sites, especially Taking Derivatives In Python which goes through the same rules.