f_cub = lambda x: x**3 - x
x_arr2 = np.linspace(-1.6, 1.6, 400)
f_arr2 = f_cub(x_arr2)
fig, axes = plt.subplots(1, 3, figsize=(12, 4.5))
# f(x) graph
ax = axes[0]
ax.plot(x_arr2, f_arr2, color='steelblue', lw=2.5)
ax.axhline(0, color='k', lw=0.8)
ax.fill_between(x_arr2, 0, f_arr2, where=(f_arr2 > 0),
alpha=0.15, color='crimson', label='$f>0$')
ax.fill_between(x_arr2, 0, f_arr2, where=(f_arr2 < 0),
alpha=0.15, color='steelblue', label='$f<0$')
for xe in [-1, 0, 1]:
ax.plot(xe, 0, 'ko', markersize=8, zorder=5)
ax.set_xlabel('$x$'); ax.set_ylabel('$f(x)$')
ax.set_title(r"$f(x)=x^3-x$"); ax.legend(fontsize=8)
# Phase line
ax2 = axes[1]
ax2.axhline(0, color='k', lw=1.2)
stab_colors = ['crimson', 'seagreen', 'crimson']
for xe, sc in zip([-1, 0, 1], stab_colors):
ax2.plot(xe, 0, 'o', markersize=12, color=sc, zorder=5)
arrow_cfg = [(-1.35, 'crimson', '->'), (-0.5, 'steelblue', '<-'),
(0.5, 'steelblue', '->'), (1.35, 'crimson', '<-')]
arrow_cfg = [(-1.35,'crimson','<-'),(-0.5,'steelblue','->'),(0.5,'steelblue','<-'),(1.35,'crimson','->')]
for xm, color, style in arrow_cfg:
ax2.annotate('', xy=(xm+0.2*(-1 if '<-' in style else 1),0), xytext=(xm,0),
arrowprops=dict(arrowstyle='->', color=color, lw=2))
ax2.text(-1, 0.3, 'unstable', ha='center', fontsize=8, color='crimson')
ax2.text(0, 0.3, 'stable', ha='center', fontsize=8, color='seagreen')
ax2.text(1, 0.3, 'unstable', ha='center', fontsize=8, color='crimson')
ax2.set_xlim(-1.8, 1.8); ax2.set_ylim(-0.5, 0.6)
ax2.set_yticks([]); ax2.set_xlabel('$x$')
ax2.set_title('Phase line')
# Solutions
ax3 = axes[2]
f_ode2 = lambda t, x: [x[0]**3 - x[0]]
t_eval2 = np.linspace(0, 5, 400)
for x0, color in zip([-1.3, -0.8, -0.3, 0.3, 0.8, 1.3],
plt.cm.coolwarm(np.linspace(0.05, 0.95, 6))):
sol = solve_ivp(f_ode2, (0, 3), [x0], t_eval=np.linspace(0,3,400),
max_step=0.01, events=lambda t,y: abs(y[0])-8)
ax3.plot(sol.t, np.clip(sol.y[0], -5, 5), color=color, lw=2, label=f'$x_0={x0}$')
for xe in [-1, 0, 1]:
ax3.axhline(xe, ls='--', lw=1.2,
color='crimson' if xe != 0 else 'seagreen')
ax3.set_xlabel('$t$'); ax3.set_ylabel('$x(t)$')
ax3.set_title('Solutions'); ax3.legend(fontsize=7)
ax3.set_ylim(-3, 3)
plt.suptitle(r"$x' = x^3 - x$", fontsize=12)
plt.tight_layout()
plt.show()