2
$\begingroup$

FindRoot has a nice example to solve a boundary-value problem for a second-order ordinary differential equation using "collocation points" to turn the ODE into a set of nonlinear coupled algebraic equations. It seems to run much faster than NDSolve, so I'd like to use it. I am having troubles that trace back to the use of subscripts, which are discouraged in any case. I'd like to replace the subscripts by something equivalent (e.g. defining a 2 by n matrix of variables) but am having difficulties.

Here is the code (copied and slightly simplified from FindRoot / Applications/Solving boundary value problems, 2nd example). Use n=100 for a prettier graph.

n = 10;
f[{u_, v_}] := {v, (1 - u - u^3)/0.01};
eqns = Flatten[Join[{Subscript[u, 0], Subscript[u, n]},
    Table[{Subscript[u, i], Subscript[v, i]} - {Subscript[u, i - 1], 
       Subscript[v, i - 1]} + 
      1/(2 n) (f[{Subscript[u, i - 1], Subscript[v, i - 1]}] + 
         f[{Subscript[u, i], Subscript[v, i]}]), {i, 1, n}]]];
sv = Flatten[
   Table[{{Subscript[u, i], 0}, {Subscript[v, i], 0}}, {i, 0, n}], 
   1];
froot = FindRoot[eqns, sv];
sol = Table[Subscript[u, i], {i, 0, n}] /. froot;
ListLinePlot[sol]

This runs and produces the plot in the FindRoot documentation. The naive "solution" is to replace the subscripts with Part but it generates errors. Suggestions will be appreciated!

eqns = Flatten[Join[{u[[0]], u[[n]]},
    Table[{u[[i]], v[[i]]} - {u[[i - 1]], v[[i - 1]]} + 
      1/(2 n) (f[{u[[i - 1]], v[[i - 1]]}] + f[{u[[i]], v[[i]]}]), {i,
       1, n}]]];
sv = Flatten[Table[{{u[[i]], 0}, {v[[i]], 0}}, {i, 0, n}], 1];
froot = FindRoot[eqns, sv];
$\endgroup$

1 Answer 1

3
$\begingroup$

We can use Array as follows

n = 100;
f[{u_, v_}] := {v, (1 - u - u^3)/0.01}; U = 
 Array[u, {n + 1}, {0, n}]; V = Array[v, {n + 1}, {0, n}];

eqns = Flatten[
   Join[{u[0], u[n]}, 
    Table[{u[i], v[i]} - {u[i - 1], v[i - 1]} + 
      1/(2 n) (f[{u[i - 1], v[i - 1]}] + f[{u[i], v[i]}]), {i, 1, 
      n}]]];
sv = Flatten[Table[{{u[i], 0}, {v[i], 0}}, {i, 0, n}], 1];
froot = FindRoot[eqns, sv];

{ListLinePlot[U /. froot], ListLinePlot[V /. froot]}

Figure 1

$\endgroup$
1
  • $\begingroup$ Thanks! This is just what I was looking for. $\endgroup$ Nov 30, 2022 at 17:02

Your Answer

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

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