1 import matplotlib.pyplot as plt
18 def iteration_dichotomie(a,b,m,epsilon,f):
19 def maj_test(xn,xnm1,n,m):
20 return f(xn) == 0 or abs(xnm1-xn) <= epsilon or n >= m
27 test = maj_test(xn,xnm1,n,m)
37 test = maj_test(xn,xnm1,n,m)
40 def iteration_newton(x0,m,epsilon,f,fp):
41 def maj_test(xn,xnm1,epsilon,n,m,f):
42 return f(xn) == 0 or abs(xnm1-xn) <= epsilon or n >= m
46 test = maj_test(xn,xn+2*epsilon,epsilon,n,m,f)
53 test= maj_test(xn,xnm1,epsilon,n,m,f)
57 def iteration_corde(a,b,x0,m,epsilon,f):
58 def maj_test(xn,xnm1,n,m,f):
59 return f(xn)== 0 or abs(xnm1-xn) <= epsilon or n >= m
61 q=float(f(b)-f(a))/(b-a)
64 test= maj_test(xn,xnm1,n,m,f)
71 test= maj_test(xn,xnm1,n,m,f)
75 def iteration_lagrange(x0,x1,m,epsilon,f):
77 delta=float(x1-x0)/(f(x1)-f(x0))
84 delta=float(x-xm1)/(f(x)-f(xm1))
87 test= not (f(x)==0 or n>=m or abs(x-xm1)<=epsilon)
90 def iteration_muller(x0,x1,x2,m,epsilon,f):
91 def maj_test(xn,xnm1):
92 return f(xn) != 0 and abs(xnm1-xn) > epsilon
100 # calcul des coefs an, bn et cn
101 an = float(f(xn))/((xn-xnm1)*(xn-xnm2))+float(f(xnm1))/((xnm1-xn)*(xnm1-xnm2))+float(f(xnm2))/((xnm2-xn)*(xnm2-xnm1))
102 bn = -float(f(xn)*(xnm1+xnm2))/((xn-xnm1)*(xn-xnm2))-float(f(xnm1)*(xn+xnm2))/((xnm1-xn)*(xnm1-xnm2))-float(f(xnm2)*(xn+xnm1))/((xnm2-xn)*(xnm2-xnm1))
103 cn = float(f(xn)*xnm1*xnm2)/((xn-xnm1)*(xn-xnm2))+float(f(xnm1)*xn*xnm2)/((xnm1-xn)*(xnm1-xnm2))+float(f(xnm2)*xn*xnm1)/((xnm2-xn)*(xnm2-xnm1))
106 xnp = float(-bn - sqrt(dn))/(2*an)
107 xnpp = float(-bn + sqrt(dn))/(2*an)
108 xnp1 = xnp if abs(xnp-xn)<abs(xnpp-xn) else xnpp
113 test= maj_test(xn,xnm1)
115 #f(x) !=0 and n<m and abs(x-xm1)>epsilon
122 print "TP 3.1 ............ dichotomie"
123 print iteration_dichotomie(0,pi/2,45,1E-9,f)
125 print "TP 3.1 ............ corde"
126 print iteration_corde(0,pi/2,0,200,1E-9,f)
128 print "TP 3.1 ............ newton"
129 print iteration_newton(0,200,1E-9,f,fp)
134 print "TP 3.1 ............ lagrange"
135 print iteration_lagrange(0,pi/2,200,0.00000001,f)
137 print "TP 3.1 ............ muller"
138 print iteration_muller(0,pi/4,pi/2,200,0.00000001,f)
141 if __name__ == '__main__':