The Gradient Vector is Perpendicular to the Level Curve
Clear[f,x,y] f[x_,y_] = 5 - (x^2 + x y + y^2)
Here is a plot of some level curves f[x,y] = c.
a = -1;
b = 1;
levelcurves = ContourPlot[Evaluate[f[x,y]],{x,a,b},{y,a,b},
ContourSmoothing->Automatic,
AxesLabel->{"x","y"}];The lighter shading indicates larger values of f[x,y].
Here is the 3D plot of the surface z = f[x,y]:
surfaceplot =
ParametricPlot3D[{x,y,f[x,y]},{x,a,b},{y,a,b},
AxesLabel->{"x","y","f[x,y]"}];
The level curves are plots at the height f[x,y]=c.
Here is f[x,y] = 4.5:
c = 4.5;
Show[levelcurves,Contours->{c},ContourShading->False];
Here is the plot of the intersection of the surface and the plane z = 4.5:
c = 4.5;
plane = ParametricPlot3D[{x,y,c},{x,a,b},{y,a,b},
PlotPoints->{2,2},DisplayFunction->Identity];
Show[surfaceplot,plane,DisplayFunction->$DisplayFunction];
Consider f(x,y) = 2 x^2 + x y + y^2.
Clear[x,y,f] f[x_,y_] = 2 x^2 + x y + y^2; grad2[f][x,y]
Here is a part of the level curve f[x,y] = f[1.5,2].
levelcurve = ContourPlot[f[x,y],{x,0,3},{y,1,4},
Contours->{f[1.5,2]},ContourShading->False,
DisplayFunction->$DisplayFunction];These next two will plot the gradient vector.
Clear[x,y,t]
x0=1.5;
y0=2;
{x1,y1}=grad2[f][x,y]/.{x->1.5,y->2};
p0={x0,y0};
p1={x1,y1};
x[t_]:=x0+t(x1-x0)
y[t_]:=y0+t(y1-y0)
gradient=ParametricPlot[{x[t],y[t]},{t,0,1}]This plots the level curve and the gradient at (1.5,2)
Show[levelcurve,gradient,PlotRange->All,
AspectRatio->Automatic,AxesLabel->{"x","y"},
DisplayFunction->$DisplayFunction];
Well...
The gradient vector is perpendicular to the level curve!
Proof:
If f[x,y] = c is your level curve and you give it a parametrization r[t]=x[t]i+y[t]j, then g[t]=f[x[t],y[t]]=c.
But then g'[t] = 0
By the chain rule g'[t] = gradf[x[t],y[t]].{x'[t],y'[t]}.
So gradf[x[t],y[t]].{x'[t],y'[t]} = 0.
This means that the gradient is perpendicular to the tangent vector at that point.
By the way, we cannot visualize it here, but this works for any dimension.
Up to The Gradient