Week 4

Quiz

  1. Use list comprehension to create a list of 100 points in the interval [-1,1].
  2. Name 3 things that are required for a good presentation of a graph.
  3. What command is used to customize the tick marks on the x-axis, and what inputs does it require?
  4. Graph $y=e^x$ on the interval [0,3].

Answers:

In [ ]:
X = [i/50-1 for i in range(101)]
  1. Title, labels on the axes, legend if more than 1 function, sensible ticks, use plt.show()
  2. plt.xticks(pos,labs) where pos is the positions of the ticks and labs is the labels
In [1]:
import matplotlib.pyplot as plt
from math import e
x_vals = [i/30 for i in range(91)]
y_vals = [e**i for i in x_vals]
plt.plot(x_vals,y_vals)
plt.show()

Notes

Let's solve the congruence equation $$ 3x^2 - 4x - 7 \equiv 0 \mod n $$ for various values of $ n $.

In [2]:
"""Write a function that takes n as input and finds all the positive solutions x < n"""
def cong_solns(n):  # the input is a positive integer
    solns = []
    for x in range(n):
        if (3*x**2-4*x-7)%n == 0:
            solns.append(x)
    return solns
In [10]:
cong_solns(4)
Out[10]:
[1, 3]
In [16]:
inputs = []
outputs = []
for n in range(100):
    solns = cong_solns(n)
    outputs += solns
    for x in solns:
        inputs.append(n)
plt.plot(inputs,outputs,'.')
plt.show()

Exercise: Write a function that takes two integers as input and returns their GCD.

In [ ]: