2
$\begingroup$

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?

$\endgroup$
  • 1
    $\begingroup$ I'm surprised the ReplaceAll takes ages to execute. How many equations are there? $\endgroup$ – Patrick Stevens Sep 9 '15 at 8:15
  • 1
    $\begingroup$ There are 2500 Equations. However, it is strange that /. takes more time to execute than the findroot command, which evaluates the system many times $\endgroup$ – Breugem Sep 9 '15 at 8:21
  • $\begingroup$ 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. $\endgroup$ – Raymond Ghaffarian Shirazi Sep 9 '15 at 9:03
  • 2
    $\begingroup$ @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. $\endgroup$ – halirutan Sep 9 '15 at 11:17
2
$\begingroup$
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[[1]]]}

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

also useful:

 ListLogPlot[err[[1]]]

enter image description here

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.

$\endgroup$
3
$\begingroup$

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}
$\endgroup$
  • $\begingroup$ All great suggestions! Saves me a lot of time! $\endgroup$ – Breugem Sep 10 '15 at 8:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.