Lab 4

Published

June 3, 2024

Goals for today

Just like our first three sessions, the idea of these labs is for you to learn enough so that you can use SymPy to help you out when checking answers if you work on examples and exercises in our book.

Our goals for today are:

  1. Finish the part about Taylor polynomials in Lab 3 and how they are related to optimization. Reload the webpage to get an updated version.
  2. Start by maintaining a Google Colab notebook from now, as a way to organize your notes. Learn bits and pieces of LaTeX and Markdown.
  3. As always, each student works through the exercise and picks up the skills needed to implement what you saw in the lecture in the computer.

How to solve systems of equations in SymPy

  1. Make sure to put all equations in a form where the right hand size is zero. For example, 4x=5 should be in the form 4x5=0.
  2. Translate the mathematical expression into a form recognized by SymPy and solve for the unknowns. So, we have two usual ways of solving equations.
from sympy import *
x = symbols('x', real = True)
solve(4*x-5, x)
[5/4]
solve(4*x-5, x, dict = True)
[{x: 5/4}]

It may sometimes be convenient to have dict = True because this dictionary form of the output is useful for quick substitutions.

  1. If you have a system of equations, we just have to put them in a list. For example, the system of equations in Section 13.1 Example 1 will look like the following in SymPy:
from sympy import *
x, y = symbols('x y', real = True)
solve([-4*x-2*y+36, -2*x-4*y+42], [x, y])
{x: 5, y: 8}
solve([-4*x-2*y+36, -2*x-4*y+42], [x, y], dict = True)
[{x: 5, y: 8}]

Combine what you have learned here and what you have learned in the past about derivatives. Do Section 13.1 Example 1 completely. Start from encoding the function to be maximized in SymPy, get first derivatives, and look for stationary points.

Templates for plotting with not too many bells and whistles

It may be useful, especially for Chapter 13, to draw surfaces, traces, and contours in Python. We are going to use matplotlib and numpy, because plotting in SymPy is slightly limited and SymPy also somewhat relies on matplotlib. All codes used for this section are based on

import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0, 10, 200)
y = np.sin(x)
fig, ax = plt.subplots()
ax.plot(x, y)
plt.show()

matplotlib is so extensive that you can easily be overwhelmed. Always think about what you want to show first before trying out the many different options.

fig, ax = plt.subplots()
ax.plot(x, y, 'r-', linewidth=2, label='$y=\sin(x)$', alpha=0.6)
ax.legend(loc='upper center')
ax.set_yticks([-1, 0, 1])
ax.set_xticks([0, 5, 10])
ax.set_title('Test plot')
plt.show()

For 3D surfaces, let use revisit Section 13.1 Example 1. Here you will get a sense of the contours of the surface in that example.

import matplotlib.pyplot as plt
import numpy as np
def f(x, y):
    return -2*x**2-2*x*y-2*y**2+36*x+42*y-158
x = np.linspace(0, 15, 100)
y = np.linspace(0, 15, 100)
# Pay attention to the case of the letters!!!!
X, Y = np.meshgrid(x, y)
Z = f(X, Y)

fig = plt.figure()
ax = plt.axes(projection='3d')
ax.contour3D(X, Y, Z, 100)
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_zlabel('z');

If you want the traces of the surface where you essentially take “slices” of the surface, you can use the code below. For example, what would f(x,y) look like, when y is fixed at 0?

plt.plot(x, f(x, 0))

Next, what would f(x,y) look like, when x is fixed at 0?

plt.plot(y, f(0, y))

Finally, it may be useful to see what happens to these slices when we fix y at 8 and then x at 5. Why do you think I am doing this?

plt.plot(x, f(x, 8))

plt.plot(y, f(5, y))

It is possible to make all of these fancier, but I think it depends on what visualizations you want. Think about that first before laying out a canvas.

Do Section 13.2 Example 1 completely.

  1. Start from encoding the function to be maximized, get first derivatives, and look for stationary points.
  2. Find all second-order partial derivatives.
  3. Determine whether you have a maximum or minimum.
  4. Plot the contours of f and some of the more relevant traces, especially within the context of the example.