1
$\begingroup$

I want to solve some differential equations, but the warnings "NDSolve change the value" are thrown. How do I change the equations? Are there some equations that are not allowed? Are some equations wrong?

Clear["`*"]
n = 10;
a = CirclePoints[n];
b = 0.1;
equ = Flatten[
   Append[Table[{Sqrt[
       Subscript[y, k - 1]'[t]^2 + Subscript[x, k - 1]'[t]^2] == 
       b, (Subscript[y, k][t] - Subscript[y, k - 1][t])*
        D[Subscript[x, k - 1][t], 
         t] == (Subscript[x, k][t] - Subscript[x, k - 1][t])*
        D[Subscript[y, k - 1][t], t], 
      Subscript[y, k - 1][0] == a[[k - 1, 2]], 
      Subscript[x, k - 1][0] == a[[k - 1, 1]], 
      Subscript[y, k - 1]'[0] == b*Normalize[a[[k]] - a[[k - 1]]][[2]]
      , Subscript[x, k - 1]'[0] == 
       b*b*Normalize[a[[k]] - a[[k - 1]]][[1]]
      }, {k, 2, 
      n}], {(Subscript[y, 1][t] - Subscript[y, n][t])*
       D[Subscript[x, n][t], 
        t] == (Subscript[x, 1][t] - Subscript[x, n][t])*
       D[Subscript[y, n][t], t], Subscript[y, n][0] == a[[n, 2]], 
     Subscript[x, n][0] == a[[n, 1]], 
     Sqrt[Subscript[y, n]'[t]^2 + Subscript[x, n]'[t]^2] == b, 
     Subscript[y, n]'[0] == b*Cos[\[Pi]/n], 
     Subscript[x, n]'[0] == b*Sin[\[Pi]/n]}], 2];

sol = NDSolve[equ, 
   Flatten[{Subscript[x, #][t], Subscript[y, #][t]} & /@ Range@n, 
    2], {t, 0, 30}, MaxSteps -> 2000, SolveDelayed -> True];
result = Flatten[{Subscript[x, #][t], Subscript[y, #][t]} & /@ 
    Range@n, 2];
result = Partition[result /. sol[[1]], 2];

ParametricPlot[result, {t, 0, 30}, PlotTheme -> "Scientific", 
 PlotLabel -> "Bugs' movation", FrameLabel -> {"x", "y"}]

$\endgroup$
3
  • 2
    $\begingroup$ Some suggestions: 1) Submit a question that captures the essence of your problem but has fewer variables. 2) Describe the difficulties you are having. 3) Do not use Subscript variables. They may look nice but often cause problems. 4) Use Method -> {"EquationSimplification" -> "Residual"} instead of the obsolete SolveDelayed -> True. $\endgroup$
    – bbgodfrey
    Sep 16, 2021 at 2:17
  • 2
    $\begingroup$ With 20 first order ODEs and 20 variables, you have 40 boundary conditions, which may be too many. $\endgroup$
    – bbgodfrey
    Sep 16, 2021 at 2:30
  • 1
    $\begingroup$ Echoing bbgodfrey's comments, you want to present what's called a "minimal working example"—the shortest and simplest example sufficient to demonstrate the problem (see: en.wikipedia.org/wiki/Minimal_working_example). In your specific case, you'll want to find the smallest combination of variables that demonstrates the problem. The exercise of doing this might also help you determine what's causing the problem. $\endgroup$
    – theorist
    Sep 16, 2021 at 6:38

1 Answer 1

5
$\begingroup$

Update

Upon further examination, I noticed that the definition of Subscript[x, k - 1]'[0] in the question contains an extra factor of b, with the consequence that this initial condition is inconsistent with the ODEs,

Sqrt[Subscript[y, k - 1]'[t]^2 + Subscript[x, k - 1]'[t]^2] == b

Also, the definitions of Subscript[x, n]'[0] and Subscript[y, n]'[0] are inconsistent with the definitions of these two quantities for other values of k. With these corrections, and also eliminating subscripted variables in favor of indexed variables as recommended in my comment above, yields the simplified code block

n = 10;
a = CirclePoints[n];
b = 0.1;
equ = Flatten@
    Table[{Sqrt[y[k]'[t]^2 + x[k]'[t]^2] == b, 
    (y[Mod[k + 1, n, 1]][t] - y[k][t])*D[x[k][t], t] == 
      (x[Mod[k + 1, n, 1]][t] - x[k][t])*D[y[k][t], t], 
    Thread[{x[k][0], y[k][0]} == a[[k]]], 
    Thread[{x[k]'[0], y[k]'[0]} == b*Normalize[a[[Mod[k + 1, n, 1]]] - a[[k]]]]}, 
    {k, 1, n}];

Solving the system, again in accordance with my comment above

sol = NDSolve[equ, Flatten[{x[#], y[#]} & /@ Range@n], {t, 0, 32}, 
    Method -> {"EquationSimplification" -> "Residual"}] // Flatten;

no longer produces the warning message in the question,

NDSolve::ivcon The given initial conditions were not consistent with the differential-algebraic equations. NDSolve will attempt to correct the values

The resulting solution is

Plot[Evaluate@Through[Values[sol][t]], {t, 0, 32}, ImageSize -> Large]

enter image description here

For completeness, two consistency tests for the initial conditions used in my answer are

(Partition[equ, 6][[All, ;; 2]] /. t -> 0) /. 
    (Flatten[Partition[equ, 6][[All, 3 ;;]]] /. Equal -> Rule)
(* {{True, True}, {True, True}, {True, True}, {True, True}, {True, True}, 
    {True, True}, {True, True}, {True, True}, {True, True}, {True, True}} *)

Partition[equ /. sol /. t -> 0, 6]
(* {{True, True, True, True, True, True}, {True, True, True, True, True, True}, 
    {True, True, True, True, True, True}, {True, True, True, True, True, True}, 
    {True, True, True, True, True, True}, {True, True, True, True, True, True}, 
    {True, True, True, True, True, True}, {True, True, True, True, True, True}, 
    {True, True, True, True, True, True}, {True, True, True, True, True, True}} *)

In contrast, corresponding consistency results for the code in the question are

(* {{False, False}, {False, False}, {False, False}, {False, False}, {False, True}, 
    {False, False}, {False, False}, {False, False}, {False, False}, {True, False}} *)

(* {{True, False, True, True, False, False}, {True, False, True, True, False, False}, 
    {True, False, True, True, False, False}, {True, True, True, True, True, False}, 
    {True, False, True, True, False, False}, {True, False, True, True, False, False}, 
    {True, False, True, True, False, False}, {True, False, True, True, False, False}, 
    {True, False, True, True, False, False}, {True, False, True, True, False, False}} *)

As an aside, I had been looking for more complicated issues, because NDSolve was able to produce the correct plot even with the wrong initial conditions, although with the warning message above. This seems surprising to me.

$\endgroup$
5
  • $\begingroup$ There is typo in your code on the line Table[{Sqrt[y[k]'[t]^2 + x[k]'[t]^2 == b, Just put ] to close \Sqrt[]`. $\endgroup$ Sep 17, 2021 at 4:40
  • $\begingroup$ @AlexTrounev Corrected. Thanks. $\endgroup$
    – bbgodfrey
    Sep 17, 2021 at 12:56
  • $\begingroup$ It is very nice solution (+1), but for me also it is not clear how NDSolve got solution with message you are mentioned. $\endgroup$ Sep 17, 2021 at 14:53
  • $\begingroup$ @AlexTrounev In the course of investigating this problem, I tried other boundary conditions that were, in retrospect, also incorrect. Some gave the correct output anyway (although with the warning message), others gave different answers. Perhaps, I shall ask a question along these lines later today. I did just add to my answer the consistency results for the question. Thanks for your interest. You clearly are an NDSolve expert. $\endgroup$
    – bbgodfrey
    Sep 17, 2021 at 15:31
  • $\begingroup$ @AlexTrounev I now see that the ODEs plus the {x[k][0], y[k][0]} initial conditions uniquely determine the {x[k]'[0], y[k]'[0]} initial conditions up to an overall sign for each k. On this basis, the ability of DSolve to obtain the correct solution from incorrect initial conditions does not seem so surprising. Probably, I shall not pose a question on this matter after all. $\endgroup$
    – bbgodfrey
    Sep 19, 2021 at 0:44

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.