# -*- coding: utf-8 -*-
"""
Created on Wed Apr  1 03:38:29 2026

@author: pjaub
"""

import numpy as np
import matplotlib.pyplot as plt

plt.close('all')

# ==================================================
# 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
fx = -2*a
fy = -2*b

# ==================================================
# Grille surface
# ==================================================
x = np.linspace(-4, 4, 200)
y = np.linspace(-4, 4, 200)
X, Y = np.meshgrid(x, y)
Z = f(X, Y)

# ==================================================
# 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)

# ==================================================
# FIGURE AVEC 3 BLOCS
# ==================================================
fig = plt.figure(figsize=(14, 5))

# --------------------------------------------------
# 1) Section y = b (gauche)
# --------------------------------------------------
ax1 = fig.add_subplot(1, 3, 1)

ax1.plot(t, z1, color='blue', linewidth=2)
ax1.plot(t, z1_tan, '--', color='blue')

ax1.scatter(a, z0, color='black')
ax1.set_title("Section y = 1")
ax1.set_xlabel("x")
ax1.set_ylabel("z")
ax1.grid()

# --------------------------------------------------
# 2) Surface (centre)
# --------------------------------------------------
ax2 = fig.add_subplot(1, 3, 2, projection='3d')

ax2.plot_surface(X, Y, Z, cmap='viridis', alpha=0.75, edgecolor='none')

# tracer les deux sections dans la surface
ax2.plot(t, b*np.ones_like(t), z1, color='blue', linewidth=3)
ax2.plot(a*np.ones_like(t), t, z2, color='green', linewidth=3)

ax2.scatter(a, b, z0, color='red', s=60)

ax2.set_title("Surface")
ax2.set_xlabel("x")
ax2.set_ylabel("y")
ax2.set_zlabel("z")
ax2.view_init(elev=25, azim=-50)

# --------------------------------------------------
# 3) Section x = a (droite)
# --------------------------------------------------
ax3 = fig.add_subplot(1, 3, 3)

ax3.plot(t, z2, color='green', linewidth=2)
ax3.plot(t, z2_tan, '--', color='green')

ax3.scatter(b, z0, color='black')
ax3.set_title("Section x = 2")
ax3.set_xlabel("y")
ax3.set_ylabel("z")
ax3.grid()

# --------------------------------------------------
plt.tight_layout()
plt.show()