# -*- coding: utf-8 -*-
"""
Created on Wed Apr  1 03:40:12 2026

@author: pjaub
"""

import numpy as np
import matplotlib.pyplot as plt

plt.close('all')

# ==================================================
# 1) Fonction
# ==================================================
def f(x, y):
    return 20 - x**2 - y**2

# point de tangence
a, b = 2, 1
z0 = f(a, b)

# dérivées partielles au point (a,b)
fx = -2*a
fy = -2*b

# plan tangent
def plan_tangent(x, y):
    return z0 + fx*(x-a) + fy*(y-b)

# ==================================================
# 2) Grille pour la surface
# ==================================================
x = np.linspace(-4, 4, 200)
y = np.linspace(-4, 4, 200)
X, Y = np.meshgrid(x, y)
Z = f(X, Y)

# petite grille autour du point de contact pour le plan tangent
xp = np.linspace(a-2, a+2, 40)
yp = np.linspace(b-2, b+2, 40)
Xp, Yp = np.meshgrid(xp, yp)
Zp = plan_tangent(Xp, Yp)

# ==================================================
# 3) Sections
# ==================================================
t = np.linspace(-4, 4, 400)

# section y = b
z1 = f(t, b)
z1_tan = z0 + fx*(t-a)

# section x = a
z2 = f(a, t)
z2_tan = z0 + fy*(t-b)

# ==================================================
# 4) Figure avec 3 sous-figures
# ==================================================
fig = plt.figure(figsize=(15, 5))
gs = fig.add_gridspec(1, 3, width_ratios=[1, 3, 1])

# --------------------------------------------------
# Gauche : section y = b
# --------------------------------------------------
ax1 = fig.add_subplot(gs[0, 0])
ax1.plot(t, z1, color='blue', linewidth=2, label="section y = 1")
ax1.plot(t, z1_tan, '--', color='blue', linewidth=2, label="tangente")
ax1.scatter(a, z0, color='black')

ax1.set_title("Section par le plan y = 1")
ax1.set_xlabel("x")
ax1.set_ylabel("z")
ax1.set_xlim(-4, 4)
ax1.set_ylim(0, 20)
ax1.set_aspect(0.3)
ax1.grid()
ax1.legend()


# --------------------------------------------------
# Centre : surface + sections + plan tangent
# --------------------------------------------------
ax2 = fig.add_subplot(gs[0, 1], projection='3d')

# surface
ax2.plot_surface(X, Y, Z, cmap='viridis', alpha=0.7, edgecolor='none')

# plan tangent
ax2.plot_surface(Xp, Yp, Zp, cmap='autumn', alpha=0.6, edgecolor='none')

# section y = b
ax2.plot(t, b*np.ones_like(t), z1, color='blue', linewidth=3)

# section x = a
ax2.plot(a*np.ones_like(t), t, z2, color='green', linewidth=3)

# point de tangence
ax2.scatter(a, b, z0, color='red', s=60)

ax2.set_title("Surface et plan tangent")
ax2.set_xlabel("x")
ax2.set_ylabel("y")
ax2.set_zlabel("z")
ax2.view_init(elev=25, azim=-50)

# --------------------------------------------------
# Droite : section x = a
# --------------------------------------------------
ax3 = fig.add_subplot(gs[0, 2])
ax3.plot(t, z2, color='green', linewidth=2, label="section x = 2")
ax3.plot(t, z2_tan, '--', color='green', linewidth=2, label="tangente")
ax3.scatter(b, z0, color='black')

ax3.set_title("Section par le plan x = 2")
ax3.set_xlabel("y")
ax3.set_ylabel("z")
ax3.set_xlim(-4, 4)
ax3.set_ylim(0, 20)
ax3.set_aspect(0.3)
ax3.grid()
ax3.legend()


# --------------------------------------------------
plt.tight_layout()
plt.show()