Search code examples
gnuplot

Tell gnuplot to ignore functions it cannot fit


I am trying to fit various data sets with gnuplot to a certain fitting function. Some of my data files cannot be fitted to the functions (since my data is noisy and they contain too many NaNs) and some of the fitting functions don't work (as described in this question: Troubles fitting exponential with gnuplot) since I do not specify initial values for my fit parameters.

One solution, of course, is to carefully choose fit parameters for each of the data sets, and not try to fit functions which are obviously unsuited to the candidate fit-function. However, since I just want to get a feel for the data, and since there are around 1000 plots I need to make, is there a way that I can specify some option like "if fitting fails, set fit function f(x)=0"?


Note: How to tell gnuplot to ignore non-converging fits did not answer my question; when I run gnuplot (gnuplot-qt, Linux, v. 5.2 patchlevel 8) then for some of my data sets it fails with an error like

Current data point
=========================
#               = 1 out of 3
x               = 5.93738        
z               = 5.97972        

Current set of parameters
=========================
a             = 5.93738        
b             = 25.7958        
c             = 73.596         

         "fitting.gp" line 68: Function evaluation yields NaN ("not a number")

gnuplot> 

(here a,b,c are my fit parameters and I am running gnuplot by entering gnuplot > load "filename".


Edit: in addition to the fit function breaking down because it is undefined for certain parameter combinations, sometimes fitting also fails with the error "Singular matrix in Invert_RtR". In all of these cases I want to tell gnuplot to continue attempting to fit the data sets that it can, and to plot the resulting datasets anyway (and fits, where available).


Solution

  • I don't think there is a gnuplot only solution.

    There is the following comment in the source code (src/fit.c):

    /* FIXME: It would be nice to exit the "fit" command non-fatally, */
    /* so that the script who called it can recover and continue.     */
    /* int_error() makes that impossible.  But if we use int_warn()   */
    /* instead the program tries to continue _inside_ the fit, which  */
    /* generally then dies on some more serious error.                */
    

    These lines are in the source of my gnuplot 5.0.5 and in the current source code (5.4?).