Example Assignment


Before you turn this problem in, make sure everything runs as expected. First, restart the kernel (in the menubar, select Kernel$\rightarrow$Restart) and then run all cells (in the menubar, select Cell$\rightarrow$Run All).

Make sure you fill in any place that says YOUR CODE HERE or "YOUR ANSWER HERE", as well as your name and collaborators below:


In [ ]:
NAME = ""
COLLABORATORS = ""


In [ ]:
# import plotting libraries
%matplotlib inline
import matplotlib.pyplot as plt

Problem 1

Write a function that returns a list of numbers, such that $x_i=i^2$, for $1\leq i \leq n$. Make sure it handles the case where $n<1$ by raising a ValueError.


In [ ]:
def squares(n):
    """Compute the squares of numbers from 1 to n, such that the 
    ith element of the returned list equals i^2.
    
    """
    # YOUR CODE HERE
    raise NotImplementedError

Your function should print [1, 4, 9, 16, 25, 36, 49, 64, 81, 100] for $n=10$. Check that it does:


In [ ]:
squares(10)

In [ ]:


In [ ]:


Problem 2

Part A

Using your squares function, write a function that computes the sum of the squares of the numbers from 1 to $n$. Your function should call the squares function -- it should NOT reimplement its functionality.


In [ ]:
def sum_of_squares(n):
    """Compute the sum of the squares of numbers from 1 to n."""
    # YOUR CODE HERE
    raise NotImplementedError

The sum of squares from 1 to 10 should be 385. Verify that this is the answer you get:


In [ ]:
sum_of_squares(10)

In [ ]:


In [ ]:

Part B

Using LaTeX math notation, write out the equation that is implemented by your sum_of_squares function.

YOUR ANSWER HERE

Part C

Create a plot of the sum of squares for $n=1$ to $n=15$. Make sure to appropriately label the $x$-axis and $y$-axis, and to give the plot a title. Set the $x$-axis limits to be 1 (minimum) and 15 (maximum).


In [ ]:
fig, ax = plt.subplots() # do not delete this line!
# YOUR CODE HERE
raise NotImplementedError

In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]: