# Plot a level curve and its gradient

Suppose that I have the following ellipse function,

$$f(x,y)=4x^2+y^2-5$$.

The gradient of this ellipse is calculated as

$$\nabla f(x,y)=[8x,2y]$$.

I know how to plot and join them. It is easy. I do the following,

P1 = ContourPlot[4 x^2 + y^2 == 5 , {x, -3, 3}, {y, -3, 3},
AspectRatio -> Automatic]
P2 = VectorPlot[{8 x, 2 y}, {x, -3, 3}, {y, -3, 3}]
Show[P1, P2]


This gives me the following. But, What I would like to do is to see the gradient vectors only on the perimeter of my ellipse not everywhere. I tried to reduce the range of the variables in VectorPlot but it doesn't help. Does anyone have a suggestion?

• Something like P2 = VectorPlot[If[4 x^2 + y^2 <= 5, {8 x, 2 y}, {0, 0}], {x, -3, 3}, {y, -3, 3}] ? – ctrl Sep 28 '18 at 13:30
• Thanks for your answer. But I want it just on the outer surface of the ellipse, in other words on its perimeter – KratosMath Sep 28 '18 at 13:31
• You could draw you own arrows like this: Show[Graphics[Table[Arrow[{{xPos[t], yPos[t]}, {xPos[t], yPos[t]} + .05 {8 xPos[t], 2 yPos[t]}}], {t,0, 2 Pi, .1}]], P1] – ctrl Sep 28 '18 at 13:52

Utilizing this post for creating Evenly spaced points on boundary of polygon, we can do this as follows:

f = {x, y} \[Function] 4 x^2 + y^2;
gradf = {x, y} \[Function] Evaluate[D[f[x, y], {{x, y}, 1}]];
P1 = ContourPlot[f[x, y] == 5, {x, -3, 3}, {y, -3, 3}];

numarrows = 100;

(*extracting the coordinates from the ContourPlot*)
pts = Cases[P1, _GraphicsComplex, ∞][[1, 1]];

(*creating evenly distributed points on the ContourPlot (does only work for a single closed contour!)*)
t = Prepend[Accumulate[Norm /@ Differences[pts]], 0.];
γ = Interpolation[Transpose[{t, pts}], InterpolationOrder -> 1, PeriodicInterpolation -> True];
s = Subdivide[γ[[1, 1, 1]], γ[[1, 1, 2]], numarrows];
newpts = γ[s];

scale = 0.1;
P2 = Graphics[ Arrow[Most@Transpose[{newpts, newpts + scale gradf @@@ newpts}]]];

Show[P1, P2] • very nice answer with a very nice explanation. Thanks – KratosMath Sep 28 '18 at 14:29
• You're welcome! – Henrik Schumacher Sep 28 '18 at 14:29
• @HenrikSchumacher In your Cases[P1, _GraphicsComplex, All] should the All be an Infinity? – That Gravity Guy Sep 28 '18 at 18:24
• @ThatGravityGuy Yeah maybe. As of version 11.3, All does also work. – Henrik Schumacher Sep 28 '18 at 18:25
• Ah. I work in 8.0 and 10.1. Though the online documentation doesn't seem to say it has that functionality yet. Just the Infinity level specification. – That Gravity Guy Sep 28 '18 at 20:08
ClearAll[gradF, f]

Use "ArcLength" as MeshFunctions and post-process points into arrows:
Normal[ContourPlot[f[x, y] == c , {x, -3, 3}, {y, -3, 3}, 