import matplotlib.pyplot as plt
import numpy as np
from mpl_toolkits.mplot3d import Axes3D
##
# Parabaloïde Hyperbolique
def f(x,y) : return x*x - y*y

X = np.arange(-1,1,0.2)
Y = np.arange(-1,1,0.2)
X,Y = np.meshgrid(X,Y)
Z = f(X,Y)
ax = Axes3D(plt.figure())
ax.plot_surface(X,Y,Z)
plt.show()

X = np.arange(-1,1,0.02)
Y = np.arange(-1,1,0.02)
X,Y = np.meshgrid(X,Y)
Z = f(X,Y)
plt.axis('equal')
plt.grid()
plt.contour(X,Y,Z,33)
plt.show()
##
# Paraboloïde Elliptique
def f(x,y) : return x*x + y*y + x*y

X = np.arange(-1,1,0.02)
Y = np.arange(-1,1,0.02)
X,Y = np.meshgrid(X,Y)
Z = f(X,Y)
plt.axis('equal')
plt.grid()
plt.contour(X,Y,Z,np.arange(0.05,0.5,0.05))
plt.show()

X = np.arange(-1,1,0.2)
Y = np.arange(-1,1,0.2)
X,Y = np.meshgrid(X,Y)
Z = f(X,Y)
ax = Axes3D(plt.figure())
ax.plot_surface(X,Y,Z)
plt.show()
##
# Trois plateaux
def f(x,y) :
    return np.arctan(x) + np.arctan(y) - np.arctan((x+y)/(1-x*y))

X = np.arange(-3,3,0.03)
Y = np.arange(-3,3,0.03)
X,Y = np.meshgrid(X,Y)
Z = f(X,Y)
ax = Axes3D(plt.figure())
ax.plot_surface(X,Y,Z)
plt.show()
##
# Exemple de Peano
def f(x,y) : return 3*x**4 - 4*x*x*y + y*y

X = np.arange(-1,1,0.001)
Y = np.arange(-0.5,1,0.001)
X,Y = np.meshgrid(X,Y)
Z = f(X,Y)
plt.axis('equal')
plt.grid()
plt.contour(X,Y,Z,[-0.2,0,0.2], colors=['b','r','g'])
plt.show()

X = np.arange(-1,1,0.1)
Y = np.arange(-0.5,1,0.1)
X,Y = np.meshgrid(X,Y)
Z = f(X,Y)
ax = Axes3D(plt.figure())
ax.plot_surface(X,Y,Z)
plt.show()
##
# Exercice 2
def f(x,y) :
    return x*np.exp(y) + y*np.exp(x)

X = np.arange(-2,0,0.1)
Y = np.arange(-2,0,0.1)
X,Y = np.meshgrid(X,Y)
Z = f(X,Y)
ax = Axes3D(plt.figure())
ax.plot_surface(X,Y,Z,cmap='coolwarm')
plt.show()

X = np.arange(-2,0,0.02)
Y = np.arange(-2,0,0.02)
X,Y = np.meshgrid(X,Y)
Z = f(X,Y)
plt.axis('equal')
plt.grid()
plt.contour(X,Y,Z,99,cmap='coolwarm')
plt.show()
##
# Exercice 3
def f(x,y):
    if x+y<1: return x*y*np.sqrt(1-x-y)
    else: return 0
f = np.vectorize(f)

ax = Axes3D(plt.figure())
X = np.arange(0,1,0.01)
Y = np.arange(0,1,0.01)
X,Y = np.meshgrid(X, Y)
Z = f(X,Y)
ax.plot_surface(X,Y,Z)
plt.show()

X = np.arange(0,1,0.002)
Y = np.arange(0,1,0.002)
X,Y = np.meshgrid(X,Y)
Z = f(X,Y)
plt.axis('equal')
plt.grid()
plt.contour(X,Y,Z,99)
plt.show()
##
# Exercice 7
def f(x,y) : return x*x*y*y/(1+x*x)/(1+y*y)/(x*x+y*y)

X = np.arange(-2,2,0.1)
Y = np.arange(-2,2,0.1)
X,Y = np.meshgrid(X,Y)
Z = f(X,Y)
ax = Axes3D(plt.figure())
ax.plot_surface(X,Y,Z,cmap='coolwarm')
plt.show()

X = np.arange(-2,2,0.02)
Y = np.arange(-2,2,0.02)
X,Y = np.meshgrid(X,Y)
Z = f(X,Y)
plt.axis('equal')
plt.grid()
plt.contour(X,Y,Z,np.arange(0,0.13,0.01))
plt.show()
##
# Exercice 8
# a)
def f(x,y) : return (2*x*x+3*y*y)*np.exp(-x*x-y*y)
X = np.arange(-2,2,0.02)
Y = np.arange(-2,2,0.02)
X,Y = np.meshgrid(X,Y)
Z = f(X,Y)
ax = Axes3D(plt.figure())
ax.plot_surface(X,Y,Z)
plt.show()
# e)
plt.axis('equal')
plt.grid()
c = plt.contour(X,Y,Z,[0.2,0.3,0.4,0.5,0.6,0.7,0.75,0.8,0.9,1,1.1])
plt.clabel(c)
plt.show()
# plus !
plt.axis('equal')
plt.grid()
plt.contour(X,Y,Z,np.arange(0.1,1.1,0.02), colors='grey')
plt.show()
#f)
print(2*np.exp(-1))
print(2*np.exp(-2/3))
print(3*np.exp(-1))
##
# Exercice 9
def f(x,y) : return 1/x + x*y + 1/y

X = np.arange(0.5,2,0.1)
Y = np.arange(0.5,2,0.1)
X,Y = np.meshgrid(X,Y)
Z = f(X,Y)
ax = Axes3D(plt.figure())
ax.plot_surface(X,Y,Z,cmap='coolwarm')
plt.show()

X = np.arange(0.2,3,0.02)
Y = np.arange(0.2,3,0.02)
X,Y = np.meshgrid(X,Y)
Z = f(X,Y)
plt.axis('equal')
plt.grid()
plt.contour(X,Y,Z,99,cmap='coolwarm')
plt.show()
##
# Exercice 10
def f(x,y) :
    if x>y : return y*(1-x)
    else : return x*(1-y)
f=np.vectorize(f)
X = np.arange(0,1,0.02)
Y = np.arange(0,1,0.02)
X,Y = np.meshgrid(X,Y)
Z = f(X,Y)
ax = Axes3D(plt.figure())
ax.plot_surface(X,Y,Z,cmap='coolwarm')
plt.show()


