Solutions to Sheet 3 by Rasmus Kornbeck

In [6]:
import matplotlib.pyplot as plt
import numpy as np
import itertools
In [7]:
# problem 1
def f(x):
    return 1/(x-1)
x1 = np.linspace(-2, 0.99)
x2 = np.linspace(1.01, 6)

y1 = f(x1)
y2 = f(x2)

plt.plot(x1, y1, lw=4, ls='--', c="m")
plt.plot(x2, y2, lw=4, ls='--', c="m")

plt.xlim(-2, 6)
plt.ylim(-6, 6)

plt.show()
2021-03-14T12:34:59.440711 image/svg+xml Matplotlib v3.3.4, https://matplotlib.org/
In [8]:
# problem 2
x = np.linspace(0, np.pi*2)
styles = [
    {"c": "g", "ls": "-"},
    {"c": "r", "ls": "--"},
    {"c": "b", "ls": "--"},
    {"c": "m", "ls": ":"}
]

fig = plt.figure()
ax1 = fig.add_subplot(221)
ax1.set_title("sin(x)")
ax1.plot(x, np.sin(x), **styles[0])
plt.axis([0, np.pi*2, -2, 2])

ax2 = fig.add_subplot(222)
ax2.set_title("sin(2x)")
ax2.plot(x, np.sin(2*x), **styles[1])
plt.axis([0, np.pi*2, -2, 2])

ax3 = fig.add_subplot(223)
ax3.set_title("2sin(x)")
ax3.plot(x, 2*np.sin(x), **styles[2])
plt.axis([0, np.pi*2, -2, 2])

ax4 = fig.add_subplot(224)
ax4.set_title("2sin(2x)")
ax4.plot(x, 2*np.sin(2*x), **styles[3])
plt.axis([0, np.pi*2, -2, 2])

plt.suptitle("Bunch of plots")
plt.show()
2021-03-14T12:35:00.268340 image/svg+xml Matplotlib v3.3.4, https://matplotlib.org/
In [9]:
# problem 2 - alternative
x = np.linspace(0, np.pi*2)
y = [np.sin(x), 2*np.sin(x), np.sin(2*x), 2*np.sin(2*x)]
titles = ["sin(x)", "2sin(x)", "sin(2x)", "2sin(2)"]
styles = [
    {"c": "g", "ls": "-"},
    {"c": "b", "ls": "--"},
    {"c": "r", "ls": "--"},
    {"c": "m", "ls": ":"}
]

fig, axs = plt.subplots(2, 2)
fig.suptitle("Bunch of plots")
q = itertools.chain.from_iterable(zip(*axs)) # goes by column rather than row

for ax, f, titl, style in zip(q, y, titles, styles):
    ax.plot(x, f, **style)
    ax.axis([0, np.pi*2, -2, 2])
    ax.set_title(titl)
2021-03-14T12:35:01.592491 image/svg+xml Matplotlib v3.3.4, https://matplotlib.org/
In [10]:
# problem 3
y = np.loadtxt("results.txt", delimiter=",", skiprows=1)
x = np.arange(1, 22)

fig = plt.figure(figsize=(7,7))
ax1 = fig.add_subplot(221)
ax1.plot(x, y[:,0])
ax1.set_title("asg1 impl")

ax2 = fig.add_subplot(222)
ax2.loglog(x, y[:,0])
ax2.set_title("asg1 log") # polynomial

ax3 = fig.add_subplot(223)
ax3.plot(x, y[:,1])
ax3.set_title("numpy impl")

ax4 = fig.add_subplot(224)
ax4.semilogy(x, y[:,1])
ax4.set_title("numpy log-linear") # exponential
---------------------------------------------------------------------------
OSError                                   Traceback (most recent call last)
<ipython-input-10-535e242131cb> in <module>
      1 # problem 3
----> 2 y = np.loadtxt("results.txt", delimiter=",", skiprows=1)
      3 x = np.arange(1, 22)
      4 
      5 fig = plt.figure(figsize=(7,7))

/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/numpy/lib/npyio.py in loadtxt(fname, dtype, comments, delimiter, converters, skiprows, usecols, unpack, ndmin, encoding, max_rows, like)
   1063             fname = os_fspath(fname)
   1064         if _is_string_like(fname):
-> 1065             fh = np.lib._datasource.open(fname, 'rt', encoding=encoding)
   1066             fencoding = getattr(fh, 'encoding', 'latin1')
   1067             fh = iter(fh)

/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/numpy/lib/_datasource.py in open(path, mode, destpath, encoding, newline)
    192 
    193     ds = DataSource(destpath)
--> 194     return ds.open(path, mode, encoding=encoding, newline=newline)
    195 
    196 

/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/numpy/lib/_datasource.py in open(self, path, mode, encoding, newline)
    529                                       encoding=encoding, newline=newline)
    530         else:
--> 531             raise IOError("%s not found." % path)
    532 
    533 

OSError: results.txt not found.