|
| 1 | +from numpy import * |
| 2 | +from numpy.linalg import * |
| 3 | +from scipy.linalg import * |
| 4 | +from matplotlib.pyplot import * |
| 5 | +from mpl_toolkits.mplot3d import * |
| 6 | +from scipy.integrate import solve_ivp |
| 7 | +# Python 3.x Standard Library |
| 8 | +import gc |
| 9 | +import os |
| 10 | + |
| 11 | +# Third-Party Packages |
| 12 | +import numpy as np; np.seterr(all="ignore") |
| 13 | +import numpy.linalg as la |
| 14 | +import scipy.misc |
| 15 | +import matplotlib as mpl; mpl.use("Agg") |
| 16 | +import matplotlib.pyplot as pp |
| 17 | +import matplotlib.axes as ax |
| 18 | +import matplotlib.patches as pa |
| 19 | + |
| 20 | + |
| 21 | +# |
| 22 | +# Matplotlib Configuration & Helper Functions |
| 23 | +# -------------------------------------------------------------------------- |
| 24 | + |
| 25 | +# TODO: also reconsider line width and markersize stuff "for the web |
| 26 | +# settings". |
| 27 | +fontsize = 10 |
| 28 | + |
| 29 | +width = 345 / 72.27 |
| 30 | +height = width / (16/9) |
| 31 | + |
| 32 | +rc = { |
| 33 | + "text.usetex": True, |
| 34 | + "pgf.preamble": r"\usepackage{amsmath,amsfonts,amssymb}", |
| 35 | + #"font.family": "serif", |
| 36 | + "font.serif": [], |
| 37 | + #"font.sans-serif": [], |
| 38 | + "legend.fontsize": fontsize, |
| 39 | + "axes.titlesize": fontsize, |
| 40 | + "axes.labelsize": fontsize, |
| 41 | + "xtick.labelsize": fontsize, |
| 42 | + "ytick.labelsize": fontsize, |
| 43 | + "figure.max_open_warning": 100, |
| 44 | + #"savefig.dpi": 300, |
| 45 | + #"figure.dpi": 300, |
| 46 | + "figure.figsize": [width, height], |
| 47 | + "lines.linewidth": 1.0, |
| 48 | +} |
| 49 | +mpl.rcParams.update(rc) |
| 50 | + |
| 51 | +# Web target: 160 / 9 inches (that's ~45 cm, this is huge) at 90 dpi |
| 52 | +# (the "standard" dpi for Web computations) gives 1600 px. |
| 53 | +width_in = 160 / 9 |
| 54 | + |
| 55 | +def save(name, **options): |
| 56 | + cwd = os.getcwd() |
| 57 | + root = os.path.dirname(os.path.realpath(__file__)) |
| 58 | + os.chdir(root) |
| 59 | + pp.savefig(name + ".svg", **options) |
| 60 | + os.chdir(cwd) |
| 61 | + |
| 62 | +def set_ratio(ratio=1.0, bottom=0.1, top=0.1, left=0.1, right=0.1): |
| 63 | + height_in = (1.0 - left - right)/(1.0 - bottom - top) * width_in / ratio |
| 64 | + pp.gcf().set_size_inches((width_in, height_in)) |
| 65 | + pp.gcf().subplots_adjust(bottom=bottom, top=1.0-top, left=left, right=1.0-right) |
| 66 | +def Q(f, xs, ys): |
| 67 | + X, Y = meshgrid(xs, ys) |
| 68 | + v = vectorize |
| 69 | + fx = v(lambda x, y: f([x, y])[0]) |
| 70 | + fy = v(lambda x, y: f([x, y])[1]) |
| 71 | + return X, Y, fx(X, Y), fy(X, Y) |
| 72 | +a = 2.0; x0 = 1.0 |
| 73 | +figure() |
| 74 | +t = linspace(0.0, 3.0, 1000) |
| 75 | +plot(t, exp(a*t)*x0, "k") |
| 76 | +xlabel("$t$"); ylabel("$x(t)$"); title(f"$a={a}$") |
| 77 | +grid(); axis([0.0, 2.0, 0.0, 10.0]) |
| 78 | +tight_layout() |
| 79 | +save("images/scalar-LTI-2") |
| 80 | +figure() |
| 81 | +plot(real(a), imag(a), "x", color="k") |
| 82 | +gca().set_aspect(1.0) |
| 83 | +xlim(-3,3); ylim(-3,3); |
| 84 | +plot([-3,3], [0,0], "k") |
| 85 | +plot([0, 0], [-3, 3], "k") |
| 86 | +xticks([-2,-1,0,1,2]); yticks([-2,-1,0,1,2]) |
| 87 | +title(f"$a={a}$"); grid(True) |
| 88 | +tight_layout() |
| 89 | +save("images/scalar-LTI-2-poles") |
| 90 | +a = 1.0; x0 = 1.0 |
| 91 | +figure() |
| 92 | +t = linspace(0.0, 3.0, 1000) |
| 93 | +plot(t, exp(a*t)*x0, "k") |
| 94 | +xlabel("$t$"); ylabel("$x(t)$"); title(f"$a={a}$") |
| 95 | +grid(); axis([0.0, 2.0, 0.0, 10.0]) |
| 96 | +tight_layout() |
| 97 | +save("images/scalar-LTI-1") |
| 98 | +figure() |
| 99 | +plot(real(a), imag(a), "x", color="k") |
| 100 | +gca().set_aspect(1.0) |
| 101 | +xlim(-3,3); ylim(-3,3); |
| 102 | +plot([-3,3], [0,0], "k") |
| 103 | +plot([0, 0], [-3, 3], "k") |
| 104 | +xticks([-2,-1,0,1,2]); yticks([-2,-1,0,1,2]) |
| 105 | +title(f"$a={a}$"); grid(True) |
| 106 | +tight_layout() |
| 107 | +save("images/scalar-LTI-1-poles") |
| 108 | +a = 0.0; x0 = 1.0 |
| 109 | +figure() |
| 110 | +t = linspace(0.0, 3.0, 1000) |
| 111 | +plot(t, exp(a*t)*x0, "k") |
| 112 | +xlabel("$t$"); ylabel("$x(t)$"); title(f"$a={a}$") |
| 113 | +grid(); axis([0.0, 2.0, 0.0, 10.0]) |
| 114 | +tight_layout() |
| 115 | +save("images/scalar-LTI-0") |
| 116 | +figure() |
| 117 | +plot(real(a), imag(a), "x", color="k") |
| 118 | +gca().set_aspect(1.0) |
| 119 | +xlim(-3,3); ylim(-3,3); |
| 120 | +plot([-3,3], [0,0], "k") |
| 121 | +plot([0, 0], [-3, 3], "k") |
| 122 | +xticks([-2,-1,0,1,2]); yticks([-2,-1,0,1,2]) |
| 123 | +title(f"$a={a}$"); grid(True) |
| 124 | +tight_layout() |
| 125 | +save("images/scalar-LTI-0-poles") |
| 126 | +a = -1.0; x0 = 1.0 |
| 127 | +figure() |
| 128 | +t = linspace(0.0, 3.0, 1000) |
| 129 | +plot(t, exp(a*t)*x0, "k") |
| 130 | +xlabel("$t$"); ylabel("$x(t)$"); title(f"$a={a}$") |
| 131 | +grid(); axis([0.0, 2.0, 0.0, 10.0]) |
| 132 | +tight_layout() |
| 133 | +save("images/scalar-LTI-m1") |
| 134 | +figure() |
| 135 | +plot(real(a), imag(a), "x", color="k") |
| 136 | +gca().set_aspect(1.0) |
| 137 | +xlim(-3,3); ylim(-3,3); |
| 138 | +plot([-3,3], [0,0], "k") |
| 139 | +plot([0, 0], [-3, 3], "k") |
| 140 | +xticks([-2,-1,0,1,2]); yticks([-2,-1,0,1,2]) |
| 141 | +title(f"$a={a}$"); grid(True) |
| 142 | +tight_layout() |
| 143 | +save("images/scalar-LTI-m1-poles") |
| 144 | +a = -2.0; x0 = 1.0 |
| 145 | +figure() |
| 146 | +t = linspace(0.0, 3.0, 1000) |
| 147 | +plot(t, exp(a*t)*x0, "k") |
| 148 | +xlabel("$t$"); ylabel("$x(t)$"); title(f"$a={a}$") |
| 149 | +grid(); axis([0.0, 2.0, 0.0, 10.0]) |
| 150 | +tight_layout() |
| 151 | +save("images/scalar-LTI-m2") |
| 152 | +figure() |
| 153 | +plot(real(a), imag(a), "x", color="k") |
| 154 | +gca().set_aspect(1.0) |
| 155 | +xlim(-3,3); ylim(-3,3); |
| 156 | +plot([-3,3], [0,0], "k") |
| 157 | +plot([0, 0], [-3, 3], "k") |
| 158 | +xticks([-2,-1,0,1,2]); yticks([-2,-1,0,1,2]) |
| 159 | +title(f"$a={a}$"); grid(True) |
| 160 | +tight_layout() |
| 161 | +save("images/scalar-LTI-m2-poles") |
| 162 | +a1 = -1.0; a2 = 2.0; x10 = x20 = 1.0 |
| 163 | +figure() |
| 164 | +t = linspace(0.0, 3.0, 1000) |
| 165 | +x1 = exp(a1*t)*x10; x2 = exp(a2*t)*x20 |
| 166 | +xn = sqrt(x1**2 + x2**2) |
| 167 | +plot(t, xn , "k") |
| 168 | +plot(t, x1, "k--") |
| 169 | +plot(t, x2 , "k--") |
| 170 | +xlabel("$t$"); ylabel("$\|x(t)\|$"); title(f"$a_1={a1}, \; a_2={a2}$") |
| 171 | +grid(); axis([0.0, 2.0, 0.0, 10.0]) |
| 172 | +tight_layout() |
| 173 | +save("images/scalar-LTI-m1p2") |
| 174 | +figure() |
| 175 | +plot(real(a1), imag(a1), "x", color="k") |
| 176 | +plot(real(a2), imag(a2), "x", color="k") |
| 177 | +gca().set_aspect(1.0) |
| 178 | +xlim(-3,3); ylim(-3,3); |
| 179 | +plot([-3,3], [0,0], "k") |
| 180 | +plot([0, 0], [-3, 3], "k") |
| 181 | +xticks([-2,-1,0,1,2]); yticks([-2,-1,0,1,2]) |
| 182 | +title(f"$a_1={a1}, \; a_2={a2}$") |
| 183 | +grid(True) |
| 184 | +tight_layout() |
| 185 | +save("images/scalar-LTI-m1p2-poles") |
| 186 | +a1 = -1.0; a2 = -2.0; x10 = x20 = 1.0 |
| 187 | +figure() |
| 188 | +t = linspace(0.0, 3.0, 1000) |
| 189 | +x1 = exp(a1*t)*x10; x2 = exp(a2*t)*x20 |
| 190 | +xn = sqrt(x1**2 + x2**2) |
| 191 | +plot(t, xn , "k") |
| 192 | +plot(t, x1, "k--") |
| 193 | +plot(t, x2 , "k--") |
| 194 | +xlabel("$t$"); ylabel("$\|x(t)\|$"); title(f"$a_1={a1}, \; a_2={a2}$") |
| 195 | +grid(); axis([0.0, 2.0, 0.0, 10.0]) |
| 196 | +tight_layout() |
| 197 | +save("images/scalar-LTI-m1m2") |
| 198 | +figure() |
| 199 | +plot(real(a1), imag(a1), "x", color="k") |
| 200 | +plot(real(a2), imag(a2), "x", color="k") |
| 201 | +gca().set_aspect(1.0) |
| 202 | +xlim(-3,3); ylim(-3,3); |
| 203 | +plot([-3,3], [0,0], "k") |
| 204 | +plot([0, 0], [-3, 3], "k") |
| 205 | +xticks([-2,-1,0,1,2]); yticks([-2,-1,0,1,2]) |
| 206 | +title(f"$a_1={a1}, \; a_2={a2}$") |
| 207 | +grid(True) |
| 208 | +tight_layout() |
| 209 | +save("images/scalar-LTI-m1m2-poles") |
| 210 | +a = 1.0j; x0=1.0 |
| 211 | +figure() |
| 212 | +t = linspace(0.0, 20.0, 1000) |
| 213 | +plot(t, real(exp(a*t)*x0), label="$\Re(x(t))$") |
| 214 | +plot(t, imag(exp(a*t)*x0), label="$\mathrm{Im}(x(t))$") |
| 215 | +xlabel("$t$") |
| 216 | +legend(); grid() |
| 217 | +tight_layout() |
| 218 | +save("images/scalar-LTI-alt-1") |
| 219 | +fig = figure() |
| 220 | +ax = fig.add_subplot(111, projection="3d") |
| 221 | +zticks = ax.set_zticks |
| 222 | +ax.plot(t, real(exp(a*t)*x0), imag(exp(a*t)*x0)) |
| 223 | +xticks([0.0, 20.0]); yticks([]); zticks([]) |
| 224 | +ax.set_xlabel("$t$") |
| 225 | +ax.set_ylabel("$\Re(x(t))$") |
| 226 | +ax.set_zlabel("$\mathrm{Im}(x(t))$") |
| 227 | +tight_layout() |
| 228 | +save("images/scalar-LTI-3d") |
| 229 | +figure() |
| 230 | +plot(real(a), imag(a), "x", color="k") |
| 231 | +gca().set_aspect(1.0) |
| 232 | +xlim(-3,3); ylim(-3,3); |
| 233 | +plot([-3,3], [0,0], "k") |
| 234 | +plot([0, 0], [-3, 3], "k") |
| 235 | +xticks([-2,-1,0,1,2]); yticks([-2,-1,0,1,2]) |
| 236 | +title(f"$a={a}$"); grid(True) |
| 237 | +tight_layout() |
| 238 | +save("images/scalar-LTI-1j-poles") |
| 239 | +a = -0.5 + 1.0j; x0=1.0 |
| 240 | +figure() |
| 241 | +t = linspace(0.0, 20.0, 1000) |
| 242 | +plot(t, real(exp(a*t)*x0), label="$\Re(x(t))$") |
| 243 | +plot(t, imag(exp(a*t)*x0), label="$\mathrm{Im}(x(t))$") |
| 244 | +xlabel("$t$") |
| 245 | +legend(); grid() |
| 246 | +tight_layout() |
| 247 | +save("images/scalar-LTI-alt-2") |
| 248 | +fig = figure() |
| 249 | +ax = fig.add_subplot(111, projection="3d") |
| 250 | +zticks = ax.set_zticks |
| 251 | +ax.plot(t, real(exp(a*t)*x0), imag(exp(a*t)*x0)) |
| 252 | +xticks([0.0, 20.0]); yticks([]); zticks([]) |
| 253 | +ax.set_xlabel("$t$") |
| 254 | +ax.set_ylabel("$\Re(x(t))$") |
| 255 | +ax.set_zlabel("$\mathrm{Im}(x(t))$") |
| 256 | +tight_layout() |
| 257 | +save("images/scalar-LTI-3d-2") |
| 258 | +figure() |
| 259 | +plot(real(a), imag(a), "x", color="k") |
| 260 | +gca().set_aspect(1.0) |
| 261 | +xlim(-3,3); ylim(-3,3); |
| 262 | +plot([-3,3], [0,0], "k") |
| 263 | +plot([0, 0], [-3, 3], "k") |
| 264 | +xticks([-2,-1,0,1,2]); yticks([-2,-1,0,1,2]) |
| 265 | +title(f"$a={a}$") |
| 266 | +grid(True) |
| 267 | +tight_layout() |
| 268 | +save("images/scalar-LTI-m11j-poles") |
0 commit comments