Last active
December 27, 2019 00:28
-
-
Save Gabrielcarvfer/01cd5ae0ec74439908b581432a6def6c to your computer and use it in GitHub Desktop.
Recursive tangent demonstrated by Mathologer Burkard Polster in "Pi is IRRATIONAL: animation of a gorgeous proof" https://www.youtube.com/watch?v=Lk_QF_hcM8A
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# You will need matplotlib to run this script (python -m pip install matplotlib) | |
# | |
# Recursive tangent demonstrated by Mathologer Burkard Polster in "Pi is IRRATIONAL: animation of a gorgeous proof" | |
# https://www.youtube.com/watch?v=Lk_QF_hcM8A | |
# | |
# tan(x) = x | |
# ----------------------------------------- | |
# 1 - x^2 | |
# ------------------------------------- | |
# 3 - x^2 | |
# --------------------------------- | |
# 5 - x^2 | |
# ----------------------------- | |
# 7 - x^2 | |
# ------------------------- | |
# 9 - x^2 | |
# --------------------- | |
# 11 - x^2 | |
# ---------------- | |
# 13 - x^2 | |
# ---------- | |
# 15 - x^2 | |
# ---- | |
# ... | |
PI = 3.14159265359 | |
#Defines the recursive tangent function | |
def tan_recursion(denominator, currLevel, maxLevel, squaredRadians): | |
resulting_value = denominator | |
if currLevel < maxLevel: | |
resulting_value -= squaredRadians / tan_recursion(denominator+2, currLevel+1, maxLevel, squaredRadians) | |
return resulting_value | |
#Defines the tangent function and how deep the recursion might go | |
def tan(radians, maxLevel): | |
squaredRadians = radians*radians #calculates squared radians | |
if radians == 0.0: | |
return 0.0 | |
resulting_value = radians / tan_recursion(1, 0, maxLevel, squaredRadians) | |
return resulting_value | |
#Defines the function that converts degrees into radians | |
def degree_to_rad(angle): | |
return angle*PI/180.0 | |
#Defines the main function | |
def main(): | |
#Calculates two examples of tangent values with known values | |
print("The tangent of Pi/12 (15o) is %.2f" % tan(degree_to_rad(15),10)) | |
print("The tangent of Pi/06 (30o) is %.2f" % tan(degree_to_rad(30),10)) | |
print("The tangent of Pi/04 (45o) is %.2f" % tan(degree_to_rad(45),10)) | |
print("The tangent of Pi/03 (60o) is %.2f" % tan(degree_to_rad(60),10)) | |
print("The tangent of Pi/02 (90o) is %.2f" % tan(degree_to_rad(90),10)) | |
#Create a list of values ranging from -180 to 175 degrees, with every 5th step (-180,-175,-170,...) | |
interval_to_print = list(range(-180, 180, 5)) | |
#Create two lists to store the calculated tangent values | |
standard_library_tangent_values = [] | |
recursive_tangent_tangent_values_by_depth = [[],[],[],[],[]] | |
#Imports the python standard library to calculate the tangent | |
import math | |
#For each angle in the interval_to_print list, calculate the tangent values | |
for angle in interval_to_print: | |
# Transform degrees into rads | |
rads = degree_to_rad(angle) | |
#Calculate the tangent with the standard library | |
standard_library_tangent_values.append(math.tan(rads)) | |
#Calculate the tangent with the recursive function with | |
# varying depths of recursion (1,3,5,7,9) and store on their values | |
for i in range(5): | |
recursive_tangent_tangent_values_by_depth[i].append(tan(rads, (i+1)*2-1)) | |
#Import the graph library to plot the tangent curves | |
import matplotlib.pyplot as pyplot | |
#Plot the tangent curve given by the standard library | |
pyplot.plot(interval_to_print, standard_library_tangent_values, 'green') | |
color = ["purple","blue","orange","turquoise","red"] | |
#Plot the tangent curve given by the recursive tangent functions with varying depths, each of them with a different color | |
for i in range(5): | |
pyplot.plot(interval_to_print, recursive_tangent_tangent_values_by_depth[i], color[i]) | |
#Limit the plot are to [-Pi, Pi] (X-axis) and [-10, 10] (Y-axis) | |
pyplot.axis([-180, 180, -10, 10]) | |
#Print the plotted curves (Zoom into the converging curves to spot how close they get as the depth of recursion increases) | |
pyplot.show() | |
return | |
#Calls the main function | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment