0% found this document useful (0 votes)
18 views

Zero Fonction - Py

Uploaded by

Yasser Rghaoui
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

Zero Fonction - Py

Uploaded by

Yasser Rghaoui
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

1 #~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=

2 # UIASS > CPGE > MPSI > [email protected]


3 #~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=
4 #
5 # Zero de fonction
6 #
7 #~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=
8 import numpy as np
9 import scipy.optimize as spo
10 import matplotlib.pyplot as plt
11 #~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.
12 def f( x ): return (x-3)**3+4
13 #return (x*x+ np.cos(9*x))*np.sin(x)-2+1/np.exp(x)
14 #~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.
15 def fp( x ): return 3*(x-3)**2
16 #~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.
17 def dichotomie(f, a, b, epsl=10**-5):
18 if f(a)*f(b) > 0: return None
19 while True:
20 c = (a+b)/2
21 if abs(f(c)) < epsl : return c #if b-a < eps : return c
22 if f(a)*f(c) < 0: b = c
23 else : a = c
24 ## plt.plot(c, 0, ".", color="red")
25 #~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.
26 def newton(f, x, fp, epsl=10**-5, maxIter=100):
27 k = 0
28 for i in range(maxIter):
29 #if fp(x) == 0: return None
30 z = x - f(x)/fp(x)
31 if abs(z-x) < epsl: break # if abs(f(z)) < epsl: return z
32 x = z
33 return z
34 ## plt.plot(z, 0, "*", color="green")
35 #~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.
36 def Sequante(f, a, b, epsl=10**-5, maxIter=1000):
37 x = ( a*f(b)-b*f(a) ) / ( f(b)-f(a) )
38 for i in range(maxIter):
39 if abs( f(x) ) < epsl: return x
40 b, a = x, b
41 #if f(b)-f(a) == 0: return None
42 x = ( a*f(b) - b*f(a) ) / ( f(b)-f(a) )
43 ## W = np.linspace(0, f(x), 25)
44 ## plt.plot([x]*len(W), W, "--", color="cyan")
45 #~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.
46 def Regula_Falsi(f, a, b, epsl=10**-5, maxIter=1000):
47 for i in range(maxIter):
48 x = ( a*f(b)-b*f(a) ) / ( f(b)-f(a) )
49 if abs(f(x)) < epsl : return x #if b-a < eps : return x
50 if f(a)*f(x) < 0: b = x
51 else : a = x
52 ## W = np.linspace(0, f(x), 25)
53 ## plt.plot([x]*len(W), W, "--", color="orange")
54 #~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.
55 fig, ax = plt.subplots()
56 ax.grid(True, which='both')
57
58 ax.spines[ 'left'].set_position('zero') ; ax.spines['right'].set_color('none')
59 ax.spines['bottom'].set_position('zero') ; ax.spines[ 'top'].set_color('none')
60 ax.yaxis.tick_left() ; ax.xaxis.tick_bottom()
61
62 a, b = 0, 10 # a, b = -np.pi, np.pi #a, b = 0, 2.4
63 X = np.linspace( a, b, 200 ) ; Y = f(X) ; plt.plot(X, Y, lw=2, color="blue")
64
65 x0 = dichotomie(f, a, b) ; plt.plot(x0, f(x0), "*", lw=3, color="red")
66 x0 = spo.bisect(f, a, b) ; plt.plot(x0, f(x0), "*", lw=5, color="pink")
67
68 x1 = newton(f, b, fp) ; plt.plot(x1, f(x1), "D", lw=3, color="green")
69 x1 = spo.newton(f, b, fp) ; plt.plot(x1, f(x1), "D", lw=5, color="lime")
70
71 x2 = Sequante(f, a, b) ; plt.plot(x2, f(x2), "o", lw=3, color="cyan")
72 x2 = spo.newton(f, a) ; plt.plot(x2, f(x2), "o", lw=5, color="purple")
73
74 x3 = Regula_Falsi(f, a, b) ; plt.plot(x3, f(x3), "s", lw=3, color="orange")
75 plt.show() # plt.savefig("myPlot.png")
76

You might also like