Menu

[r4657]: / trunk / py4science / examples / skel / polyroots1d_skel.py  Maximize  Restore  History

Download this file

48 lines (38 with data), 1.0 kB

 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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#!/usr/bin/env python
"""Simple demonstration of polynomials and root finding."""
import numpy as N
import scipy as S
import pylab as P
def plot_poly(x,y,roots):
"""Simple utility to make a charting screen with x/y axes"""
P.figure()
P.axhline(0,color='g')
P.axvline(0,color='g')
P.grid()
P.plot(x,y,'b-')
P.scatter(roots.real,roots.imag,s=80,c='r')
P.xlabel('Re')
P.ylabel('Im')
# Create the coefficients for a polynomial with nroots_minus1 at x=-1, one
# root at -2 and one root at 1:
coefs = XXX # Hint: use reduce() and N.convolve
# Construct the polynomial and get its roots
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
# Sample and plot p(x)
x = XXX
y = XXX
# Show roots
plot_poly(x,y,roots)
P.xlim(-3,2)
P.ylim(-250,100)
P.title('All roots')
# Make a new plot around the -1 cluster of roots (use a window of size eps
# around -1)
eps = 0.05
XXX
# 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.