0

I am having some issues with finding good initial conditions for a fit that just won't behave. So I had the idea to Brute-force it by inserting it in a loop that tells gnuplot to keep fitting until he gets a relative deviation of less than 100% while randomly changing initial values each time he re runs the loop, i wrote something like that:

while(abs(m_err/m) > 1){
    m = rand(0)*0.3
    k = rand(0)
    x_0 = 15 + rand(0)*10 
    fit logi(x) 'data.csv' u 1:8 via m,k,x_0
}   

i multiply the rand function so that i get values near the value i expect to be good for fitting.

This method obviously results in a lot of fits not converging and stopping the script mid-loop. How can i tell gnuplot to simply ignore this and just re-run the randomization and fitting loop if the fit doesn't converge or it gives a singular matrix?

Uwe Allner
  • 3,399
  • 9
  • 35
  • 49
Defcon97
  • 111
  • 3

1 Answers1

2

It might help to know what version of gnuplot you are using. So far as I know non-convergence is not a fatal error in current gnuplot. It prints an error message but should continue anyway. If that is not the case, you might try setting a maximum number of iterations and then testing for convergence yourself when the fit returns. E.g.

set fit maxiter 100   # no limit by default
set fit limit 1.e-5   # this is the default convergence criterion
do for [i=1:100] {
  a = something + i*0.01
  b = something_else
  fit f(x) 'datafile' using 1:2 via a, b
  if (FIT_ITER < 100) break;   # It converged!
}
if (FIT_ITER >= 100) {
  print "No starting point led to convergence."
  exit
}
Ethan
  • 13,715
  • 2
  • 12
  • 21