The following is part of a on-going collection of Jupyter notebooks. The goal being to have a library of notebooks as an introduction to Mathematics and technology. These were all created by Gavin Waters. If you use these notebooks, be nice and throw up a credit somewhere on your page.
Alot of times when programing it is good to call something and pass varaible to that thing. An example in mathematics is defining a mathematical function $f(x)$
The syntax for setting up, creating, your definition is simple.
def name(variable):
do stuff
do more stuff
__return something else__
def f(x):
x +=4
return x
f(2)
6
You can also use composition of functions for this
f(f(2))
10
lets do something silly with this to illustrate the power.
def silly_sum(x,n):
x += float(1/n)
return x
y=0
for count in range(1,11):
y=silly_sum(y,count)
print(y)
1.0 1.5 1.8333333333333333 2.083333333333333 2.283333333333333 2.4499999999999997 2.5928571428571425 2.7178571428571425 2.8289682539682537 2.9289682539682538
The above code was simply $\sum_{n=1}^{10}\frac{1}{n}$. If you try this for very large numbers then it would be nice to not print all of the numbers and just the last one
y=0
for count in range(1,100001):
y=silly_sum(y,count)
print(y)
12.090146129863335
We know that this series, the harmonic series, is divergent. One of the proofs collects the terms in groups, so that they always add up to a half. Could we write a script to give the number of terms for each half? Lets do the first 7 "halfs"
y=0
half_steps = 1
sub_total = 0
n=1
total =0
while half_steps<=7:
count = 0
while sub_total<=0.5:
count +=1
sub_total = silly_sum(sub_total,n)
n+=1
total = sub_total + total
print( "half-steps #"+str(half_steps) + ' , takes '+str(count)+' terms and reaches a total of '+str(total)+'. The sub_total was : '+str(sub_total))
sub_total = 0
half_steps +=1
half-steps #1 , takes 1 terms and reaches a total of 1.0. The sub_total was : 1.0 half-steps #2 , takes 2 terms and reaches a total of 1.8333333333333333. The sub_total was : 0.8333333333333333 half-steps #3 , takes 3 terms and reaches a total of 2.45. The sub_total was : 0.6166666666666667 half-steps #4 , takes 5 terms and reaches a total of 3.019877344877345. The sub_total was : 0.5698773448773449 half-steps #5 , takes 8 terms and reaches a total of 3.547739657143682. The sub_total was : 0.5278623122663371 half-steps #6 , takes 13 terms and reaches a total of 4.05849519543652. The sub_total was : 0.5107555382928382 half-steps #7 , takes 22 terms and reaches a total of 4.575430393744269. The sub_total was : 0.5169351983077494
def f(x):
return x**2 -3*x -28
f(2)
-30
To illustrate the function in a graphical way, we will use a package called numpy and matplotlib. See Numpy and Matplotlib post to see more about these.
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
The above code loads the packages, the below code sets up a grid of numbers that we can feed into f(x). That will give us our y values and then we can plot x-y graph.
X=np.linspace(-100,100,500)
plt.plot(f(X))
[<matplotlib.lines.Line2D at 0x10e068320>]