# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# 1)Write a scheme to evolve a logistic map xn for a specified number n, initial conditions x0,
# and ratio r. Be sure to remap to the domain x=[0,1] if needed.
#
# 2) Starting with initial conditions x0= 0.65 in each case, compute the logistic map for n =
# [0,100] (integer values), repeating for all values r = [0.7,1.00] in increments of 0.01.
#
# 3) Record the values of xn and plot them as small red dots for each value of r. Compute the
# Lyapunov exponent λ for each map, and plot it as a blue line as a function of r.
#
# 4) For approximately what values of r does λ = 0?
#
# 5) Plot in separate panels all values of xn and λ for each value of r.
#
# 6) Extra credit:
# Produce an animation of xn and λ as a function of r, with r progressing with each frame in 0.01
# increments, and for each animation frame plot in a second panel xn vs. n.

# +

import matplotlib.pyplot as plt

import numpy as np
# %matplotlib inline
# -

# Reasons for some not great coding practice:
#
# 1. I would've done all the figures just with one for loop to generate all of them but matplotlib subplots just had them all overlapping and I didn't think it was worth it to figure out how to fix it as opposed to just plotting the figure in 30 different cells as opposed to just having one for loop and plotting it all to the same figure.
#
# 2. My lyapunov graph vs the ratio is at the very end, through the eyeball test it looks like the logistic map has a stable period roughly at .96, .89 area, and probably .74 though those all look fairly similar. According to the lyupnov graph there are quite a few places where it approaches 0, it would probably reach 0 if the step size was less than .01. There's a for loop statement at the bottom that runs through all the sign changes and points out where the lyapunov approaches 0.
#
# 3. I have a complaint about step 5, that it's kind of unclear, mianly im pretty sure that we want xn vs r and lyapunov vs r but can be read xn and lyapunov vs r which doesn't exactly make sense. Also lyapunov is stupidly hard to spell and i ended up mispelling it when I made my original function so now there's like 3 different spellings in this notebook and it's annoying me.
#
# 4. Anyway I loved this class, if Ta's are grading this you guys were awesome, if Brant is grading this, you're awesome too
#
#

# +
#Xn+1 = Xn(a-bXn)
#0 - 100 iterations


#plot all ratios 0.7 - 1.00 increments of 0.01
ratio = np.linspace(0.7, 1.0, 31)#all the ratio values I need
#print(ratio)
x_init = 0.65
future = lambda old, r : (4 * r * old) * (1 - old)
ratio_counter = 0
lyapunov_vals_to_plot = []


# +
#Lyupunov for given number of iterations:
#=1/2 * sum from i=0 to n-1 ln(4*ratio*(1-2*calculated val))
def lyupunov(map_vals, ratio):
    lyupunov_sum = []
    
    #print(len(map_vals))
    lyupunov_numpy_array = np.zeros((len(map_vals)))
    to_sum = 0
    #Make a numpy array
    #print(ratio)
    #Loop to calculate lyupunov, for each of the values 0 - 100 needs unique lyapunov

    for i in range(len(map_vals)):
        
        #Goes through each index of inputted map vals, generates a corresponding lyapunov
        to_sum = 0

        to_sum += np.log(abs(4* ratio * ( 1 - 2 * map_vals[i])))
 
        
    to_sum *= 2
    return to_sum



# + tags=[]
#Instead of plotting individually, create an array to store calculated values to plot at which point we plot them, repeat for the conditions provided
function_to_plot = []
#lyapunov_local = []
#housekeeping to plot multiple things
#iterations = np.linspace(0, 100, 101, endpoint=True)

calculated = 0

ratio_to_use = ratio[ratio_counter]
ratio_counter += 1
for i in range(101):
        #Different number of iterations
        if i == 0:
            calculated = x_init
        else:
            calculated = future(function_to_plot[i-1], ratio_to_use)
        function_to_plot.append(calculated) #Line 
        #lyapunov_local.append(lyupunov(function_to_plot, ratio_to_use))

lyupnov_val = lyupunov(function_to_plot, ratio_to_use)
lyapunov_vals_to_plot.append(lyupnov_val) 
plt.title('Ratio Used: %.2f' % ratio_to_use + ' Lyapunov val: %.2f' % lyupnov_val)
plt.plot(function_to_plot, '.r')
function_to_plot.clear()




# +
ratio_to_use = ratio[ratio_counter]
ratio_counter += 1
for i in range(101):
        #Different number of iterations
        if i == 0:
            calculated = x_init
        else:
            calculated = future(function_to_plot[i-1], ratio_to_use)
        function_to_plot.append(calculated) #Line 
        #lyapunov_local.append(lyupunov(function_to_plot, ratio_to_use))

lyupnov_val = lyupunov(function_to_plot, ratio_to_use)
lyapunov_vals_to_plot.append(lyupnov_val) 
plt.title('Ratio Used: %.2f' % ratio_to_use + ' Lyapunov val: %.2f' % lyupnov_val)
plt.plot(function_to_plot, '.r')
function_to_plot.clear()

# +
ratio_to_use = ratio[ratio_counter]
ratio_counter += 1
for i in range(101):
        #Different number of iterations
        if i == 0:
            calculated = x_init
        else:
            calculated = future(function_to_plot[i-1], ratio_to_use)
        function_to_plot.append(calculated) #Line 
        #lyapunov_local.append(lyupunov(function_to_plot, ratio_to_use))

lyupnov_val = lyupunov(function_to_plot, ratio_to_use)
lyapunov_vals_to_plot.append(lyupnov_val) 
plt.title('Ratio Used: %.2f' % ratio_to_use + ' Lyapunov val: %.2f' % lyupnov_val)
plt.plot(function_to_plot, '.r')
function_to_plot.clear()

# +
ratio_to_use = ratio[ratio_counter]
ratio_counter += 1
for i in range(101):
        #Different number of iterations
        if i == 0:
            calculated = x_init
        else:
            calculated = future(function_to_plot[i-1], ratio_to_use)
        function_to_plot.append(calculated) #Line 
        #lyapunov_local.append(lyupunov(function_to_plot, ratio_to_use))

lyupnov_val = lyupunov(function_to_plot, ratio_to_use)
lyapunov_vals_to_plot.append(lyupnov_val) 
plt.title('Ratio Used: %.2f' % ratio_to_use + ' Lyapunov val: %.2f' % lyupnov_val)
plt.plot(function_to_plot, '.r')
function_to_plot.clear()

# +
ratio_to_use = ratio[ratio_counter]
ratio_counter += 1
for i in range(101):
        #Different number of iterations
        if i == 0:
            calculated = x_init
        else:
            calculated = future(function_to_plot[i-1], ratio_to_use)
        function_to_plot.append(calculated) #Line 
        #lyapunov_local.append(lyupunov(function_to_plot, ratio_to_use))

lyupnov_val = lyupunov(function_to_plot, ratio_to_use)
lyapunov_vals_to_plot.append(lyupnov_val) 
plt.title('Ratio Used: %.2f' % ratio_to_use + ' Lyapunov val: %.2f' % lyupnov_val)
plt.plot(function_to_plot, '.r')
function_to_plot.clear()

# +
ratio_to_use = ratio[ratio_counter]
ratio_counter += 1
for i in range(101):
        #Different number of iterations
        if i == 0:
            calculated = x_init
        else:
            calculated = future(function_to_plot[i-1], ratio_to_use)
        function_to_plot.append(calculated) #Line 
        #lyapunov_local.append(lyupunov(function_to_plot, ratio_to_use))

lyupnov_val = lyupunov(function_to_plot, ratio_to_use)
lyapunov_vals_to_plot.append(lyupnov_val) 
plt.title('Ratio Used: %.2f' % ratio_to_use + ' Lyapunov val: %.2f' % lyupnov_val)
plt.plot(function_to_plot, '.r')
function_to_plot.clear()

# +
ratio_to_use = ratio[ratio_counter]
ratio_counter += 1
for i in range(101):
        #Different number of iterations
        if i == 0:
            calculated = x_init
        else:
            calculated = future(function_to_plot[i-1], ratio_to_use)
        function_to_plot.append(calculated) #Line 
        #lyapunov_local.append(lyupunov(function_to_plot, ratio_to_use))

lyupnov_val = lyupunov(function_to_plot, ratio_to_use)
lyapunov_vals_to_plot.append(lyupnov_val) 
plt.title('Ratio Used: %.2f' % ratio_to_use + ' Lyapunov val: %.2f' % lyupnov_val)
plt.plot(function_to_plot, '.r')
function_to_plot.clear()

# +
ratio_to_use = ratio[ratio_counter]
ratio_counter += 1
for i in range(101):
        #Different number of iterations
        if i == 0:
            calculated = x_init
        else:
            calculated = future(function_to_plot[i-1], ratio_to_use)
        function_to_plot.append(calculated) #Line 
        #lyapunov_local.append(lyupunov(function_to_plot, ratio_to_use))

lyupnov_val = lyupunov(function_to_plot, ratio_to_use)
lyapunov_vals_to_plot.append(lyupnov_val) 
plt.title('Ratio Used: %.2f' % ratio_to_use + ' Lyapunov val: %.2f' % lyupnov_val)
plt.plot(function_to_plot, '.r')
function_to_plot.clear()

# +
ratio_to_use = ratio[ratio_counter]
ratio_counter += 1
for i in range(101):
        #Different number of iterations
        if i == 0:
            calculated = x_init
        else:
            calculated = future(function_to_plot[i-1], ratio_to_use)
        function_to_plot.append(calculated) #Line 
        #lyapunov_local.append(lyupunov(function_to_plot, ratio_to_use))

lyupnov_val = lyupunov(function_to_plot, ratio_to_use)
lyapunov_vals_to_plot.append(lyupnov_val) 
plt.title('Ratio Used: %.2f' % ratio_to_use + ' Lyapunov val: %.2f' % lyupnov_val)
plt.plot(function_to_plot, '.r')
function_to_plot.clear()

# +
ratio_to_use = ratio[ratio_counter]
ratio_counter += 1
for i in range(101):
        #Different number of iterations
        if i == 0:
            calculated = x_init
        else:
            calculated = future(function_to_plot[i-1], ratio_to_use)
        function_to_plot.append(calculated) #Line 
        #lyapunov_local.append(lyupunov(function_to_plot, ratio_to_use))

lyupnov_val = lyupunov(function_to_plot, ratio_to_use)
lyapunov_vals_to_plot.append(lyupnov_val) 
plt.title('Ratio Used: %.2f' % ratio_to_use + ' Lyapunov val: %.2f' % lyupnov_val)
plt.plot(function_to_plot, '.r')
function_to_plot.clear()

# +
ratio_to_use = ratio[ratio_counter]
ratio_counter += 1
for i in range(101):
        #Different number of iterations
        if i == 0:
            calculated = x_init
        else:
            calculated = future(function_to_plot[i-1], ratio_to_use)
        function_to_plot.append(calculated) #Line 
        #lyapunov_local.append(lyupunov(function_to_plot, ratio_to_use))

lyupnov_val = lyupunov(function_to_plot, ratio_to_use)
lyapunov_vals_to_plot.append(lyupnov_val) 
plt.title('Ratio Used: %.2f' % ratio_to_use + ' Lyapunov val: %.2f' % lyupnov_val)
plt.plot(function_to_plot, '.r')
function_to_plot.clear()

# +
ratio_to_use = ratio[ratio_counter]
ratio_counter += 1
for i in range(101):
        #Different number of iterations
        if i == 0:
            calculated = x_init
        else:
            calculated = future(function_to_plot[i-1], ratio_to_use)
        function_to_plot.append(calculated) #Line 
        #lyapunov_local.append(lyupunov(function_to_plot, ratio_to_use))

lyupnov_val = lyupunov(function_to_plot, ratio_to_use)
lyapunov_vals_to_plot.append(lyupnov_val) 
plt.title('Ratio Used: %.2f' % ratio_to_use + ' Lyapunov val: %.2f' % lyupnov_val)
plt.plot(function_to_plot, '.r')
function_to_plot.clear()

# +
ratio_to_use = ratio[ratio_counter]
ratio_counter += 1
for i in range(101):
        #Different number of iterations
        if i == 0:
            calculated = x_init
        else:
            calculated = future(function_to_plot[i-1], ratio_to_use)
        function_to_plot.append(calculated) #Line 
        #lyapunov_local.append(lyupunov(function_to_plot, ratio_to_use))

lyupnov_val = lyupunov(function_to_plot, ratio_to_use)
lyapunov_vals_to_plot.append(lyupnov_val) 
plt.title('Ratio Used: %.2f' % ratio_to_use + ' Lyapunov val: %.2f' % lyupnov_val)
plt.plot(function_to_plot, '.r')
function_to_plot.clear()

# +
ratio_to_use = ratio[ratio_counter]
ratio_counter += 1
for i in range(101):
        #Different number of iterations
        if i == 0:
            calculated = x_init
        else:
            calculated = future(function_to_plot[i-1], ratio_to_use)
        function_to_plot.append(calculated) #Line 
        #lyapunov_local.append(lyupunov(function_to_plot, ratio_to_use))

lyupnov_val = lyupunov(function_to_plot, ratio_to_use)
lyapunov_vals_to_plot.append(lyupnov_val) 
plt.title('Ratio Used: %.2f' % ratio_to_use + ' Lyapunov val: %.2f' % lyupnov_val)
plt.plot(function_to_plot, '.r')
function_to_plot.clear()

# +
ratio_to_use = ratio[ratio_counter]
ratio_counter += 1
for i in range(101):
        #Different number of iterations
        if i == 0:
            calculated = x_init
        else:
            calculated = future(function_to_plot[i-1], ratio_to_use)
        function_to_plot.append(calculated) #Line 
        #lyapunov_local.append(lyupunov(function_to_plot, ratio_to_use))

lyupnov_val = lyupunov(function_to_plot, ratio_to_use)
lyapunov_vals_to_plot.append(lyupnov_val) 
plt.title('Ratio Used: %.2f' % ratio_to_use + ' Lyapunov val: %.2f' % lyupnov_val)
plt.plot(function_to_plot, '.r')
function_to_plot.clear()

# +
ratio_to_use = ratio[ratio_counter]
ratio_counter += 1
for i in range(101):
        #Different number of iterations
        if i == 0:
            calculated = x_init
        else:
            calculated = future(function_to_plot[i-1], ratio_to_use)
        function_to_plot.append(calculated) #Line 
        #lyapunov_local.append(lyupunov(function_to_plot, ratio_to_use))

lyupnov_val = lyupunov(function_to_plot, ratio_to_use)
lyapunov_vals_to_plot.append(lyupnov_val) 
plt.title('Ratio Used: %.2f' % ratio_to_use + ' Lyapunov val: %.2f' % lyupnov_val)
plt.plot(function_to_plot, '.r')
function_to_plot.clear()

# +
ratio_to_use = ratio[ratio_counter]
ratio_counter += 1
for i in range(101):
        #Different number of iterations
        if i == 0:
            calculated = x_init
        else:
            calculated = future(function_to_plot[i-1], ratio_to_use)
        function_to_plot.append(calculated) #Line 
        #lyapunov_local.append(lyupunov(function_to_plot, ratio_to_use))

lyupnov_val = lyupunov(function_to_plot, ratio_to_use)
lyapunov_vals_to_plot.append(lyupnov_val) 
plt.title('Ratio Used: %.2f' % ratio_to_use + ' Lyapunov val: %.2f' % lyupnov_val)
plt.plot(function_to_plot, '.r')
function_to_plot.clear()

# +
ratio_to_use = ratio[ratio_counter]
ratio_counter += 1
for i in range(101):
        #Different number of iterations
        if i == 0:
            calculated = x_init
        else:
            calculated = future(function_to_plot[i-1], ratio_to_use)
        function_to_plot.append(calculated) #Line 
        #lyapunov_local.append(lyupunov(function_to_plot, ratio_to_use))

lyupnov_val = lyupunov(function_to_plot, ratio_to_use)
lyapunov_vals_to_plot.append(lyupnov_val) 
plt.title('Ratio Used: %.2f' % ratio_to_use + ' Lyapunov val: %.2f' % lyupnov_val)
plt.plot(function_to_plot, '.r')
function_to_plot.clear()

# +
ratio_to_use = ratio[ratio_counter]
ratio_counter += 1
for i in range(101):
        #Different number of iterations
        if i == 0:
            calculated = x_init
        else:
            calculated = future(function_to_plot[i-1], ratio_to_use)
        function_to_plot.append(calculated) #Line 
        #lyapunov_local.append(lyupunov(function_to_plot, ratio_to_use))

lyupnov_val = lyupunov(function_to_plot, ratio_to_use)
lyapunov_vals_to_plot.append(lyupnov_val) 
plt.title('Ratio Used: %.2f' % ratio_to_use + ' Lyapunov val: %.2f' % lyupnov_val)
plt.plot(function_to_plot, '.r')
function_to_plot.clear()

# +
ratio_to_use = ratio[ratio_counter]
ratio_counter += 1
for i in range(101):
        #Different number of iterations
        if i == 0:
            calculated = x_init
        else:
            calculated = future(function_to_plot[i-1], ratio_to_use)
        function_to_plot.append(calculated) #Line 
        #lyapunov_local.append(lyupunov(function_to_plot, ratio_to_use))

lyupnov_val = lyupunov(function_to_plot, ratio_to_use)
lyapunov_vals_to_plot.append(lyupnov_val) 
plt.title('Ratio Used: %.2f' % ratio_to_use + ' Lyapunov val: %.2f' % lyupnov_val)
plt.plot(function_to_plot, '.r')
function_to_plot.clear()

# +
ratio_to_use = ratio[ratio_counter]
ratio_counter += 1
for i in range(101):
        #Different number of iterations
        if i == 0:
            calculated = x_init
        else:
            calculated = future(function_to_plot[i-1], ratio_to_use)
        function_to_plot.append(calculated) #Line 
        #lyapunov_local.append(lyupunov(function_to_plot, ratio_to_use))

lyupnov_val = lyupunov(function_to_plot, ratio_to_use)
lyapunov_vals_to_plot.append(lyupnov_val) 
plt.title('Ratio Used: %.2f' % ratio_to_use + ' Lyapunov val: %.2f' % lyupnov_val)
plt.plot(function_to_plot, '.r')
function_to_plot.clear()

# +
ratio_to_use = ratio[ratio_counter]
ratio_counter += 1
for i in range(101):
        #Different number of iterations
        if i == 0:
            calculated = x_init
        else:
            calculated = future(function_to_plot[i-1], ratio_to_use)
        function_to_plot.append(calculated) #Line 
        #lyapunov_local.append(lyupunov(function_to_plot, ratio_to_use))

lyupnov_val = lyupunov(function_to_plot, ratio_to_use)
lyapunov_vals_to_plot.append(lyupnov_val) 
plt.title('Ratio Used: %.2f' % ratio_to_use + ' Lyapunov val: %.2f' % lyupnov_val)
plt.plot(function_to_plot, '.r')
function_to_plot.clear()

# +
ratio_to_use = ratio[ratio_counter]
ratio_counter += 1
for i in range(101):
        #Different number of iterations
        if i == 0:
            calculated = x_init
        else:
            calculated = future(function_to_plot[i-1], ratio_to_use)
        function_to_plot.append(calculated) #Line 
        #lyapunov_local.append(lyupunov(function_to_plot, ratio_to_use))

lyupnov_val = lyupunov(function_to_plot, ratio_to_use)
lyapunov_vals_to_plot.append(lyupnov_val) 
plt.title('Ratio Used: %.2f' % ratio_to_use + ' Lyapunov val: %.2f' % lyupnov_val)
plt.plot(function_to_plot, '.r')
function_to_plot.clear()

# +
ratio_to_use = ratio[ratio_counter]
ratio_counter += 1
for i in range(101):
        #Different number of iterations
        if i == 0:
            calculated = x_init
        else:
            calculated = future(function_to_plot[i-1], ratio_to_use)
        function_to_plot.append(calculated) #Line 
        #lyapunov_local.append(lyupunov(function_to_plot, ratio_to_use))

lyupnov_val = lyupunov(function_to_plot, ratio_to_use)
lyapunov_vals_to_plot.append(lyupnov_val) 
plt.title('Ratio Used: %.2f' % ratio_to_use + ' Lyapunov val: %.2f' % lyupnov_val)
plt.plot(function_to_plot, '.r')
function_to_plot.clear()

# +
ratio_to_use = ratio[ratio_counter]
ratio_counter += 1
for i in range(101):
        #Different number of iterations
        if i == 0:
            calculated = x_init
        else:
            calculated = future(function_to_plot[i-1], ratio_to_use)
        function_to_plot.append(calculated) #Line 
        #lyapunov_local.append(lyupunov(function_to_plot, ratio_to_use))

lyupnov_val = lyupunov(function_to_plot, ratio_to_use)
lyapunov_vals_to_plot.append(lyupnov_val) 
plt.title('Ratio Used: %.2f' % ratio_to_use + ' Lyapunov val: %.2f' % lyupnov_val)
plt.plot(function_to_plot, '.r')
function_to_plot.clear()

# +
ratio_to_use = ratio[ratio_counter]
ratio_counter += 1
for i in range(101):
        #Different number of iterations
        if i == 0:
            calculated = x_init
        else:
            calculated = future(function_to_plot[i-1], ratio_to_use)
        function_to_plot.append(calculated) #Line 
        #lyapunov_local.append(lyupunov(function_to_plot, ratio_to_use))

lyupnov_val = lyupunov(function_to_plot, ratio_to_use)
lyapunov_vals_to_plot.append(lyupnov_val) 
plt.title('Ratio Used: %.2f' % ratio_to_use + ' Lyapunov val: %.2f' % lyupnov_val)
plt.plot(function_to_plot, '.r')
function_to_plot.clear()

# +
ratio_to_use = ratio[ratio_counter]
ratio_counter += 1
for i in range(101):
        #Different number of iterations
        if i == 0:
            calculated = x_init
        else:
            calculated = future(function_to_plot[i-1], ratio_to_use)
        function_to_plot.append(calculated) #Line 
        #lyapunov_local.append(lyupunov(function_to_plot, ratio_to_use))

lyupnov_val = lyupunov(function_to_plot, ratio_to_use)
lyapunov_vals_to_plot.append(lyupnov_val) 
plt.title('Ratio Used: %.2f' % ratio_to_use + ' Lyapunov val: %.2f' % lyupnov_val)
plt.plot(function_to_plot, '.r')
function_to_plot.clear()

# +
ratio_to_use = ratio[ratio_counter]
ratio_counter += 1
for i in range(101):
        #Different number of iterations
        if i == 0:
            calculated = x_init
        else:
            calculated = future(function_to_plot[i-1], ratio_to_use)
        function_to_plot.append(calculated) #Line 
        #lyapunov_local.append(lyupunov(function_to_plot, ratio_to_use))

lyupnov_val = lyupunov(function_to_plot, ratio_to_use)
lyapunov_vals_to_plot.append(lyupnov_val) 
plt.title('Ratio Used: %.2f' % ratio_to_use + ' Lyapunov val: %.2f' % lyupnov_val)
plt.plot(function_to_plot, '.r')
function_to_plot.clear()

# +
ratio_to_use = ratio[ratio_counter]
ratio_counter += 1
for i in range(101):
        #Different number of iterations
        if i == 0:
            calculated = x_init
        else:
            calculated = future(function_to_plot[i-1], ratio_to_use)
        function_to_plot.append(calculated) #Line 
        #lyapunov_local.append(lyupunov(function_to_plot, ratio_to_use))

lyupnov_val = lyupunov(function_to_plot, ratio_to_use)
lyapunov_vals_to_plot.append(lyupnov_val) 
plt.title('Ratio Used: %.2f' % ratio_to_use + ' Lyapunov val: %.2f' % lyupnov_val)
plt.plot(function_to_plot, '.r')
function_to_plot.clear()

# +
ratio_to_use = ratio[ratio_counter]
ratio_counter += 1
for i in range(101):
        #Different number of iterations
        if i == 0:
            calculated = x_init
        else:
            calculated = future(function_to_plot[i-1], ratio_to_use)
        function_to_plot.append(calculated) #Line 
        #lyapunov_local.append(lyupunov(function_to_plot, ratio_to_use))

lyupnov_val = lyupunov(function_to_plot, ratio_to_use)
lyapunov_vals_to_plot.append(lyupnov_val) 
plt.title('Ratio Used: %.2f' % ratio_to_use + ' Lyapunov val: %.2f' % lyupnov_val)
plt.plot(function_to_plot, '.r')
function_to_plot.clear()

# +
ratio_to_use = ratio[ratio_counter]
ratio_counter += 1
for i in range(101):
        #Different number of iterations
        if i == 0:
            calculated = x_init
        else:
            calculated = future(function_to_plot[i-1], ratio_to_use)
        function_to_plot.append(calculated) #Line 
        #lyapunov_local.append(lyupunov(function_to_plot, ratio_to_use))

lyupnov_val = lyupunov(function_to_plot, ratio_to_use)
lyapunov_vals_to_plot.append(lyupnov_val) 
plt.title('Ratio Used: %.2f' % ratio_to_use + ' Lyapunov val: %.2f' % lyupnov_val)
plt.plot(function_to_plot, '.r')
function_to_plot.clear()

# +
#For loop is only meant to be used after running all above cells


zero_crossings = np.where(np.diff(np.sign(lyapunov_vals_to_plot)))[0] #this line of code is stolen from a stack overflow page cause writing a for loop just wasn't worth it
for i in zero_crossings:
    print('\n\nThere was a change in sign of the lyapunov exponent between ', i, ' and ', i+1)
    print('\nThis indicates that between or at the ratio values of ', ratio[i], ' and ', ratio[i+1], ' the logistic map approached equilibrium\n')


# +
print(lyapunov_vals_to_plot)
plt.axhline(y=0.0, color = 'r')

plt.plot(ratio, lyapunov_vals_to_plot)
# -

# Extra Credit:
# Uses fmpeg, credit to peeps on stack overflow, animations in matplotlib are annoying
# https://stackoverflow.com/questions/28074461/animating-growing-line-plot-in-python-matplotlib
# Animations are below.

from matplotlib import animation, rc
from IPython.display import HTML

# +
# First set up the figure, the axis, and the plot element we want to animate
fig = plt.figure()
ax1 = fig.add_subplot(2, 1, 1)
ax2 = fig.add_subplot(2, 1, 2)

ax1.set_xlim(( .7, 1))
ax1.set_ylim((-6, 4))

ax2.set_xlim((0, 100))
ax2.set_ylim((0, 1))
# ax2.set_xlim(( .7, 1))
# ax2.set_ylim((-6, 4))

line, = ax1.plot([], [], lw=2)
line2, = ax2.plot([], [], '.r')


# -

# initialization function: plot the background of each frame
def init():
    line.set_data([], [])
    line2.set_data([], [])
    return (line, line2)


# +
# animation function. This is called sequentially
def animate(i):
    
#     x = np.linspace(0, 2, 1000)
#     y = np.sin(2 * np.pi * (x - 0.01 * i))
#     print(x, y)
#     x = ratio[i]
#     y = lyapunov_vals_to_plot[i]
#     print(x,y)
    line.set_data(ratio[:i], lyapunov_vals_to_plot[:i])
    stupid_array = []
    animated_calc = []
    for j in range(101):
        #Different number of iterations
        stupid_array.append(j)
        if j == 0:
            calc = x_init
        else:
            calc = future(animated_calc[j-1], ratio[i])
        animated_calc.append(calc) #Line 
        
#         #np.linspace(0, 100, 101)
#     line2.set_data(np.linspace(0, 100, 101) ,function_to_plot)
#     print(stupid_array)
#     print(animated_calc)
    line2.set_data(stupid_array[:], animated_calc[:])
    animated_calc.clear()
    stupid_array.clear()
    return line, line2 


# -

# call the animator. blit=True means only re-draw the parts that have changed.
anim = animation.FuncAnimation(fig, animate, init_func=init,
                               frames=31, interval=250, blit=True)


HTML(anim.to_html5_video())
#HTML(anim2.to_html5_video())



StarcoderdataJupyterScriptsDedupFiltered