Math Codified - Limits

Prerequisites

  • I assume you have a basic understanding of python and math.
  • I will be using python 3.6 and above (sorry no legacy code here).
  • I recommend writing the code yourself (in a jupyter notebook perhaps) and to play around with it.
  • Being prepared for bad jokes

Basics

Limits are a concept in math used in various situations, for example when calculating derivatives. I have a article about how to understand derivatives using python here.

Limits are used when a function isn’t defined for a certain input or if we want to see if the function has a limit at or ie if the function converges at infinity (functions can also converge to other functions but that is a matter for another day). That a function converges means that there is a number that the function approaches at , an invisible line it never crosses. Often it can be seen as a “ceiling” or a “floor” like in this picture:

Floor function

A limit is written as this:

stands for limes in latin but I always think of it as standing for the english word limit, so if you hear someone talk about limes they are probably talking about limits. is the function we want to get the limit of and is the limit we want to approach. We write to say “as approaches ”. The whole expression if spoken would be The limit of as approaches .

Can we with that in mind evaluate some limits of the function in the picture above, ? Let’s try!

First let’s explore what happens when becomes really big, namely when approaches infinity. We would write the expression like this:

If we study the graph we can see that the function never seems to go lower than the red-dashed line at , in other words the limit as x approaches is 2 or as a math expression:

When you just look at the equation that makes alot of sense as well, becomes really small when is large so it basically becomes at .

A quick note about when to use limits and when not to. If you have a well behaved function (defined everywhere basically) then the limit is just the function evaluated at that point:

It is just when a function isn’t defined for certain (or if you want to see how it behaves at ) that limits are useful. We will show this in an code example below.

Exercise

That wasn’t too hard, right? Here comes an exercise for you to test yourself (the answer is at the bottom of this page):

Write a mathematical expression of the limit of as approaches and write a reasonable answer to that limit.

Code Time!

Now comes the juicy part you came here for! Put on your coding gear, here we GO! (don’t tell me you haven’t got any coding gear, let’s see… I know, take that old hoodie and… a pair of glasses! That looks rather code-ish, right?)

The way we are going to do limits in python is when is a number we will add a small number to it and evaluate an approximation of the value like this:

# our function. is the same as above
def f(x):
    return 1/x + 2

# the small number we will add to x. 
h = 1e-3

# The limit as x goes to 0
x = 0 + h
limit = f(x)
print(limit)
# 1002.0

Note: 1e-3 is scientfic notation for and likewise is 7e3 the same as .

If we test with a smaller , for example 1e-6, we get 1000002. This is a strong indication that it tends to infinity and if we choose even smaller values for the limits get’s higher and higher. We can modify the code to write the answer for multiple values of and that could help us see a trend:

def f(x):
    return 1/x + 2

x = 0

# a list of the h's we want to test
h_list = [1e-3, 1e-6, 1e-9, 1e-12]

# loop over the h's and evaluate the function at each
for h in h_list:
    limit = f(x + h)
    print(f"h: {h}, limit: {limit}")
#h: 0.001, limit: 1002.0
#h: 1e-06, limit: 1000002.0
#h: 1e-09, limit: 1000000001.9999999
#h: 1e-12, limit: 1000000000002.0

This is the structure we will be using now when we want to see how a function behaves as it approaches infinity. We will have a list of increasingly big numbers (instead of small):

def f(x):
    return 1/x + 2

# list of big numbers
x_list = [1e3, 1e6, 1e9, 1e12]

# loop over the x's and evaluate the function at each
for x in x_list:
    limit = f(x)
    print(f"x: {x}, limit: {limit}")
#x: 1000.0, limit: 2.001
#x: 1000000.0, limit: 2.000001
#x: 1000000000.0, limit: 2.000000001
#x: 1000000000000.0, limit: 2.000000000001

As we can see it seems to converge to 2 at infinity. If you continue to increase you will soon surpass the precision of floating point numbers (that is shown) and it will just display “2.0”. Python is really convinient in the way that you can have really big numbers without worrying about what whether it should be an unsigned, long etc int. It just works! Test to print some really big number, preferably using the notation I have used above if you don’t want to hold the 0-button until the heat-death of the universe. It will handle quite big numbers, numbers to0 big for us to even imagine. 1e100, also called a googol would need to divided by 2 one hundred times just to get down to . Think about how big is, even so it is just % of a googol. A googol was probably a piece of cake for your computer, then I have a challenge for it: beware! The googolplex ! (the space after the word is to keep the size of the number down, in fact it would be so ridiculously huge that I don’t know how to write it in any other way then that). A googolplex is 10 to the power of a googol, . Seeing as my computer can’t handle numbers larger than 1e300 I have a hard time seeing a computer being able to handle a googolplex without cheating. For anyone interested, a googol is about a googol-th (%) of a percent of a googolplex.

If you are reading this at night I apologize for the last paragraph, it might have been a bit to math heavy for this article about codifying math. But I just think that it is remarkable that we can express really big numbers even though we can’t really comprehend them.

That was it for this time, I hope you learnt at least something new and that I haven’t scared you away from math. I would really like to discuss math and such with you so if you are in the mood you can find me on twitter and on my email which both can be found in the footer.

Have a nice day!

Answer to Exercise

An important note here is differentiate between and . The first one is true but the second one is not, this is correct then: is undefined.

Questions & Contact

If you got any questions comment them below, send me an email or contact me on twitter. The links are in the footer below. You can also contact me there casually just to discuss what’s at the limit of your imagination right now.

Written on July 12, 2018