-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathincremental1.py
More file actions
36 lines (30 loc) · 892 Bytes
/
incremental1.py
File metadata and controls
36 lines (30 loc) · 892 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
31
32
33
34
35
36
import numpy as np
import matplotlib.pyplot as plt
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
def draw(xmin, xmax, inc, func):
x = np.linspace(xmin, xmax, inc)
#func = lambda x: np.sin(np.dot(10.0, x)) + np.cos(np.dot(3.0, x))
f1 = func(x)
plt.figure(1)
plt.plot(x, f1, 'ro-')
plt.grid()
plt.show()
if __name__ == '__main__':
xmin=3; xmax=6
inc=100
func=lambda x: np.sin(np.dot(10.0, x))+np.cos(np.dot(3.0, x))
nb, xb=incsearch(func, xmin, xmax, inc)
draw(xmin, xmax, inc, func)
print('number of brackets= ', nb)
print('root interval=', xb)