Gradient Approximation in Higher Dimensions¶
Finite difference generalizes readily to multiple dimensions by computing the directional partial derivatives individually:
where is the unit basis vector along coordinate dimension .
The full gradient vector combines these directional components:
pointing in the direction of steepest ascent with magnitude equal to the maximum directional rate of change.
Boundary cases are handled similarly, but with the bounded coordinate evaluated using forward or backward differences.
import numpy as np
import plotly.graph_objects as go
def ugly_function(x, y):
return (np.sin(x) * np.cos(y) + np.cos(x) * np.sin(y)) * np.exp(-(x**2 + y**2) / 10)
x = np.linspace(-5, 5, 50)
y = np.linspace(-5, 5, 50)
X, Y = np.meshgrid(x, y)
Z = ugly_function(X, Y)
dx = x[1] - x[0]
dy = y[1] - y[0]
dZdx = np.gradient(Z, dx, axis=1)
dZdy = np.gradient(Z, dy, axis=0)
fig = go.Figure()
fig.add_trace(go.Surface(x=X, y=Y, z=Z, colorscale="Viridis", opacity=0.85, showscale=False))
arrow_x = X[::5, ::5].flatten()
arrow_y = Y[::5, ::5].flatten()
arrow_z = Z[::5, ::5].flatten()
arrow_u = .5*dZdx[::5, ::5].flatten()
arrow_v = .5*dZdy[::5, ::5].flatten()
fig.add_trace(go.Cone(
x=arrow_x, y=arrow_y, z=arrow_z,
u=arrow_u, v=arrow_v, w=np.zeros_like(arrow_u),
sizemode="absolute", sizeref=0.2, showscale=False, colorscale="Blues"
))
fig.update_layout(
title="Surface f(x, y) with Gradient Cones",
height=550,
margin=dict(l=10, r=10, b=10, t=50)
)
fig.show()