7
$\begingroup$

I would like to know whether there is a way to monitor the evaluation of the norm of the error of FindRoot. Sometimes the evaluation of of this for a large system can takes minutes, hours or days, and I would like to see which parameters (e.g., damping factor) can lead to improvement of the performance of FindRoot.

e.g., Suppose I solve the complex system

FindRoot[Equations, Start];

I would like to get an update at every iteration about: 1) the iteration number 2) the norm of error of the function

$\endgroup$
2
  • 4
    $\begingroup$ Just in case you haven't seen this reference.wolfram.com/mathematica/tutorial/… The package UnconstrainedProblems has a few interesting functions- $\endgroup$ Sep 9, 2015 at 8:12
  • $\begingroup$ I was not aware of this! The plotting takes time to execute but would provide great intuition for test cases. Thanks! $\endgroup$
    – Breugem
    Sep 9, 2015 at 8:24

1 Answer 1

13
$\begingroup$

You can use EvaluationMonitor to print progress. For example:

n = 0;
f[x_] := Cos[x];
FindRoot[f[x], {x, 8}, EvaluationMonitor :> (n++; Print[{n, f[x]^2}])]

Gives:

{1,0.0211703}
{2,1.09558*10^-6}
{3,1.46112*10^-19}
{4,9.3735*10^-32} 
{x -> 7.85398}
$\endgroup$
7
  • $\begingroup$ works great! Is there any way the stepsize could be influenced? $\endgroup$
    – Breugem
    Sep 9, 2015 at 8:41
  • $\begingroup$ @Breugem What do you mean? Only to show the progress every 5 steps, say? or change the internal step size of FindRoot? $\endgroup$
    – yohbs
    Sep 9, 2015 at 8:44
  • $\begingroup$ I was referring to the internal step size of Findroot $\endgroup$
    – Breugem
    Sep 9, 2015 at 8:47
  • 1
    $\begingroup$ @Breugem FindRoot does not use fixed steps! Do you know the Newton method for finding roots? There the step-size depends on the gradient and the function-value. For other methods used by FindRoot the situation is equivalent. Therefore, you can not influence the step-size directly through an option. $\endgroup$
    – halirutan
    Sep 9, 2015 at 11:08
  • 2
    $\begingroup$ @Breugem Here you get the stepsizes: L = {}; f[x_] = Cos[x]; FindRoot[f[x], {x, 8}, EvaluationMonitor :> (AppendTo[L, x]; Print[Abs[f[x]]])] and then StepSizes = Differences[L] $\endgroup$
    – Coolwater
    Sep 9, 2015 at 13:32

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