How To Do Calculus with Python: Derivatives Cheat Sheet [Part 1]
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:
- Power Rule,
- Product Rule,
- Quotient Rule,
- Chain Rule,
- 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.
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:
I would strongly recommend writing the python code in a new Jupyter Notebook, which comes with the Anaconda
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.
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:
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
Where 6x is the second order differential, or put in a hand-written way:
Let's go through a quick example by hand:
So running this in python will give you the following:
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
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.
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:
And that is pretty much it. Try to calculate by hand the following examples if you want good practice:
For more indepth explanation of the chain rule, check Aaron Schlegel's post on the Chain Rule
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:
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:
What about 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:
And taking the partial derivative in respect to y:
You can also take the derivatives with respect to many variables one after the other within the same line of code:
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:
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
, 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)
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)
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)
And with this, you now have a python library for finding derivatives and turning them into functions.
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.
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.
- Taking Derivatives In Python
- Calculating Derivatives: Problems and Solutions
- Derivatives of Exponential Functions
Subscribe to get your daily round-up of top tech stories!