# Iterating FindRoot to solve a differential equation

I am new to Mathematica style programming, better at traditional programming. I would like to know if I can create a table of results using FindRoot, where the solution from the current FindRoot call becomes the start point for the next FindRoot call. I would like to create a For loop with a set number of iterations.

### Update

In the comment section someone asked what I was trying to achieve with an iterated FindRoot. What I am trying to solve is the following differential equation using numerical methods:

NDSolve[{Exp[y'[x]] + Exp[-y[x]] == 1, y == 1}, y, {x, 0, 5}]


Mathematica does not like this equation, and gives me the error:

NDSolve::ndnum: Encountered non-numerical value for a derivative at x == 0.

I have not seen a differential equation like this before, but thought it would be interesting to see what kind of numerical solution it would give. My way around this is to use FindRoot and a finite difference equation to find y[x].

FindRoot[Exp[(u1 - u0)/h] + Exp[-u0] == 1, {u1, u0}]


I start with u0 = 1, and h = 0.1. I can then solve for u1. I then use u1 as the new u0 and use FindRoot again to find the next u1. That is where I need to iterate the FindRoot function. If someone has a better idea I would appreciate it. I will try the Nest function, and I saw another similar question on StackExchange elsewhere, and will try that solution too.

• You are looking for the Nest family of functions. Se e.g. the documentation for Nest here: reference.wolfram.com/language/ref/Nest.html – MarcoB Sep 8 '15 at 1:12
• @MarcoB is correct, but why would you want to do this? – bbgodfrey Sep 8 '15 at 1:26
• Welcome to Mathematica.SE! I hope you will become a regular contributor. To get started, 1) take the introductory tour now, 2) when you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge, 3) remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign, and 4) give help too, by answering questions in your areas of expertise. – bbgodfrey Sep 8 '15 at 1:27
• This is not difficult to do, what you describe with approach better than a loop one, but as it is described now it does not make sense. Indeed, FindRoot  already returns the result of a Newton iteration with a fixed precision. So, no use to iterate it further. If you have in mind something more complex than a simple equation (or a system) to help we should see its explicit form. – Alexei Boulbitch Sep 8 '15 at 12:25
• Try s = First@NDSolve[{y'[x] == Log[1 - Exp[-y[x]]], y == 1}, y, {x, 0, 5}] – bbgodfrey Sep 13 '15 at 22:24

As I noted in an earlier comment, the ODE to be solved can be rewritten as

eq = y'[x] == Log[1 - Exp[-y[x]]]


Then,

s = First@NDSolve[{y'[x] == Log[1 - Exp[-y[x]]], y == 1}, y, {x, 0, 5}];
Plot[Evaluate[ReIm[y[x] /. s]], {x, 0, 5}, AxesLabel -> {x, y}] where Re[y] is blue, and Im[y] is tan. Because the ODE is singular at y == 0, it is natural to ask whether the solution is valid beyond that point.

Plot[Evaluate[ReIm[(Exp[y'[x]] + Exp[-y[x]] - 1) /. s]], {x, 0, 5},
AxesLabel -> {x, "eq"}] shows that it is.

• Thanks for the more detailed answer. You say that the second plot indicates that the solutions beyond the point y==0 are valid. I don't know if this is an ODE or a Mathematica piece of information, but can you explain why the second graph shows that the validity? What would the graph look like if it were not valid? – PMay Sep 15 '15 at 3:34
• And what is the purpose of the ampersand symbol after the First function call. In the Mathematica help, they do not use the ampersand. – PMay Sep 15 '15 at 3:36
• @PMay The second figure plots the ODE evaluated for the solution s. If this solution were incorrect, the values shown in the second plot would be much larger. In fact, you can solve the ODE with high WorkingPrecision, and the residual error shown in the second plot would be smaller still. In answer to your second comment, First@NDSolve[...] is equivalent to First[NDSolve[...]]. Look up Prefix. Thanks for accepting my answer. Best wishes. – bbgodfrey Sep 15 '15 at 3:54
• Just a remark. The plot of Re, Im is wrong. The function y is not singular in 1, its derivative is, y has a vertical drop. Yet the plot shows that y has a minimum at around x=1.5, thus implying that y' =0 at that point. That means 1-Exp[-y]=0 and of course that cannot happen unless y=+Inf. – Ed 209 Jul 17 '18 at 8:52