1 import matplotlib.pyplot as plt
18 def iteration_dichotomie(a,b,m,epsilon,f):
19 def maj_test(xn,xnm1):
20 return f(xn) != 0 and abs(xnm1-xn) > epsilon
28 while n <= m and test:
31 test = maj_test(xn,xnm1)
40 def iteration_newton(x0,m,epsilon,f,fp):
41 def maj_test(xn,xnm1):
42 return f(xn) != 0 and abs(xnm1-xn) > epsilon
53 test= maj_test(xn,xnm1)
55 #f(x) !=0 and n<m and abs(x-xm1)>epsilon
59 def iteration_corde(a,b,x0,m,epsilon,f):
60 def maj_test(xn,xnm1):
61 return f(xn) != 0 and abs(xnm1-xn) > epsilon
63 q=float(f(b)-f(a))/(b-a)
72 test= maj_test(xn,xnm1)
74 #f(x) !=0 and n<m and abs(x-xm1)>epsilon
77 """def iteration_newton(x0,m,epsilon,f,fp):
89 test= not (f(x)==0 or n>=m or abs(x-xm1)<=epsilon)
93 def iteration_lagrange(x0,x1,m,epsilon,f):
95 delta=float(x1-x0)/(f(x1)-f(x0))
102 delta=float(x-xm1)/(f(x)-f(xm1))
105 test= not (f(x)==0 or n>=m or abs(x-xm1)<=epsilon)
108 def iteration_muller(x0,x1,x2,m,epsilon,f):
109 def maj_test(xn,xnm1):
110 return f(xn) != 0 and abs(xnm1-xn) > epsilon
117 while n < m and test:
118 # calcul des coefs an, bn et cn
119 an = float(f(xn))/((xn-xnm1)*(xn-xnm2))+float(f(xnm1))/((xnm1-xn)*(xnm1-xnm2))+float(f(xnm2))/((xnm2-xn)*(xnm2-xnm1))
120 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))
121 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))
124 xnp = float(-bn - sqrt(dn))/(2*an)
125 xnpp = float(-bn + sqrt(dn))/(2*an)
126 xnp1 = xnp if abs(xnp-xn)<abs(xnpp-xn) else xnpp
131 test= maj_test(xn,xnm1)
133 #f(x) !=0 and n<m and abs(x-xm1)>epsilon
140 print "TP 3.1 ............ dichotomie"
141 print iteration_dichotomie(0,pi/2,200,0.00000001,f)
144 print "TP 3.1 ............ corde"
145 print iteration_corde(0,pi/2,0,200,0.00000001,f)
148 print "TP 3.1 ............ newton"
149 print iteration_newton(0,200,0.00000001,f,fp)
152 print "TP 3.1 ............ lagrange"
153 print iteration_lagrange(0,pi/2,200,0.00000001,f)
155 print "TP 3.1 ............ muller"
156 print iteration_muller(0,pi/4,pi/2,200,0.00000001,f)
159 if __name__ == '__main__':