title: Find the max and min values of a function using python
body: <p>Write a function <code>maxmin(f, a, b, n=1000)</code> for finding the maximum
  and minimum values of a mathematical function <code>f(x)</code> in the interval
  between <code>a</code> and <code>b</code>. The following test program</p>
  
  <pre><code>from math import cos, pi
  print maxmin(cos, -pi/2, 2*pi, 100001)
  </code></pre>
  
  <p>should write out</p>
  
  <pre><code>(1.0, -1.0).
  </code></pre>
  
  <p>The <code>maxmin</code> function can compute a set of <code>n</code> uniformly spaced coordinates
  between <code>a</code> and <code>b</code> stored in a list <code>x</code>, then compute <code>f</code> at the
  points in <code>x</code> and store the values in another list <code>y</code>. The Python functions
  <code>max(y)</code> and <code>min(y)</code> return the maximum and minimum values in the
  list <code>y</code>, respectively.</p>
  
  <p>I tried using </p>
  
  <pre><code>from math import cos, pi
  import numpy
  
  def maxmin(f, a, b, n=1000):
      x = (f(i) for i in numpy.linspace(a, b, n))
      #print x
      maximum = max(x==2)
      minimum = min(x==0)
      return maximum, minimum
  
  print maxmin(cos, -pi/2, 2*pi, 100001)
  </code></pre>
  
  <p>But it doesn't run properly. When i mean it doesn't work properly i doesn't give me the max and min that i should be getting which (1.0, -1.0).
   This is the error i get</p>
  
  <pre><code>minimum = min(x)
  ValueError: min() arg is an empty sequence
  &gt;&gt;&gt;
  </code></pre>
  
  <p>I'm confused what sequence it wants</p>