# Plotting the solutions of $y' = -\frac{\cos y}{\sin x}$

I struggle with the problem of plotting the solution of this ODE:

$\qquad y' = -\frac{\cos y}{\sin x}$

I've already tried to use ContourPlot in a loop and in a table. For example, I tried:

For[i = 0, i < 5, i = i + 0.1,
f5 =
Show[
f5,
ContourPlot[y == 2*ArcTan[i/Tan[x/2]] - Pi/2, {x, -10, 10}, {y, -10, 10}]];]


In most cases I get nothing except an empty coordinate system.

I know what the curves should look like, because I worked it out by hand as shown below:

Could anybody point to possible way of plotting this "leafs"? Thank you!

• StreamPlot[{1, -Cos[y]/Sin[x]}, {x, -4, 4}, {y, -4, 4}, StreamColorFunction -> "Rainbow", StreamPoints -> Fine], Vector Plot should also work. – Moo Mar 8 '16 at 14:18
• @Moo Youre made my day! According to my assignment I should also plot stream field "under" solutions. Is there some way to "imulate" integral curves by StreamPlot[]? – RuD_wow Mar 8 '16 at 14:43
• Welcome to Mathematica.SE! I suggest the following: 1) As you receive help, try to give it too, by answering questions in your area of expertise. 2) Take the tour! 3) When you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge. Also, please remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign! – Michael E2 Mar 9 '16 at 19:22

This is just a suggested amendment to JasonB's answer. It wasn't clear to me that it deserves a separate answer, since it deals with side issues tangentially related to the OP's question.

It's easier to deal with the singularities if we scale the direction field by Sin[x]. Since only one streamline needs updating when the initial condition is changed, we aim for code that does only that. Applying Dynamic to a plot inside Show, e.g. Show[p1, Dynamic[p2]], does not work, so I extract the graphics from the stream plot, apply Dynamic and rewrap in Graphics. Try it with Graphics@Dynamic@First@ omitted and you'll see a tremendous slow-down; there is still a big difference even if the first StreamPlot is computed once and passed to Manipulate in a variable, because all the graphics are updated even though they don't change.

Manipulate[
Show[
StreamPlot[{Sin[x], -Cos[y]},
{x, -4, 4}, {y, -4, 4},
StreamColorFunction -> "Rainbow", StreamPoints -> Fine],
Graphics@Dynamic@First@
StreamPlot[{Sin[x], -Cos[y]},
{x, -4, 4}, {y, -4, 4},
StreamColorFunction -> Red, StreamPoints -> {{{p0, Red}}}]
],
{{p0, {1.5, 1}}, {-4, -4}, {4, 4}, Locator}]


• I really like your streamplot better, with the coloring and I especially like the point selector rather than choosing x and y points. I was trying to show the solution the the differential equation on top of the stream plot, which is the red line in my manipulate function. The OP didn't state this in the question but in a comment to it, and I like how it shows the slope of the solution has the same direction as the arrows only at the point selected. If I had more time, I bet you could make it so you don't have to put the DSolve inside the Manipulate. – Jason B. Mar 8 '16 at 19:41
• Or maybe I'm missing it altogether and you are plotting the solution and not having to use DSolve. I'm in no way a mathematician so I miss things like that – Jason B. Mar 8 '16 at 19:45
• @JasonB Yes the red line is a solution. Note there are both an analytic (in the math. sense) and an algebraic viewpoint to what a solution is. Analytically, a solution is a function defined on an interval, such that the values of the function and its derivative satisfy the ODE throughout the interval. The ODE has singularities where Sin[x] == 0. So a solution in this sense is only defined between successive multiples of Pi. This is what NDSolve returns. OTOH, DSolve returns an algebraic expression in terms of periodic trig. fns. – Michael E2 Mar 8 '16 at 19:56
• So does streamplot use DSolve under the hood when you supply the streampoints? (I could look that up myself tomorrow lol) – Jason B. Mar 8 '16 at 20:13
• @JasonB No, NDSolve. – Michael E2 Mar 8 '16 at 20:14

I may not be understanding the question correctly, but if you wish to show the stream field under solutions, perhaps this is a method:

Manipulate[
Module[{soln},
soln = Quiet@
DSolve[{y'[x] == -Cos[y[x]]/Sin[x], y[x0] == y0}, y[x], x];
Show[
StreamPlot[{1, -Cos[y]/Sin[x]}, {x, -4, 4}, {y, -4, 4},
StreamColorFunction -> "Rainbow", StreamPoints -> Fine],
Plot[y[x] /. soln, {x, -4, 4}, PlotStyle -> Directive[Thick, Red],
PlotPoints -> 60],
Graphics@{PointSize[Large], Blue, Point[{x0, y0}]}
]
],
{{x0, 1.5}, -4, 4, Appearance -> "Open"},
{{y0, 1}, -4, 4, Appearance -> "Open"}]
`