-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrbc_distance_example.py
69 lines (56 loc) · 1.74 KB
/
rbc_distance_example.py
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import numpy as np
import matplotlib.pyplot as plt
def perturb_function( nx, vf, nsamp=5 ):
"""
Given f, perturb a few points, but keep the two functions close.
nx -- x points
vf -- vectorized version of f
"""
# get indices to perturb
sample_idx = np.random.random_integers( 0, len(nx), size=(nsamp,) )
print sample_idx
ny = vf( nx )
# perturb ny
ny[ sample_idx ] += np.random.sample( size=(nsamp,) )
return ny
def plot_func( nx, ny, fig=None, diag=True ):
"""
"""
if not fig:
fig = plt.figure()
color = "b"
label = r"$f(x)$"
else:
print "hello"
color = "r"
label = r"$g(x)$"
if not diag:
ax = fig.gca()
ax.plot( nx, ny, lw=3, color=color, label=label )
else:
ax1 = fig.add_subplot( 121 )
ax1.plot( nx, ny, lw=3, color=color, label=label )
ax1.set_xlabel( r'$x$', fontsize=20 )
ax1.set_ylabel( r'$f(x)$', fontsize=20 )
# plot diagonal
ax2 = fig.add_subplot( 122 )
ax2.plot( nx, nx, 'k-', lw=1.5 )
ax2.set_xlabel( r'$\tau$', fontsize=20 )
fig.show()
return fig
def main():
# create a couple of polynomials that are "close".
nx = np.linspace( 0, 5, 200 )
f1 = lambda x : 0.3 * ( x * (0.9*x - 1) * (0.8*x - 2.5) * (x - 4) * (x-4.5) ) + 3
f2 = lambda x : 0.3 * ( x * (0.9*x - 1) * (0.8*x - 2.5) * (x - 4) * (x-4.5)*\
(0.9*x - 0.2) * (0.5*x -2) * (0.7*x - 2.1) ) + 3
vf1 = np.vectorize( f1 )
vf2 = np.vectorize( f2 )
ny1 = vf1( nx )
ny2 = vf2( nx )
thefig = plot_func( nx, ny1 )
#thefig = plot_func( nx, ny2, fig=thefig )
plt.legend()
return thefig
if __name__ == "__main__":
fig = main()