# Tracking FindRoot errors in NestWhileList and Table

I'm solving many systems of two equations by varying the initial parameters. I'm running FindRoot in a NestWhileList where the NestWhileList counter allows me to alter parameter values and feed in current solutions as the start point for the next system. My NestWhileList index runs up to 5000 and when I run it I get 8 or 9 error messages, so far of two kinds. They are

line search decreased step size to within tolerance...

General::stop: "Further output of FindRoot will be suppressed during this calculation."


Now I'd like to know for which NestWhileList index was the error generated.

This is a minimal working example similar to the code that I am running. In this example, I start with a start point sol and look for solutions. Then the counter increases, the new start point sol is the previous solution + the increment from the previous start point: add. The list d is some vector of parameter values.

d = {0, 0, 0, 0, 0};
sol = {0, 0};
counter = 5;

data = NestWhileList[{#[] + 1,{x, y} = {x, y} /.FindRoot[{x + y == #[]*#[], x- y == d[[#[]]]}, {x, #[][] + #[]  []}, {y, \#[][] + #[][]}],{x - #[][], y - #[][]}} &,{1, sol, add},Not[2 < #[][] < #[][] < 3] &, 2, counter]


Of course, this simple example gives no errors. I'd love to be able to figure out which index is (or which all indices are) generating errors so I can go look it up individually in detail and see if the error is because a solution doesn't exist or it does exist but my output is not that solution or maybe everything is fine and I can ignore the messages. I'd love it if you can tell me how this NestWhileList error tracker can be extended to a Table setting.

• Did you check Check[]? – Dr. belisarius Sep 29 '12 at 22:16
• Thanks belisarius. I wasnt aware of Check. Now I am looking at Check and CheckAbort to see if I can implement it with NestWhileList in a productive way. – Amatya Sep 29 '12 at 22:32

A combination of Reap/Sow and Check using a modified version of the OP's example:

counter = 50;
d = ConstantArray[0, {counter}];
sol = {0, 0};

data = Reap[ NestWhileList[{#[] +  1, {x, y} = {x, y} /.
Check[FindRoot[{x + y == #[]*#[],  x - 4 y ==  d[[#[]]]},
{x, #[][] + #[][]}, {y, #[][] + #[][]}],
Sow[{#[], x, y}] -> {x, y}],
{x - #[][],  y - #[][]}} &, {1, sol, add},
Not[2 < #[][] < #[][] < 3] &, 2, counter]] To get the steps and the values of {x,y} where FindRoot issued error messages use the second part of data"

data[]
(* {{{13, 115.2, 28.8}, {14, 115.2, 28.8}, {16, 180., 45.}, {17, 180.,  45.},
{18, 180., 45.}, {19, 180., 45.}, {21, 320., 80.}, {22, 320., 80.},
{23, 320., 80.}, {24, 320., 80.}, {26, 500., 125.}, {27, 500., 125.},
{28, 500., 125.}, {29, 500., 125.}, {31, 720.,  180.}, {32, 720., 180.},
{33, 720., 180.}, {34, 720., 180.}, {36, 980., 245.}, {37, 980., 245.},
{38, 980., 245.}, {39, 980.,  245.}, {41, 1280., 320.}, {42, 1280., 320.},
{43, 1280., 320.}, {44, 1280., 320.}, {46, 1620., 405.}, {47, 1620.,  405.},
{48, 1620., 405.}, {49, 1620., 405.}}}*)

• Hey Kguler, that is sweet! How did you get $x+y=a^2$ and $x-4y=0$ to show errors? Also, it looks like your result jpeg might be for the case when the second equation is $x-y=0$. This is great though.. thanks a lot! – Amatya Sep 30 '12 at 18:45
• Hey Kguler, I just implemented it and it works so well. This has saved me so much time. I'm totally filled with gratitude. Thank you. – Amatya Sep 30 '12 at 19:49
• @Amatya, thank you for the accept. I reloaded the picture of the results I get on my system for the input equations $x+y=a^2$ and $x-4y=0$. – kglr Sep 30 '12 at 21:31
• @kguler Reap and Sow might be more popular on this site +1, but I find all those torn pictures a bit overestimated. – Artes Sep 30 '12 at 22:05
• @Artes, thanks for the upvote. (removed torn picture :)) – kglr Sep 30 '12 at 22:20