In [1]:
%matplotlib notebook
import matplotlib.pyplot as plt
import numpy as np
from __future__ import print_function
from ipywidgets import interact, interactive, fixed
import ipywidgets as widgets
def plotSequence(y):
n = np.linspace(0, y.size, y.size)
plt.scatter(n, y)
plt.plot([n, n], [np.zeros(n.size), y], color='gray', linestyle="--")
return
In [2]:
def fibonacci(n):
if n == 0: # first initial condition
return 0
elif n == 1: # second initial condition
return 1
else:
return fibonacci(n-1) + fibonacci(n-2)
N = 20
z = np.linspace(0,N,N)
for i in range(N):
z[i] = fibonacci(i)
plt.figure()
plotSequence(z)
In [3]:
def fibonacciC1(n):
return (1/np.sqrt(5))*pow(((1+np.sqrt(5))/2),n)
def fibonacciC2(n):
return -1*(1/np.sqrt(5))*pow(((1-np.sqrt(5))/2),n)
N = 15
x = np.linspace(0,N,N)
y = np.linspace(0,N,N)
z = np.linspace(0,N,N)
for i in range(N):
x[i] = fibonacciC1(i)
y[i] = fibonacciC2(i)
z[i] = x[i] + y[i]
plt.figure()
plotSequence(x)
plt.figure()
plotSequence(y)
plt.figure()
plotSequence(z)
In [ ]: