Menu

[r3313]: / trunk / py4science / book / examples / scipy / roots1d.py  Maximize  Restore  History

Download this file

33 lines (23 with data), 587 Bytes

 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
import scipy as S
import pylab as P
# poly1d objects are constructed from coefficients, highest-order first.
coefs = [ 1. , -3.5, -0.5, 11. , -17. , 6. ]
pol = S.poly1d(coefs)
roots = pol.r
print 'Polynomial p(x):\n',pol,'\n'
print 'p(x) built with coefs:',coefs
print 'Roots of p(x):',roots
# Plot p(x)
x = P.frange(-4,4,npts=400)
y = pol(x)
P.figure()
P.axhline(0,color='g')
P.axvline(0,color='g')
P.plot(x,y,'b-')
# Show roots
P.scatter(roots.real,roots.imag,s=80,c='r')
# Set limits and grid to make the plot clear
P.ylim(-40,40)
P.grid()
# Display on screen
P.show()
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.