def u_niteratif(n):
u = ...
k = 0
while k < = n:
u = ...
k = ...
return u
def u_nrecursif(n):
if n == 0:
return ...
else:
return ...
def suite_fibo(n):
a = 1
b = 1
for i in range(n):
u = a + b
b = a
a = u
return u
| \color{white}i | \color{white}a | \color{white}b | \color{white}u | |
|---|---|---|---|---|
| Ligne 2 | ||||
| Ligne 3 | ||||
| Premier passage dans la boucle | ||||
| Deuxième passage dans la boucle | ||||
| Troisième passage dans la boucle | ||||
| Quatrième passage dans la boucle | ||||
| Cinquième passage dans la boucle | ||||
| Sixième passage dans la boucle |
from math import sin, sqrt
def u(n):
return 7 - 5*sin(1/n) + 17/n**2
def S(n):
somme = 0
for i in range(1, n+1):
somme = somme + u(i)
return somme/n
def premiers_termes(v, n):
liste = []
for k in range(1, n+1):
liste.append(v(k))
return liste
print(premiers_termes(u, 100))
print(premiers_termes(S, 100))
def racine2_balayage():
compteur = 0
while compteur*compteur - 2 < 0:
compteur = compteur + 0.01
return compteur
from math import log, exp
def logarithme_dichotomie(x, precision = 0.01):
a = -100
b = 100
milieu = 0
while abs(a - b) > precision:
if exp(milieu) - x < 0:
...
else:
...
milieu = ...
return milieu
print(logarithme_dichotomie(2))
from math import*
from random import*
def f(x):
return x*(1 - x)
def cherche_max(fonction):
x_max = 0
y_max = fonction(x_max)
for i in range(100000):
a = random()
if fonction(a) > y_max:
y_max = fonction(a)
x_max = a
return x_max
print(cherche_max(f))
from random import random
def fonction(x):
return x*(3 - x)*(3 + x)
def monte_carlo(f, a, b, n):
somme = 0
for i in range(n):
somme = somme + f(a + (b - a)*random())
return somme/n
def rectangles(f, a, b, n):
x = points(a, b, n)
fx = images(f, x)
p = n*[(b-a)/n]
p = p + [0]
integrale = 0
for k in range(n+1):
integrale = integrale + p[k]*fx[k]
return integrale
def trapezes(f, a, b, n):
x = points(a, b, n)
fx = images(f, x)
p = (n - 1)*[(b - a)/n]
p = [0.5*(b - a)/n] + p + [0.5*(b - a)/n]
integrale = 0
for k in range(n + 1):
integrale = integrale + p[k]*fx[k]
return integrale
import matplotlib.pyplot as plt
def euler_explicite(h):
X = []
Y = []
x = 0
y = 1
while (x <= 1):
X.append(x)
Y.append(y)
x = ...
y = ...
return [X,Y]
def graphe(x,y):
plt.plot(x,y)
plt.show()
import matplotlib.pyplot as plt # on charge la fonction pyplot de matplotlib
from math import *
from numpy import linspace
def DLexp(x, n):
return 1 + x
def trace(N,n):
xs = linspace(-1, 1, N)
ys = [exp(x) for x in xs]
ya = [DLexp(x, n) for x in xs]
plt.plot(xs, ys)
plt.plot(xs, ya)
plt.savefig("figure.png")
from math import sqrt, sin
def sinus_amorti(x):
if (x == 0):
return 0
else:
return sin(x)/sqrt(x)
def fonction2liste(fonction, a, b, n):
x = []
y = []
for k in range(n + 1):
x_i = a + (b - a)*k/n
x.append(x_i)
y.append(fonction(x_i))
return [x, y]
def maxima_locaux(liste):
...
return ml
out = fonction2liste(sinus_amorti, 0, 20, 1000)
maxloc = maxima_locaux(out[1])
print(maxloc)
Nos manuels sont collaboratifs, n'hésitez pas à nous en faire part.
j'ai une idée !
Oups, une coquille