Código
import numpy as np
import plotly.graph_objects as go
f = lambda x, y: 6 - x**2 - y**2
a, b = 1.0, 1.0 # ponto onde medimos a derivada parcial
fig = go.Figure()
# superfície z = f(x, y): a cúpula, semitransparente para enxergar o corte
grid = np.linspace(-2.2, 2.2, 60)
X, Y = np.meshgrid(grid, grid)
fig.add_surface(x=X, y=Y, z=f(X, Y), colorscale="Blues", opacity=0.55,
showscale=False, name="z = f(x, y)")
# plano de corte y = b (a fatia vertical que gera a curva)
xp, zp = np.linspace(-2.2, 2.2, 2), np.linspace(-1.5, 6.5, 2)
Xp, Zp = np.meshgrid(xp, zp)
fig.add_surface(x=Xp, y=np.full_like(Xp, b), z=Zp, opacity=0.22,
showscale=False, colorscale=[[0, "#ff7f0e"], [1, "#ff7f0e"]])
# curva-fatia z = f(x, b): interseção da superfície com o plano
xs = np.linspace(-2.2, 2.2, 120)
fig.add_scatter3d(x=xs, y=np.full_like(xs, b), z=f(xs, b), mode="lines",
line=dict(color="#d62728", width=6), name="curva-fatia z = f(x, b)")
# reta tangente à curva-fatia em x = a; inclinação fx(a, b) = -2a
inc = -2 * a
xt = np.array([a - 1.1, a + 1.1])
fig.add_scatter3d(x=xt, y=np.full_like(xt, b), z=f(a, b) + inc * (xt - a),
mode="lines", line=dict(color="black", width=5),
name=f"tangente, inclinação {inc:.0f}")
# ponto de tangência (a, b, f(a, b))
fig.add_scatter3d(x=[a], y=[b], z=[f(a, b)], mode="markers",
marker=dict(color="black", size=5), name="(a, b, f(a, b))")
fig.update_layout(
template="plotly_white", height=560,
scene=dict(xaxis_title="x", yaxis_title="y", zaxis_title="z",
camera=dict(eye=dict(x=1.6, y=-1.6, z=1.0))),
margin=dict(l=0, r=0, t=0, b=0),
legend=dict(orientation="h", y=0.02),
)
fig.show()A superfície z = 6 − x² − y² cortada pelo plano y = 1. A interseção é a curva-fatia vermelha z = 5 − x²; a reta preta é sua tangente em x = 1, cuja inclinação é a derivada parcial fx(1, 1) = −2. Arraste para girar.