In [12]:
import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import fsolve
%matplotlib inline
def sigmoid(x):
return 1. / (1 + np.exp(-x))
def df(x, w=0, theta=0):
return w * sigmoid(x) * (1 - sigmoid(x))
def f(x, w=0, theta=0):
return w * sigmoid(x) + theta
def g(x, w, theta):
return f(x, w, theta) - x
In [13]:
x = np.linspace(-5, 5, 100)
theta = -3.5
w = 8.
plt.plot(x, f(x, w, theta))
plt.plot(x, x)
plt.xlabel('x')
FP = []
for x_0 in range(-4, 4):
temp, _, ier, _ = fsolve(g, (x_0), args=(w, theta), full_output=True)
if ier == 1:
FP.append(temp)
FP = np.array(FP)
plt.plot(FP, f(FP, w, theta), '*')
Out[13]:
In [58]:
for theta in np.arange(-10, 0, .05):
x = np.arange(-10, 10, dtype='float')
stable = []
unstable = []
for i in range(len(x)):
temp, _, ier, _ = fsolve(g, (x[i]), args=(w, theta), full_output=True)
if ier == 1:
x[i] = temp
if abs(df(temp, w,theta)) < 1:
stable.append(temp)
else:
unstable.append(*temp)
else:
x[i] = None
plt.plot(theta * np.ones(len(stable)), stable, '.', color='green')
plt.plot(theta * np.ones(len(unstable)), unstable, '.', color='red')
In [42]:
fsolve(g, (-1), args=(w, -10))
Out[42]:
In [55]:
y = np.linspace(-10, 10, 100)
for i in np.arange(-5, -3, .5):
plt.plot(y, f(y, w, i))
plt.plot(y, y)
Out[55]:
In [62]:
x, di, ler, s = fsolve(g, (-10), args=(w, -10), full_output=True)
In [63]:
x
Out[63]:
In [64]:
ler
Out[64]:
In [66]:
plt.plot(np.arange(3), [1, 2, None])
Out[66]:
In [78]:
def l(x, r):
return x * np.exp(r* (1-x))
In [88]:
y = np.linspace(-1, 4, 100)
plt.plot(y, l(y, np.exp(1)))
plt.plot(y, y)
Out[88]:
In [97]:
Npre = 200
Nplot = 100
x = np.zeros((Nplot, 1))
for r in np.arange(1, 25, .5):
x[0] = np.random.random() * 100
for n in range(Npre):
x[0] = l(x[0], r)
for n in range(Nplot - 1):
x[n + 1] = l(x[n], r)
plt.plot(r * np.ones((Nplot, 1)), x, '.')
In [22]:
#hj = set()
hj.add(100)
In [23]:
hj
Out[23]:
In [36]:
fsolve(g, (x[0]), args=(w, theta), full_output=True)
Out[36]:
In [35]:
'nan'
Out[35]:
In [ ]: