]> AND Private Git Repository - cours-mesi.git/blob - tps/chap3/jjm.py
Logo AND Algorithmique Numérique Distribuée

Private GIT Repository
j
[cours-mesi.git] / tps / chap3 / jjm.py
1 from math import *
2
3 def f(x):
4     return cos(x)-x
5
6
7 def iteration_lagrange(x0, q0, m, epsilon, f):
8     def maj_test(xn,xnm1):
9         return f(xn)!=0 and abs(xn-xnm1)>epsilon
10
11     n=0
12     test=f(x0)!=0
13     X=[x0]
14     xnm1=x0
15     xn=float(x0-f(x0))/q0
16     qn=q0
17     print(xn,q0)
18     while n<=m and test:
19         qn=float((f(xn)-f(xnm1)))/(xn-xnm1)
20         xnm1=xn
21         xn=xnm1-f(xnm1)/qn
22
23         X+=[xn]
24         test=maj_test(xn,xnm1)
25         n+=1
26     return (n,X)
27
28 print(iteration_lagrange(pi/4,1,200,0.0000001,f))
29
30
31 def iteration_lagrange(x0, x1, m, epsilon, f):
32     def maj_test(xn,xnm1):
33         return f(xn)!=0 and abs(xn-xnm1)>epsilon
34
35     n=2
36     test=f(x0)!=0
37     X=[x0,x1]
38     xnm1=x0
39     xn=x1
40     while n<=m and test:
41         qn=float((f(xn)-f(xnm1)))/(xn-xnm1)
42         xnm1=xn
43         xn=xnm1-f(xnm1)/qn
44
45         X+=[xn]
46         test=maj_test(xn,xnm1)
47         n+=1
48     return (n,X)
49
50
51
52 print(iteration_lagrange(0,pi/4,200,0.0000001,f))