8
$\begingroup$

For didactic purposes (a line integral of a vector field) I'd like to plot a vector field along a curve in 2D and 3D, like in this picture:

Enter image description here

Mathematica is able to vizualize vector fields.

Here is my unsuccessful attempt

VectorPlot[{-1 - x^2 + y, 1 + x - y^2}, {x, -3, 3}, {y, -3, 3},
RegionFunction -> Function[{x, y}, 1 <= x^2 + y^2 <= 1]]

Enter image description here

I know this and this application to this end, but those are not it. An analog of the PlotPositionVector command of Maple is required.

$\endgroup$
5
  • 1
    $\begingroup$ for 3D see this Q/A $\endgroup$
    – kglr
    Mar 24 at 7:27
  • $\begingroup$ @kglr: I need not TNB, but a field plot along a curve, not along a knot. Thank you anyway. $\endgroup$
    – user64494
    Mar 24 at 7:32
  • 1
    $\begingroup$ field[{x_, y_}] = {-1 - x^2 + y, 1 + x - y^2}; Show[VectorPlot[Evaluate[field[{x, y}]], {x, -4, 4}, {y, -4, 4}, VectorScaling -> True, VectorPoints -> Table[{Cos[t], Sin[t]}, {t, 0, 2 \[Pi], .1}]], Graphics[Circle[{0, 0}, {1, 1}]], AspectRatio -> Automatic] /. Arrow[{{x_, y_}, {z_, w_}}] :> Arrow[{{(x + z)/2, (y + w)/2}, {z, w}}] $\endgroup$
    – cvgmt
    Mar 24 at 8:35
  • 1
    $\begingroup$ field[{x_, y_}] = {-1 - x^2 + y, 1 + x - y^2}; Show[ VectorPlot[Evaluate[field[{x, y}]], {x, -4, 4}, {y, -4, 4}, VectorScaling -> True, VectorPoints -> Table[{Sin[u], Sin[2 u]}, {u, 0, 2 \[Pi], .1}]] /. Arrow[{{x_, y_}, {z_, w_}}] :> Arrow[{{(x + z)/2, (y + w)/2}, {z, w}}], ParametricPlot[{Sin[u], Sin[2 u]}, {u, 0, 2 Pi}], AspectRatio -> Automatic] $\endgroup$
    – cvgmt
    Mar 24 at 8:39
  • $\begingroup$ @cvgmt: Thank you. Can you present this as an answer, adding 3d case? TIA. $\endgroup$
    – user64494
    Mar 24 at 9:06
11
$\begingroup$
vf[{x_, y_}] = {-1 - x^2 + y, 1 + x - y^2};

curve[t_] := Sqrt[t] {Cos[t], Sin[t]};

Using ParametricPlot:

scale = .2;

ParametricPlot[curve[t], {t, 0, 3 Pi}, PlotStyle -> Thick, 
  Epilog -> {Red, Thick, 
      Arrow[{curve @ #, curve @ # + (scale Norm[#] Normalize[#] & @ vf[curve@#])}] & /@ 
        Rest[Subdivide[0, 3 Pi, 50]]}, PlotRange -> {-5, 5}] 

enter image description here

Using VectorPlot:

VectorPlot[Evaluate @ vf[{x, y}], {x, -5, 5}, {y, -5, 5}, 
   VectorSizes -> {0, .5}, VectorMarkers -> Placed["Arrow", "Start"], 
 VectorPoints -> (curve /@ Subdivide[0, 3 Pi, 60]), 
 Epilog -> {Red, First@ParametricPlot[curve[t], {t, 0, 3 Pi}]}, 
 PlotRange -> {-5, 5}]  

enter image description here

ParametricPlot3D:

vf3D[{x_, y_, z_}] = {-1 - x^2 + y, 1 + x - y^2, Sqrt[z] };
curve3D[t_] := Sqrt[t] {Cos[t], Sin[t], t/5};

Show[ParametricPlot3D[curve3D[t], {t, 0, 3 Pi}, PlotStyle -> Thick], 
  Graphics3D @ {Red, Thick, 
      Arrow[{curve3D @ #, curve3D @ # + (scale Norm[#] Normalize[#] &@
        vf3D[curve3D@ #])}] & /@ Rest[Subdivide[0, 3 Pi, 50]]}, 
  PlotRange -> All] 

enter image description here

$\endgroup$
1
  • $\begingroup$ +1. As usually, your answer is very well. However, I will be waiting some time for other answers. $\endgroup$
    – user64494
    Mar 24 at 9:29
8
$\begingroup$

Edition

r[u_] = {Sin[u], Cos[u], u/10};
curve = ParametricPlot3D[{Sin[u], Cos[u], u/10}, {u, 0, 20}];
Show[VectorPlot3D[{x, y, z}, {x, -2, 2}, {y, -2, 2}, {z, -2, 2}, 
   VectorScaling -> True, VectorMarkers -> "Arrow", 
   VectorPoints -> Table[r[u], {u, 0, 20, .1}]] /. 
  Arrow[{p1_, p2_}] :> Arrow[{(p1 + p2)/2, p2}], curve]

enter image description here

Or use the approach Placed["Arrow", "Start"] by @kglr

r[u_] = {Sin[u], Cos[u], u/10};
curve = ParametricPlot3D[{Sin[u], Cos[u], u/10}, {u, 0, 20}];
Show[VectorPlot3D[{x, y, z}, {x, -2, 2}, {y, -2, 2}, {z, -2, 2}, 
  VectorScaling -> True, VectorMarkers -> Placed["Arrow", "Start"], 
  VectorPoints -> Table[r[u], {u, 0, 20, .1}]], curve]

enter image description here

r[u_] = {Sin[u], Cos[u], u/10};
curve = ParametricPlot3D[{Sin[u], Cos[u], u/10}, {u, 0, 20}];
vectorfield = 
  VectorPlot3D[{x, y, z}, {x, -2, 2}, {y, -2, 2}, {z, -2, 2}, 
    VectorScaling -> True, 
    VectorPoints -> Table[r[u], {u, 0, 20, .1}]] /. 
   Arrow[Tube[{p1_, p2_}, t_]] :> Arrow[Tube[{(p1 + p2)/2, p2}, t]];
Show[vectorfield, curve, PlotRange -> All, Boxed -> False]

enter image description here

$\endgroup$
2
  • $\begingroup$ cvgmt (@ does not work.):+1. Arrows are not nice for me. $\endgroup$
    – user64494
    Mar 24 at 9:32
  • $\begingroup$ VectorMarkers -> Placed["Arrow", "Start"], VectorStyle -> Red, VectorColorFunction -> None $\endgroup$
    – cvgmt
    Mar 24 at 11:08
6
$\begingroup$

A 2D example:

field[{x_, y_}] = {-y^2, x  y};
curve[t_] = {t, t^3} + 0.3;
Show[
 ParametricPlot[curve[t], {t, -1 , 1}, PlotRange -> {{-1, 1}, {-1, 1}}],
 Graphics[{Arrowheads[{{Automatic, 1}}], Table[Arrow[{curve[t], curve[t] + field[curve[t]]}], {t, -1, 1, .1}]}
  ]
 ]

enter image description here

And a 3D example:

field[{x_, y_, z_}] = 0.5 {-y^2, x  y, Sqrt[z y]};
curve[t_] = {t, t^3, Sqrt[t]};
Show[
 ParametricPlot3D[curve[t], {t, 0 , 1},  PlotRange -> {{0, 1}, {0, 1.5}, {0, 2}}],
 Graphics3D[{Arrowheads[0.03], 
   Table[Arrow[{curve[t], curve[t] + field[curve[t]]}], {t, 0,  1, .05}]}
  ]
 ]

enter image description here

$\endgroup$
2
  • $\begingroup$ DanielHuber (@ does not work.) :+1. Your answer does not intersect with the kglr's one. $\endgroup$
    – user64494
    Mar 24 at 9:30
  • $\begingroup$ kgr extended his answer after the above comment of me. $\endgroup$
    – user64494
    Mar 24 at 13:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.