2
$\begingroup$

I'm trying to combine the plot of a function with a vector plot, but no matter what option I choose for VectorPoints, the resulting combined plot just looks ridiculous. For example, the combined plot with the "VectorPoints -> Coarse" option looks like this:

enter image description here

What I actually want is just one vector plot line with small arrows that sits slightly above the solid red line of the function, and one that sits slightly below it.

Edit 1: this is the combined plot using StreamPlot instead of VectorPlot with the same parameters, i.e.

spfig = StreamPlot[{1, f[x, y]}, {x, 40, 75}, {y, 1, 5}, StreamPoints -> Coarse];
Show[fig1, spfig]

enter image description here

Edit 2: here is a MWE:

F[x_, y_] := 1 - (2 (64 - x)^(1/3) y[x]^2)/(2 (64 - x)^(1/3) + y[x]^3);
Eq = D[y[x], x] == F[x, y]/2 // Simplify;
NDSolveRes = NDSolve[{Eq, y[53.854] == 2.632}, y, {x, 0, 70}];
fig1 = Plot[y[x] /. NDSolveRes, {x, 0, 90}, AxesLabel -> {"x", "y"}, PlotStyle -> {Darker[Red], Thickness -> 0.003}];
f[x_, y_] := 1/2 - (y^2 (64 - x)^(1/3))/(y^3 + 2 (64 - x)^(1/3));
vpfig = VectorPlot[{1, f[x, y]}, {x, 40, 75}, {y, 1, 5}, VectorPoints -> Coarse];
Show[fig1, vpfig]

Edit 3: here are the error messages:

enter image description here

$\endgroup$
4
  • $\begingroup$ You might want to look into StreamPlot with the StreamPoints option. $\endgroup$
    – Lukas Lang
    Sep 22 at 9:17
  • $\begingroup$ I have, but the lines of the StreamPlot differ significantly from those of the VectorPlot. I have attached another figure to my original post showing the output of StreamPlot using the exact same function and parameters as for VectorPlot. $\endgroup$ Sep 22 at 9:31
  • $\begingroup$ Please provide the function definition and what you used to plot the red curve. Can you plot each over the same range and then use Show? $\endgroup$
    – Moo
    Sep 22 at 10:49
  • $\begingroup$ I've added a MWE, see edit 2 at the end of my original post. $\endgroup$ Sep 22 at 12:52
5
$\begingroup$

You should be able to use VectorPoints where you define the points you want instead of using Coarse

F[x_, y_] := 1 - (2 (64 - x)^(1/3) y[x]^2)/(2 (64 - x)^(1/3) + y[x]^3)
Eq = D[y[x], x] == F[x, y]/2 // Simplify
NDSolveRes = NDSolve[{Eq, y[53.854] == 2.632}, y, {x, 0, 70}]
xs = Range[40, 70]
ys = Flatten[Table[Re[Evaluate[y[x]] /. NDSolveRes], {x, 40, 70}]]
upperPoints = Transpose[{xs, ys + .3}]
lowerPoints = Transpose[{xs, ys - .3}]
fig1 = Plot[y[x] /. NDSolveRes, {x, 0, 90}, AxesLabel -> {"x", "y"}, 
  PlotStyle -> {Darker[Red], Thickness -> 0.003}]
f[x_, y_] := 1/2 - (y^2 (64 - x)^(1/3))/(y^3 + 2 (64 - x)^(1/3))
vpfig1 = VectorPlot[{1, f[x, y]}, {x, 40, 75}, {y, 1, 5}, 
  VectorPoints -> lowerPoints]
vpfig2 = VectorPlot[{1, f[x, y]}, {x, 40, 75}, {y, 1, 5}, 
  VectorPoints -> upperPoints]
Show[fig1, vpfig1, vpfig2]

enter image description here

You can change the arrow properties as you see fit for better scaling and such.

EDIT:

Here's a version with better scaling that puts the upper and lower arrows along the entire line

F[x_, y_] := 1 - (2 (64 - x)^(1/3) y[x]^2)/(2 (64 - x)^(1/3) + y[x]^3)
Eq = D[y[x], x] == F[x, y]/2 // Simplify
NDSolveRes = NDSolve[{Eq, y[53.854] == 2.632}, y, {x, 0, 70}]
xs = Range[0, 70, 4]
ys = Flatten[Table[Re[Evaluate[y[x]] /. NDSolveRes], {x, 0, 70, 4}]]
upperPoints = Transpose[{xs, ys + .3}]
lowerPoints = Transpose[{xs, ys - .3}]
fig1 = Plot[y[x] /. NDSolveRes, {x, 0, 90}, AxesLabel -> {"x", "y"}, 
  PlotStyle -> {Darker[Red], Thickness -> 0.003}]
f[x_, y_] := 1/2 - (y^2 (64 - x)^(1/3))/(y^3 + 2 (64 - x)^(1/3))
vpfig1 = VectorPlot[{1, f[x, y]}, {x, 0, 75}, {y, 0, 8}, 
  VectorPoints -> lowerPoints, VectorAspectRatio -> .1, 
  VectorSizes -> .6, VectorColorFunction -> None, VectorStyle -> Blue]
vpfig2 = VectorPlot[{1, f[x, y]}, {x, 0, 75}, {y, 0, 8}, 
  VectorPoints -> upperPoints, VectorAspectRatio -> .1, 
  VectorSizes -> .6, VectorColorFunction -> None, VectorStyle -> Blue]
Show[fig1, vpfig1, vpfig2]

enter image description here

$\endgroup$
9
  • $\begingroup$ Thank you! How exactly do I change the arrow properties? Ideally, I would like to decrease the density/frequency of arrows to only 6-7 arrows for each line (upper and lower) and make the arrowheads smaller. I've tried xs = Range[40, 70,4] to increase the step size but I can't figure out how to make it compatible with the flattened table. Also, I've tried "VectorScaling->" and "VectorSizes->" in the VectorPlot[] bracket (as illustrated here: reference.wolfram.com/language/ref/VectorPlot.html), but it seems that these commands are not recognised for some reason (text turned red)?? $\endgroup$ Sep 22 at 15:46
  • $\begingroup$ Try adding VectorAspectRatio -> .1, VectorSizes -> .6 to the VectorPlots. For the spacing you can do 40,70,4 in both the xs and in the ys and it'll space out the points.xs = Range[40, 70, 4] and ys = Flatten[Table[Re[Evaluate[y[x]] /. NDSolveRes], {x, 40, 70, 4}]] $\endgroup$
    – Derek H
    Sep 22 at 18:54
  • $\begingroup$ @ConfusedCabbage See my edit $\endgroup$
    – Derek H
    Sep 22 at 19:01
  • $\begingroup$ I've copied the code from your edit, but "VectorAspectRatio -> .1" and "VectorSizes -> .6" are highlighted in red and I'm getting errors, see screenshot in edit 3 at the end of my original post. $\endgroup$ Sep 22 at 22:51
  • 1
    $\begingroup$ Never mind. I found a way to do it: specifying PlotRange->{{0,90},{0,9}} in fig1 and plotting fig1 first in Show[] keeps the range fixed to $x \in \left[0,90\right]$ and $y \in \left[0,9\right]$. Thanks again for your help! $\endgroup$ Sep 23 at 4:14

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.