3
$\begingroup$

I wrote the simple following code to plot a given vector field:

rr := Sqrt[x^2 + y^2];
a := 0.5;
xlim = 1;
splot = StreamPlot[{
    rr^a (x (x rr + y rr + y^2))/rr^3,
    rr^a (y (x rr + y rr - x^2))/rr^3},
   {x, -xlim, xlim}, {y, -xlim, xlim},
   StreamColorFunction -> "Heat", AxesLabel -> {"x", "y"}];
Show[splot]

enter image description here

I would like to color the trajectories which enter and leave the origin in different colors. For example:

enter image description here

Is there a simple way to do this? Thanks very much in advance!

$\endgroup$

1 Answer 1

3
$\begingroup$

This should get you started:

First, make a color function that uses the point $(x,y)$ and vector $(v_x,v_y)$ at that point to get the $\cos$ of the angle between the two:

Clear[color]
color[x_, v_] := Module[{q},
  q = x.v/(Norm[x] Norm[v]);
  Return[Blend[{Blue, Black, Red}, (q + 1)/2]]
  ]

Edit Another color Function:

Clear[color]
color[x_, v_] := Module[{q, c},
Which[
  x[[1]] > 0 && x[[2]] <= 0,
  c = GrayLevel[.2]
 ,
 True,
  q = x.v/(Norm[x] Norm[v]);
 c = Blend[{Blue, Black, Red}, (q + 1)/2]
 ];
 Return[c]
 ]

Then use the function in your plot:

rr := Sqrt[x^2 + y^2];
a := 0.5;
xlim = 1;
splot = StreamPlot[{rr^a (x (x rr + y rr + y^2))/rr^3, 
rr^a (y (x rr + y rr - x^2))/rr^3},
     {x, -xlim, xlim}, {y, -xlim, xlim},
StreamColorFunctionScaling -> False, 
StreamColorFunction -> (color[{#1, #2}, {#3, #4}] &), 
AxesLabel -> {"x", "y"}];
Show[splot]

enter image description here

You can play with the colors in the Blend[] function to get the look what you want.

$\endgroup$
6
  • $\begingroup$ Thank you -- this is almost exactly what I needed! One small question, how can it be modified so that ll curves under the x-axis are blue. I didn't describe this well, but I only want to color the exiting curves from the origin red (as in my hand-drawn picture) $\endgroup$
    – J. Doee
    Nov 1, 2017 at 23:49
  • $\begingroup$ You almost never need to use Return[], and Normalize[] is built-in. Thus: color[x_, v_] := Blend[{Blue, Black, Red}, (1 + Normalize[x].Normalize[v])/2]. Alternatively: color[x_, v_] := Blend[{Blue, Black, Red}, 1 - CosineDistance[x, v]/2]. $\endgroup$ Nov 2, 2017 at 4:12
  • $\begingroup$ Thanks! Is there a way to modify the color function to make it piecewise defined so that it follows this rule above the x-axis and below it colors everything blue? $\endgroup$
    – J. Doee
    Nov 2, 2017 at 11:43
  • $\begingroup$ Actually, I'd like all the vectors in the fourth quadrant to be colored gray. $\endgroup$
    – J. Doee
    Nov 2, 2017 at 14:38
  • $\begingroup$ I was able to do this simply by "showing" two plots, where I excluded the relevant regions. Thanks! $\endgroup$
    – J. Doee
    Nov 2, 2017 at 15:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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