X-Git-Url: https://bilbo.iut-bm.univ-fcomte.fr/and/gitweb/cours-mesi.git/blobdiff_plain/d1778cd1db87ac86b892c6d65935e6ee1abc95a3..2db144d6141d827fa394d04e7e4ef6771c7089ee:/tps/chap3/methodes.py diff --git a/tps/chap3/methodes.py b/tps/chap3/methodes.py index 8ae3b71..9c9424e 100644 --- a/tps/chap3/methodes.py +++ b/tps/chap3/methodes.py @@ -16,79 +16,61 @@ def fp(x): def iteration_dichotomie(a,b,m,epsilon,f): - def maj_test(xn,xnm1): - return f(xn) != 0 and abs(xnm1-xn) > epsilon - xnm1 = a - xn= a + def maj_test(xn,xnm1,n,m): + return f(xn) == 0 or abs(xnm1-xn) <= epsilon or n >= m + xn= float(a) + xnm1 =float(b) X=[] - n = 1 - an= a - bn=b - test = True - while n <= m and test: + n = 0 + an= float(a) + bn=float(b) + test = maj_test(xn,xnm1,n,m) + while not test: xnm1 = xn xn=float(an+bn)/2 - test = maj_test(xn,xnm1) X +=[xn] if f(an)*f(xn)<=0 : bn=xn else : an=xn n +=1 + test = maj_test(xn,xnm1,n,m) return (n,X) def iteration_newton(x0,m,epsilon,f,fp): - def maj_test(xn,xnm1): - return f(xn) != 0 and abs(xnm1-xn) > epsilon - n=0; - test= f(x0) != 0 - xn=x0 - X=[x0] - while n < m and test: + def maj_test(xn,xnm1,epsilon,n,m,f): + return f(xn) == 0 or abs(xnm1-xn) <= epsilon or n >= m + n = 0 + xn = float(x0) + X = [x0] + test = maj_test(xn,xn+2*epsilon,epsilon,n,m,f) + while not test: qn=fp(xn) xnm1=xn xn= xn-f(xn)/qn X += [xn] n=n+1 - test= maj_test(xn,xnm1) - -#f(x) !=0 and nepsilon + test= maj_test(xn,xnm1,epsilon,n,m,f) return (n,X) def iteration_corde(a,b,x0,m,epsilon,f): - def maj_test(xn,xnm1): - return f(xn) != 0 and abs(xnm1-xn) > epsilon - n=0; + def maj_test(xn,xnm1,n,m,f): + return f(xn)== 0 or abs(xnm1-xn) <= epsilon or n >= m + n=0 q=float(f(b)-f(a))/(b-a) - test= f(x0) != 0 - xn=x0 - X=[x0] - while n < m and test: + xnm1 = float(b) + xn=float(x0) + test= maj_test(xn,xnm1,n,m,f) + X=[float(x0)] + while not test: xnm1=xn - xn= xn-f(xn)/q + xn= xn-float(f(xn))/q X += [xn] n=n+1 - test= maj_test(xn,xnm1) - -#f(x) !=0 and nepsilon + test= maj_test(xn,xnm1,n,m,f) return (n,X) -"""def iteration_newton(x0,m,epsilon,f,fp): - n=0; - delta=float(1)/fp(x0) - test= f(x0) != 0 - x=x0 - X=[x0] - while(test): - xm1=x - x= x-delta*f(x) - delta=float(1)/fp(x) - X += [x] - n=n+1 - test= not (f(x)==0 or n>=m or abs(x-xm1)<=epsilon) - return (n,X) -""" def iteration_lagrange(x0,x1,m,epsilon,f): n=0; @@ -138,24 +120,24 @@ def iteration_muller(x0,x1,x2,m,epsilon,f): def main(): print "TP 3.1 ............ dichotomie" - print iteration_dichotomie(0,pi/2,200,0.00000001,f) + print iteration_dichotomie(0,pi/2,45,1E-9,f) - print "TP 3.1 ............ corde" - print iteration_corde(0,pi/2,0,200,0.00000001,f) - + print iteration_corde(0,pi/2,0,200,1E-9,f) print "TP 3.1 ............ newton" - print iteration_newton(0,200,0.00000001,f,fp) + print iteration_newton(0,200,1E-9,f,fp) + +""" print "TP 3.1 ............ lagrange" print iteration_lagrange(0,pi/2,200,0.00000001,f) print "TP 3.1 ............ muller" print iteration_muller(0,pi/4,pi/2,200,0.00000001,f) - +""" if __name__ == '__main__': main()