-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathfonctions_surface_5.py
More file actions
59 lines (45 loc) · 1.25 KB
/
fonctions_surface_5.py
File metadata and controls
59 lines (45 loc) · 1.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
N = 100 # Taille de la grille : NxN
# Recherche dans un carré [0,6]x[0,6]
c = 6.0
VX = np.linspace(0, c, N)
VY = np.linspace(0, c, N)
X,Y = np.meshgrid(VX, VY)
def f(x,y):
# distance au carré
# z = (x-1)**2+ (y-2)**2 + (x-3)**2 + (y-5)**2 + (x-6)**2 + (y-1)**2
# distance norme 1
# z = np.abs(x-1)+ np.abs(y-2) + np.abs(x-3) + np.abs(y-5) + np.abs(x-6) + np.abs(y-1)
# distance norme 2
z = np.sqrt((x-1)**2+ (y-2)**2) + np.sqrt((x-3)**2 + (y-5)**2) + np.sqrt((x-6)**2 + (y-1)**2)
return z
Z = f(X,Y)
Ztest = f(2.88, 2.99)
print('Z =', Ztest)
# Minimum sur la grille
print(VX)
print(Z)
print(np.argmin(Z))
imin, jmin = np.unravel_index(np.argmin(Z, axis=None), Z.shape)
print("--- Minimum sur la grille ---")
print("Taille de la grille :",N,"x",N)
print("Nombre de points :",N**2)
print(X[imin,jmin],Y[imin,jmin],Z[imin,jmin])
# Points au hasard
K = 1000
zmin = +np.inf # infini
for __ in range(K):
x = c*np.random.random()
y = c*np.random.random()
z = f(x,y)
if z < zmin:
xmin = x
ymin = y
zmin = z
print("--- Minimum au hasard ---")
print("Nombre de points tirés :",K)
print(xmin,ymin,zmin)
# Par les tranches
# voir fichier 'fonctions_surface.6.sage'