1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#!/usr/bin/env python

# Exercise (a)

import pylab

x = pylab.arange(-5,5,.01)
f = pylab.poly1d((1./3, 0, 0, -2, -6*pylab.pi, -10))
print "f(x) ="
print f

print "Roots:\n\tx%s\ty" % (11*" ")
f_roots = pylab.roots(f)
for i in range(len(f_roots)):
    if f_roots[i].imag == 0:
        print "\t%+012.3e\t%+012.3e" % (f_roots[i],f(f_roots[i]))
        
print "Extrema:\n\tx%s\ty" % (11*" ")
f_prime = pylab.polyder(f)
f_prime2 = pylab.polyder(f_prime)
f_prime_roots = pylab.roots(f_prime)
for i in range(len(f_prime_roots)):
    if f_prime_roots[i].imag == 0:
        if f_prime2(f_prime_roots[i]) < 0: kind = 'Maximum'
        elif f_prime2(f_prime_roots[i]) > 0: kind = 'Minimum'
        else: kind=''
        print "\t%+012.3e\t%+012.3e\t(%s)" % (f_prime_roots[i],f(f_prime_roots[i]),kind)

pylab.plot(x,f(x))
pylab.show()