Interactive Models with Stream Plot and Locator

Exercise

In a previous lab, we studied the system  ({{{, ({{x'= y, (1)}, {y'=-y-2sin(x), (2)}})}}).  
We will now focus on a generalization 
({{{, ({{x'= y, (1)}, {y'=-ϵ y-2sin(x), (2)}})}}),  where ϵ is a real parameter in the interval [0,1]. This system defines the equation of motion of a pendulum. 

The physical meaning of the variables and parameters of the system:
x = x(t) is the angular coordinate (the angle that the bob of the pendulum makes with the vertical direction, represented as a function of time),
y = x’(t) is the velocity of the angular motion. 
ϵ ⩾ 0 is the coefficient of friction (when ϵ=0 there is no friction). The model assumes that the friction is proportional to the velocity.

Our task for today is to make an interactive plot of the Phase Portrait when -3π < x < 3π , -2π < y < 2π, which can highlight the trajectory corresponding to the initial condition chosen by the user. The parameter ϵ of the system of differential equations should be defined as a parameter of the interactive model.

Solution 1

Manipulate[
StreamPlot[{y,-2Sin[x]-ϵ*y},{x,-3π,3π},{y,-2π,2π}, ImageSize->Large,
StreamPoints->{{{{a,b},Red},Automatic}}],
{a, -3π,3π},
{b,-2π,2π},
{ϵ, 0,1}
]

Physical Interpretation :

Case I: ϵ=0 (no friction)

There are two types of critical points: centers (stable, but not asymptotically stable), and saddle fixed points (unstable).

The closed trajectories correspond to back and forth oscillations of the pendulum.

The points at the centers of these closed trajectories correspond to the stable equilibrium of the pendulum at rest at the bottom of its motion.

The trajectories above and beneath the closed trajectories correspond to motions in which the pendulum has enough energy to simply go full circle, round and round   over the top.

The level curves joining the saddle fixed points are those exceptional motions in which for t→∞  and for t→-∞  the pendulum tends to the unstable equilibrium  at the very top of the swing. Note that the time required to go from one saddle to the next is infinite.

Case II: ϵ > 0 (friction acts as a damping factor on the motion of the pendulum)
 There are two types of critical points : spiral sinks (asymptotically stable), and saddle fixed points (unstable).

The spiral sinks correspond to the stable equilibrium of the pendulum at rest at the bottom of its motion.

Almost all trajectories converge to one of the spiral sinks as t→∞.

There are some exceptional trajectories converging to the saddle fixed points for t→∞ OR for  t→-∞  which correspond to the exceptional motions where the pendulum tends to the unstable equilibrium at the very top of the swing.

Solution 1 can be improved:

The disadvantage of Solution 1 is that every time we drag the slider to change the initial condition, the entire phase portrait gets replotted, not just the trajectory selected by the user!

To improve Solution 1, let' s first recall how to combine graphical objects together:

Show[g1, g2, …, gk]    ---  shows several graphics combined.

Example a)

splot=StreamPlot[{y,-2Sin[x]-y},{x,-3π,3π},{y,-2π,2π}, ImageSize->Large, StreamColorFunction->"Rainbow"];
line = Plot[1, {x,-3π,3π}, PlotStyle->Red];
Show[splot, line]

Example b)

a=1; b=4;
splot=StreamPlot[{y,-2Sin[x]-y},{x,-3π,3π},{y,-2π,2π}, ImageSize->Large];
sol = NDSolve[{x'[t]==y[t],y'[t]==-2Sin[x[t]]- y[t],x[0]== a,y[0]== b},{x,y},{t,0,10}]
trajectory = ParametricPlot[Evaluate[{x[t], y[t]} /. sol], {t, 0, 10}, PlotStyle->Red];
Show[splot, trajectory]

Solution 2

splot=StreamPlot[{y,-2Sin[x]-y},{x,-3π,3π},{y,-2π,2π},StreamScale->Automatic, ImageSize->Large];
Manipulate[Module[{sol,x,y, trajectory},
sol = NDSolve[{x'[t]==y[t],y'[t]==-2Sin[x[t]]- y[t],x[0]== a,y[0]== b},{x,y},{t,0,10}];
trajectory = ParametricPlot[Evaluate[{x[t], y[t]} /. sol], {t, 0, 10}, PlotStyle->Red];
Show[splot, trajectory]],
 {a,-3π,3π }, {b,-2π,2π} ,
 SaveDefinitions->True]

Solution 3

Locator[{x,y}]  represents a locator object at position (x,y) in a graphical object.

Locator[Dynamic[pos]] 
takes the position to be the dynamically updated current value of pos, with this value being reset if the locator object is moved.

Graphics[ Text["some text",{0,7}]]

Let' s write an interactive model that shows the previous StreamPlot, with a dynamic variable called point, which is linked to Locator.

Manipulate[
Show[splot],
{point,Locator},
 SaveDefinitions->True]

Let' s first see what Locator can do.

Manipulate[
Show[splot, Graphics[ Text[Style[point, Large],{0,7}]]],
{{point,{2,3}},Locator},
 SaveDefinitions->True
]

Manipulate[
Show[splot, Graphics[ Text[Style[point, Large],point+{0,1}]]],
{{point,{2,3}},Locator},
 SaveDefinitions->True
]

Exercise :

1. First assume that ϵ=1, as in the examples above. Use Manipulate and Locator to make an interactive plot that shows the corresponding StreamPlot. When the user clicks somewhere in the Phase Portrait, we plot the trajectory that passes through that point in red.

2. Now take the code from part 1, and make ϵ a parameter of your model as well. Be sure to implement it in such a way that you only draw the Phase Portrait when the user enter a different value for ϵ. When the user clicks somewhere, you should not redraw the entire Phase Portrait, but only the trajectory that passes through the corresponding point.

Solution :

Let's use Locator inside the interactive model. When the user clicks on a point in the Phase Portrait, we solve the differential equation corresponding to the initial condition selected by the user, and highlight the corresponding trajectory in the Phase Portrait. We can include also a slider for the time t (that controls how much of the trajectory we should plot)

Solution to Exercise 1 :

splot=StreamPlot[{y,-2Sin[x]-y},{x,-3π,3π},{y,-2π,2π}, ImageSize->Large];
Manipulate[Module[{trajectory,x,y},
trajectory = ParametricPlot[
Evaluate[{x[t],y[t]}/.NDSolve[{x'[t]==y[t],y'[t]==-2Sin[x[t]]- y[t],x[0]==point[[1]],y[0]==point[[2]]},{x,y},{t,0,T}]],{t,0,T},PlotStyle->Red];
Show[splot, trajectory]],
{{T,10},0,100},
{{point,{1,0}},Locator},
SaveDefinitions->True]

Solution to Exercise 2 :

We can use two nested Manipulate[..] functions, or one single Manipulate[..] in combination with DynamicModule[..] and Dynamic[..].

Manipulate[
splot2=StreamPlot[{y,-2Sin[x]-ϵ*y},{x,-3π,3π},{y,-2π,2π}, ImageSize->Large];
Manipulate[Module[{trajectory,x,y},
trajectory = ParametricPlot[
Evaluate[{x[t],y[t]}/.NDSolve[{x'[t]==y[t],y'[t]==-2Sin[x[t]]- ϵ*y[t],x[0]==point[[1]],y[0]==point[[2]]},{x,y},{t,0,T}]],{t,0,T},PlotStyle->Red];
Show[splot2, trajectory]],
{{T,10},0,100},
{{point,{1,0}},Locator},
SaveDefinitions->True],
{ϵ,0,1}, 
SaveDefinitions->True
]

Manipulate[DynamicModule[{splot3},
splot3=StreamPlot[{y,-2Sin[x]-ϵ*y},{x,-3π,3π},{y,-2π,2π}, ImageSize->Large];
Dynamic[Module[{trajectory,x,y},
trajectory = ParametricPlot[
Evaluate[{x[t],y[t]}/.NDSolve[{x'[t]==y[t],y'[t]==-2Sin[x[t]]- ϵ*y[t],x[0]==point[[1]],y[0]==point[[2]]},{x,y},{t,0,T}]],{t,0,T},PlotStyle->Red];
Show[splot3, trajectory]]]],
{{T,10},0,100},
{{point,{1,0}},Locator},
{ϵ,0,1}, 
SaveDefinitions->True
]