0% found this document useful (0 votes)
40 views2 pages

Zero Fonction - Py

Uploaded by

Yasser Rghaoui
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views2 pages

Zero Fonction - Py

Uploaded by

Yasser Rghaoui
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

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

2 # UIASS > CPGE > MPSI > [Link]@[Link]


3 #~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=
4 #
5 # Zero de fonction
6 #
7 #~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=
8 import numpy as np
9 import [Link] as spo
10 import [Link] as plt
11 #~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.
12 def f( x ): return (x-3)**3+4
13 #return (x*x+ [Link](9*x))*[Link](x)-2+1/[Link](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 ## [Link](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 ## [Link](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 = [Link](0, f(x), 25)
44 ## [Link]([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 = [Link](0, f(x), 25)
53 ## [Link]([x]*len(W), W, "--", color="orange")
54 #~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.
55 fig, ax = [Link]()
56 [Link](True, which='both')
57
58 [Link][ 'left'].set_position('zero') ; [Link]['right'].set_color('none')
59 [Link]['bottom'].set_position('zero') ; [Link][ 'top'].set_color('none')
60 [Link].tick_left() ; [Link].tick_bottom()
61
62 a, b = 0, 10 # a, b = -[Link], [Link] #a, b = 0, 2.4
63 X = [Link]( a, b, 200 ) ; Y = f(X) ; [Link](X, Y, lw=2, color="blue")
64
65 x0 = dichotomie(f, a, b) ; [Link](x0, f(x0), "*", lw=3, color="red")
66 x0 = [Link](f, a, b) ; [Link](x0, f(x0), "*", lw=5, color="pink")
67
68 x1 = newton(f, b, fp) ; [Link](x1, f(x1), "D", lw=3, color="green")
69 x1 = [Link](f, b, fp) ; [Link](x1, f(x1), "D", lw=5, color="lime")
70
71 x2 = Sequante(f, a, b) ; [Link](x2, f(x2), "o", lw=3, color="cyan")
72 x2 = [Link](f, a) ; [Link](x2, f(x2), "o", lw=5, color="purple")
73
74 x3 = Regula_Falsi(f, a, b) ; [Link](x3, f(x3), "s", lw=3, color="orange")
75 [Link]() # [Link]("[Link]")
76

You might also like