ECON526: Problem Set 1 (Solutions)

Authors
Affiliation

Jesse Perla, UBC

Jesse Perla

University of British Columbia

SOLUTIONS

TipSOLUTIONS

We are mostly looking to ensure they have a working python setup, so expect grades for this to be nearly perfect for all students.

Instructions

  • Ensure you modify the field above with your name and student number above immediately
  • Modify directly and do not rename the file as Canvas will automatically append your name to it.
  • Submit both the .ipynb and an exported html version of this file.
  • See here for instructions.
  • Do not change the filenames. Canvas automatically appends your name/student number to submitted files.

Setup

import numpy as np
import matplotlib.pyplot as plt

Install Python (with Jupyter and VS Code)

  • See here for instructions and links.
  • VS Code is optional but recommended.

Question 1

Modify the next cell with some math text with the Pythagorean theorem (e.g. \(x^2 + y^2 = z^2\))

TipSOLUTIONS

Any markdown cell with LaTeX math, e.g. \(x^2 + y^2 = z^2\).

Question 2

Modify the next cell to create a function which takes the sides of the rectangle and calculates the hypotenuse (i.e., code up \(z(x,y) = \sqrt{x^2 + y^2}\)) then calculate \(z(3,4)\)

# modify here
TipSOLUTIONS
# SOLUTIONS
def hypotenuse(x, y):
    return np.sqrt(x**2 + y**2)


hypotenuse(3, 4)
np.float64(5.0)

Question 3

Take the following system of equations, build a matrix, and solve for the system of equations using numpy’s linalg.solve function.

\[ \begin{aligned} 2 x_1 + 3 x_2 &= 5 \\ 4 x_1 + 9 x_2 &= 20 \end{aligned} \]

# modify here
TipSOLUTIONS
# SOLUTIONS
A = np.array([[2, 3], [4, 9]])
b = np.array([5, 20])
x = np.linalg.solve(A, b)
x
array([-2.5       ,  3.33333333])

Question 4

Now take the following system

\[ \begin{aligned} x_1 + 2 x_2 &= 5 \\ 2 x_1 + 4 x_2 &= 10 \end{aligned} \]

Try this with linalg.solve. Why doesn’t it work? If this has a solution, find it and explain.

# modify here
TipSOLUTIONS

The second row is twice the first, so the matrix is singular and np.linalg.solve([[1, 2], [2, 4]], [5, 10]) raises LinAlgError: Singular matrix. The system is still consistent because the right-hand side is also doubled, so there are infinitely many solutions: every \((x_1, x_2)\) with \(x_1 = 5 - 2 x_2\).

Question 5

Plot \(f(x) = x^2\) for a grid of 20 points of \(x \in [0,1]\). Hint: You can make a grid with np.linspace(...) and use matplotlib’s plt.plot(...) function.

# modify here
TipSOLUTIONS
# SOLUTIONS
x = np.linspace(0, 1, num=20)
y = x**2
plt.plot(x, y)