-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathincsearch.py
More file actions
51 lines (40 loc) · 1.02 KB
/
incsearch.py
File metadata and controls
51 lines (40 loc) · 1.02 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
48
49
50
51
import numpy as np
import matplotlib.pyplot as plt
x1=np.linspace(3, 6, 50)
func1=lambda x1: np.sin(np.dot(10.0,x1))+np.cos(np.dot(3.0, x1))
f1=func1(x1)
plt.figure(1)
plt.plot(x1,f1, 'ro-')
plt.grid()
#plt.show()
x2=np.linspace(3, 6, 100)
func2=lambda x2: np.sin(np.dot(10.0,x2))+np.cos(np.dot(3.0, x2))
f2=func1(x2)
plt.figure(2)
plt.plot(x2,f2, 'bd-')
plt.grid()
#plt.show()
def incsearch(func, xmin, xmax, ns):
x=np.linspace(xmin, xmax, ns)
f=func(x)
nb=0
xb=[]
for k in np.arange(np.size(x)-1):
if np.sign(f[k]) != np.sign(f[k+1]):
nb=nb+1
xb.append(x[k])
xb.append(x[k+1])
xbt=np.hstack(xb)
xb=xbt.reshape(nb, 2)
return nb, xb
xmin=3; xmax=6
func=lambda x: np.sin(np.dot(10.0, x))+np.cos(np.dot(3.0, x))
# check the 50 points
nb, xb=incsearch(func, 3, 6, 50)
print('number of brackets= ', nb)
print('root interval=', xb)
# check the 100 points
nb1, xb1=incsearch(func, 3, 6, 100)
print('number of brackets= ', nb1)
print('root interval=', xb1)
plt.show()