# How to put constraints on NDSolve[] solution

Assumed the following example of double pendulum by Thison (Answered by Jens):

Animation of double pendulum

Imagine that in the given animation(i.e. http://i.stack.imgur.com/p4TgL.gif ), the second pendulum (Second Linkage) can not rotate 360 degree. In other words, I want to somehow put a constraint on this system with these given equations and initial conditions and limit their motions. For instance, Second pendulum can have a rotation like 0<phi2[t]<180.

I wonder how it is possible to apply this constraint in NDSolve[] or any possible solution for this problem.

• You have to put constraint not in NDSolve, but on formulation of the problem, i.e. equations, itself. If your do not want to deals with physics here, one idea is to redefine angle function that it always return 180, for higher values. – user18792 Nov 28 '14 at 10:53
• @user18792: Could you please elaborate it more?! How is it possible to redefine and add a function that always return 180 for higher values?! – Shellp Nov 28 '14 at 14:30
• I absolutely aggree with the first sentence of user18792: you should reformulate the problem. If you just ad hoc enforce a constraint with a programming trick the solution will violate physical laws (energy conservation, momentum conservation,...) in an arbitrary and basically uncontrolled way... – Albert Retey Nov 28 '14 at 15:09
• @Albert Retey: I would be thankful if you could help me with this problem and tell me how to apply the constraints and consider them in the formulation. – Shellp Nov 28 '14 at 21:49

By restriction I had in mind clipping: something like this.

    x1[t_] := Evaluate[Sin[Clip[phi1[t] /. sol, {-Pi/4, Pi/4}]]]
y1[t_] := Evaluate[-Cos[Clip[phi1[t] /. sol, {-Pi/4, Pi/4}]]]
x2[t_] :=
Evaluate[Sin[Clip[phi1[t] /. sol, {-Pi/4, Pi/4}]] +
Sin[Clip[phi2[t] /. sol, {-Pi/4, Pi/4}]]]
y2[t_] :=
Evaluate[-(Cos[Clip[phi1[t] /. sol, {-Pi/4, Pi/4}]] +
Cos[Clip[phi2[t] /. sol, {-Pi/4, Pi/4}]])]


Then you will see as @Albert Retey pointed unphysical movie of pendulum. So, I would suggest you to start from the Lagrangian formulation

L=((m1+m2)/2)*l1^2*D[ϕ1[t],t]^2+(m2/2)*l2^2*D[ϕ2[t],t]^2+m2*l1*l2*D[ϕ1[t],t]*D[ϕ2[t],t]*Cos[ϕ1[t]-ϕ2[t]]+(m1+m2)*g*l1*Cos[ϕ1[t]]+m2*g*l2*Cos[ϕ2[t]]


From which you can easily derive equations you solve

eqns={D[D[L,D[ϕ1[t],t]],t]-D[L,ϕ1[t]]==0,D[D[L,D[ϕ2[t],t]],t]-D[L,ϕ2[t]]==0}


This problem by the way is listed in Landau (I volume, Mechanics, p11 as exercise).

Then your have to think how to add restrictions into the Lagrangian (may be using Lagrangian multipliers method). I never did these things. May be somebody else have some ideas.

• Thank you so much for you help. Unfortunately, in this exercise you mentioned, no constraints are considered.However, that's a great help to me. – Shellp Nov 28 '14 at 21:51