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];