# for lecture use notebook
%matplotlib inline
# set up view
import matplotlib.animation as animation
import matplotlib as mp
mp.rcParams['animation.html'] = 'jshtml'
qr_setting = None
qrviz_setting = 'save'
#
%config InlineBackend.figure_format='retina'
# import libraries
import numpy as np
import matplotlib as mpf
import pandas as pd
import matplotlib.pyplot as plt
import laUtilities as ut
import slideUtilities as sl
import demoUtilities as dm
import pandas as pd
from importlib import reload
from datetime import datetime
from IPython.display import Image
from IPython.display import display_html
from IPython.display import display
from IPython.display import Math
from IPython.display import Latex
from IPython.display import HTML;
These are stored and run from this notebook because doing it in the lecture slides notebook introduces irritating artifacts: it does not seem to be possible to create the animation and display it without also displaying the still image produced in the last animation frame!
After searching stack exchange again I discovered the trick of closing the figure in order to suppress the still image. This notebook is updated to show the technique, but I moved all the code back into the main L17 notebook, so this notebook is no longer needd.
# we are putting x into a list so that it can be read inside the
# animate() closure. Currently can only read env variables in a closure
# this is the routine that will be called on each timestep
def animate(i):
newx = A @ x[0]
x[0] = newx
xvals.append(x[0][0])
yvals.append(x[0][1])
lines[0].set_data(xvals,yvals)
import matplotlib.animation as animation
A = np.array([[np.cos(0.1),-np.sin(0.1)],[ np.sin(0.1),np.cos(0.1)]])
x = [np.array([1, 500.])]
xvals = []
yvals = []
fig, ax = plt.subplots(figsize = (6, 6))
ax.set_xlim(-500, 500)
ax.set_ylim(-500, 500)
plt.plot(-500, -500, ''),
plt.plot(500, 500, '')
plt.axis('equal')
#
# close the figure so that it doesn't separately plot itself
plt.close()
#
lines = ax.plot([], [], 'ro-')
#
# instantiate the animator.
animation.FuncAnimation(fig,
animate,
frames = 75,
fargs = None,
interval = 100,
repeat=False)
A = np.array([[1.1, 0],[0, 0.9]])
x = [np.array([1,500.])]
xvals = []
yvals = []
fig = plt.figure()
plt.ioff()
ax = plt.axes(xlim=(-500,500),ylim=(-500,500))
plt.plot(-500, -500,''),
plt.plot(500, 500,'')
plt.axis('equal')
lines = ax.plot([],[],'o-')
# instantiate the animator.
anim = animation.FuncAnimation(fig, animate,
frames=75, interval=150, repeat=False, blit=False)
anim.save('images/L17-ex2.mp4')
A = np.array([[0.8, 0.5],[-0.1, 1.0]])
x = [np.array([1,500.])]
xvals = []
yvals = []
fig = plt.figure()
plt.ioff()
ax = plt.axes(xlim=(-500,500),ylim=(-500,500))
plt.plot(-500, -500,''),
plt.plot(500, 500,'')
plt.axis('equal')
lines = ax.plot([],[],'o-')
# instantiate the animator.
anim = animation.FuncAnimation(fig, animate,
frames=75, interval=150, repeat=False, blit=False)
anim.save('images/17-ex3.mp4')
D = np.array([[1.1, 0],[0, 0.9]])
P = np.array([[1,2],[4,1]])
A = P @ D @ np.linalg.inv(P)
x = [np.array([600+5,300+5.])]
xvals = []
yvals = []
size = 400
fig = plt.figure()
plt.ioff()
ax = plt.axes(xlim=(-size,size),ylim=(-size,size))
plt.plot(-size, -size,''),
plt.plot(size, size,'')
plt.axis('equal')
nsteps = 50
lines = ax.plot([],[],'o-')
# instantiate the animator.
anim = animation.FuncAnimation(fig, animate,
frames=nsteps, interval=150, repeat=False, blit=False)
anim.save('images/L18-ex1.mp4')
fig = plt.figure()
ax = plt.axes(xlim=(-size,size),ylim=(-size,size))
plt.plot(-size, -size,''),
plt.plot(size, size,'')
plt.axis('equal')
ev1 = P[:, 0]
line1 = np.column_stack((-100 * ev1, 100 * ev1))
ev2 = P[:, 1]
line2 = np.column_stack((-250 * ev2, 250 * ev2))
plt.plot(line1[0], line1[1], '-')
plt.plot(line2[0], line2[1], '-', color = 'b')
plt.text(-575, -50, r'Eigenspace for $\lambda_2 = 0.9$')
plt.text(-575, -100, r'Span {(2, 1)}')
plt.text(-50, -300, r'Eigenspace for $\lambda_1 = 1.1$')
plt.text(-50, -350, r'Span {(1, 4)}')
plt.savefig('images/L18-ex1-static.png')
print(A)
x = [np.array([600+5,300+5.])]
xvals = []
yvals = []
fig = plt.figure()
plt.ioff()
size = 400
ax = plt.axes(xlim=(-size,size),ylim=(-size,size))
plt.plot(-size, -size,''),
plt.plot(size, size,'')
plt.axis('equal')
ev1 = P[:, 0]
line1 = np.column_stack((-100 * ev1, 100 * ev1))
ev2 = P[:, 1]
line2 = np.column_stack((-250 * ev2, 250 * ev2))
plt.plot(line1[0], line1[1], '-')
plt.plot(line2[0], line2[1], '-', color = 'b')
plt.text(-575, -50, r'Eigenspace for $\lambda_2 = 0.9$')
plt.text(-50, -300, r'Eigenspace for $\lambda_1 = 1.1$')
nsteps = 50
lines = ax.plot([],[],'o-')
# instantiate the animator.
anim = animation.FuncAnimation(fig, animate,
frames=nsteps, interval=150, repeat=False, blit=False)
anim.save('images/L18-ex2.mp4')
[[ 0.87142857 0.05714286] [-0.11428571 1.12857143]]