# Return approximation error with FindRoot

I am solving a large system of equations. I would like to know to which extent the the solution that FindRoot returns to me is accurate:

sol = FindRoot[Equations, Start];
Norm[Equations /. sol]


The issue is that the second line the ReplaceAll (/.) operator takes a lot of time to execute. Since FindRoot compiles the function (over many variables), is there a way to utilize this speedup by letting FindRoot return the error.

e.g., I would like something like:

{sol, err} = FindRoot[Equations, Start, "ErrorReturn"]


Does this exist, or is there another efficient way to obtain the error?

• I'm surprised the ReplaceAll takes ages to execute. How many equations are there? – Patrick Stevens Sep 9 '15 at 8:15
• There are 2500 Equations. However, it is strange that /. takes more time to execute than the findroot command, which evaluates the system many times – Breugem Sep 9 '15 at 8:21
• For the Fisrt case I would suggest you using NMinimize rather than FindRoot, But the definite answer lies behind your equations. Gimme an equation with error and a correct equation so I can write you the work around. – Raymond Ghaffarian Shirazi Sep 9 '15 at 9:03
• @Breugem Without specific example, I'm very suspicious about what you did. The replacement should not take that long and I'm not sure you don't have some other mistake. With the given information, it is hard to help you. – halirutan Sep 9 '15 at 11:17

## 2 Answers

f[x_?NumberQ, y_?NumberQ ] :=
(Sow[Norm[#]]; #) &@{Sin[x + 1] + Sin[y - 3], x + y}
{result, err} = Reap[FindRoot[ f[x, y], {x, 0}, {y, 0}]];

{result, Min[err[]]}


{{x -> -0.429204, y -> 0.429204}, 1.11022*10^-16}

also useful:

 ListLogPlot[err[]] Of course instead of Sow[Norm] you could Sow the whole expression list in case you want to see which part is causing the largest error.

In case your function is very slow, you can use a memoization trick like in the example below:

f[x_?NumberQ] := f[x] = (Pause[1/10];x^2 - 1);


Then the function you requested could be implemented as below:

MFindRoot[Equations_, Start_] := {FindRoot[Equations, Start,
StepMonitor :> (minsofar = Equations)], minsofar}

• All great suggestions! Saves me a lot of time! – Breugem Sep 10 '15 at 8:02