# How to set initial guess in NDSolve

I'm trying to solve a BVP using NDSolve and I want to impose the starting initial guess. I googled and looked at the help, but I couldn't find this option.

NDSolve[{
A[u[x]] u''[x] == F[x],
u == u0,
u == u1
},u[x],{x,0,1}]


where A[u] = u^2 + u^3 + u^4 or something like that.

How can I set the starting guess?

• This doesn't look like a differential equation: u'[x] (and u''[x]) doesn't appear anywhere. NDSolve will only work with differential equations. Please post complete code (complete meaning that it's possible to just copy and paste it, and it'll work without additional definition). – Szabolcs May 30 '14 at 12:57
• @Szabolcs Thanks for your reply, you are right, I adjusted my code. I'd like to know how to add the initial guess without using shooting method. – Petrus May 30 '14 at 13:32
• To clarify: Mathematica uses the shooting method for boundary value problems by default. THe shooting method does not use an "initial guess for the function". What method are you looking to use instead? – Szabolcs May 30 '14 at 14:14

Perhaps you are looking for something similar to the following where you add a "temporal" dimension to the problem and look for stabilized behavior as time approaches Infinity. For example, if the original differential equation is:

y[x]^2 y''[x] == -10 Sin[2 Pi x] Exp[-x]


with BCs

y==1, y==2


this can be solved using the shooting method described above (although I use a slightly different method):

soltest =
ParametricNDSolveValue[{y[x]^2 y''[x] == -10 Sin[2 Pi x] Exp[-x],
y == 1, y' == k}, y, {x, 0, 1}, {k}];
kval = k /. FindRoot[soltest[k] == 2, {k, 2}][];
Plot[soltest[kval][x], {x, 0, 1}, PlotRange -> {1, 2}, Axes -> False,
Frame -> True, PlotStyle -> Thick,
FrameLabel -> {{y[x], Null}, {x, "Shooting Method"}},
LabelStyle -> 14, ImageSize -> Large ] but it can also be solved by attacking the following pde:

y[t, x]^2 Derivative[0, 2][y][t, x] - Derivative[1, 0][y][t, x] == -10 Sin[2 Pi x] Exp[-x]


(note the addition of a "time"-dependence and a term that depends on the first derivative with respect to "time"). Now we need the appropriate boundary conditions in the spatial dimension and an initial condition for "temporal" one (let's start with an appropriate linear function in x as a "guess"):

y[t,0] == 1, y[t,1] == 2, y[0,x] == 1 + x


plug into NDSolve and lets try to see if we see stability after t -> 1/2.

sol1 = NDSolveValue[{y[t, x]^2 Derivative[0, 2][y][t, x] -
Derivative[1, 0][y][t, x] == -10 Sin[2 Pi x] Exp[-x],
y[t, 0] == 1, y[t, 1] == 2, y[0, x] == 1 + x},
y, {t, 0, 1/2}, {x, 0, 1}] Looks pretty good. For fun I also used a different initial condition as well (quadratic):

sol2 = NDSolveValue[{y[t, x]^2 Derivative[0, 2][y][t, x] -
Derivative[1, 0][y][t, x] == -10 Sin[2 Pi x] Exp[-x],
y[t, 0] == 1, y[t, 1] == 2, y[0, x] == 1 + x^2},
y, {t, 0, 1/2}, {x, 0, 1}]


Finally I made an animation to show how the solutions relax into each other:

Animate[Plot[{sol0[x], sol1[t, x], sol2[t, x]}, {x, 0, 1},
PlotRange -> {1, 2}, Axes -> False, PlotStyle -> Thick,
Frame -> True,
FrameLabel -> {{y[Round[t, 0.01], x], Null}, {x, Null}},
LabelStyle -> 14, ImageSize -> Large,
PlotLegends -> {"Shooting", "linear", "quad"}], {t, 0, 1/4}] • Thanks very much for your reply and sooty for this delayed reply, but I got some troubles in real life! Your answer seems to fit what I wanna do. I'll try with this! – Petrus Jun 3 '14 at 13:48

This is documented in the Advanced Numerical Differential Equation Solving tutorial, which is the place to go to for more involved NDSolve question. (NDSolve is very well documented, but you need to know where to look for the documentation ...)

You'll find all the information under that link, but I'll copy the documentation example here for reference:

NDSolve[{x''[t] + Sin[x[t]] == 0, x == x == 0}, x, t,
Method -> {"Shooting", "StartingInitialConditions" -> {x == 0, x' == 1.5}}]


The key is the "StartingInitialConditions" option which allows specifying a set of starting conditions for $u$ and $u'$ at an arbitrary point (doesn't need to be one of the endpoints).

• Thanks again for your reply. Using a shooting method, you give a "suggestion" only for the value of the function and its derivative a fixed point. I'd like to pass "all points" of the initial guess (for instance, a linear function between my bcs). Is it possible to do that? – Petrus May 30 '14 at 13:51
• @Petrus That is how the shooting method works: you parametrize the solution in terms of initial conditions, and solve for the parameter to obtain the desired boundary conditions. Are not looking to use the shooting method? "Then what method are you looking for? Your question is not clear. "How can I set the starting guess?" makes no sense without a context. You have to explain what numerical method you want to use. – Szabolcs May 30 '14 at 13:57
• Sorry for this delayed reply, but I got some troubles in real life! When you solve a nonlinear differential equation using a fem approach (or simply a nonlinear system using a newton method) you need a guess initial value for "each" point before starting your loop cycle. I want to do something similar using NDSolve – Petrus Jun 3 '14 at 13:46