LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

NonLinearFitWithWeight does not erturn error if maximum number of iterations is exceeded

Solved!
Go to solution

Hi,

 

It seems to me that the function NonLinearFitWithWeight does NOT return an error if the maximum number of iterations is reached without reaching a solution - contrary to the description in the manual...

 

Previously I had reported a related bug in the function NonLinearFitWithMaxIters which was fixed in CVI2009 (bug ID 183434). However, since this function NonLinearFitWithWeight is new to CVI2009 it might well be a bug, too...

 

Wolfgang

0 Kudos
Message 1 of 15
(4,778 Views)

Wolfgang,

 

could you post your code for the parameters you have configured for the NonLinearFitWithWeight-function?

 

thanks,

Norbert

Norbert
----------------------------------------------------------------------------------------------------
CEO: What exactly is stopping us from doing this?
Expert: Geometry
Marketing Manager: Just ignore it.
0 Kudos
Message 2 of 15
(4,764 Views)

OK, here is the call:

 

fit_status = NonLinearFitWithWeight ( x_selected, y_selected, weigths, y_fit_1, fit_points, maximum_iterations, NLFitFunction1,      coefficient_array_1, coefficients_1, &squared_error, &iterations_1 );

The fit function is defined as follows:

 

double Studentt1 ( double x_value,
                   double a [],
                   int ncoef )

{
    double argument = 0.0;
    double result = 0.0;

    if ( ( a [ 2 ] > 0.0 ) &&
         ( x_value > 0.0 ) )
    {   
        if ( a [ 2 ] < 1.0 )
        {
            a [ 2 ] = 1.0;
        }
        else
        {
            a [ 2 ] = floor ( a [ 2 ] );
        }
        argument = ( a [ 2 ] + 1.0 ) / 2.0;
        result = ( x_value - a [ 1 ] );
        result = 1.0 + ( result * result / a [ 2 ] );
        result = pow ( result,
                       -argument );
        result = a [ 0 ] * Gamma ( argument,
                                   NULL ) / ( sqrt ( a [ 2 ] * PI ) * Gamma ( ( a [ 2 ] / 2.0 ),
                                                                              NULL ) );
    }
    else
    {
        result = 0.0;
    }
    return ( result );
}

For maximum_iterations the standard value is 1000, but the error occurs at settings of 5000 also. The value of iterations_1 equals the value of maximum_iterations indicating that the fit did not converge.

Weights are all equal (1.0), coefficients_1 = 3 and fit_points is something in between a few thousands and 24000. The raw data file (x) is attached, the y values are calculated according to        

 

time_slot = bin_width * TIME_RESOLUTION;
        x_min = ( time_slot / 2.0 ) + TIME_OFFSET - time_lag;
        total_counts = 0;
        for ( index = 0;
              index < data_points;
              index ++ )
        {
            FileReadLine ( file_handle,
                           line_buffer,
                           79 );
            x_data [ index ] = time_slot * index + x_min;
            y_raw [ index ] = strtod ( line_buffer,
                                       NULL );
            total_counts += y_raw [ index ];
            if ( y_raw [ index ] < y_min )
            {
                y_min = y_raw [ index ];
            }
            if ( y_raw [ index ] > y_max )
            {
                y_max = y_raw [ index ];
            }
        }

 

Actually I think that these details don't matter. Any function fit should behave similar. Because usually functions converge fast one probably has to reduce the number of permitted iterations. In my case, the function converges very slowly so I found out.

 

Wolfgang

 

 

0 Kudos
Message 3 of 15
(4,759 Views)

Wolfgang,

 

i did not try to paste your excerp into a working CVI application in order to reproduce the missing error.

I chose to work with the examples supplied with CVI instead. The examples use NonLinearFitWithMaxIters, but i modified the nlinfit.cws to NonLinearFitWithWeights.

I reduced the Maximum Iterations parameter of this function to 1 and received an error "maximum number of iterations has been exceeded". I assume that this is the error message you are referring to. So it seems to me, that the function is working as described in the help.

 

You state that the error does not occur with maximum_iterations set to 1000 in your application even though you appear to receive no valid solution. Increasing the maximum_iterations to 5000 still returns no valid solution but also returns no error. Is that correct?

 

Norbert

Norbert
----------------------------------------------------------------------------------------------------
CEO: What exactly is stopping us from doing this?
Expert: Geometry
Marketing Manager: Just ignore it.
0 Kudos
Message 4 of 15
(4,750 Views)

Norbert,

  

Thanks for assistance! May be this is a good idea to modify a simple sample file and try to reproduce the problem there. What I can tell is that the problem could be reproduced for different data sets, file sizes, and starting parameters.

 

My complaint with the function was that it may stop after say 1000 iterations (having reached the specified maximum number of iterations) without having found a reasonable solutions. This could be verified by increasing the permitted number of iterations to 5000. Then the fit ended only after 5000 iterations, in both cases without error.

 

 

Let me take the opportunity and suggest a product feature: In this and related cases it would be very useful to be able to export a variable array. This would make it much easier producing a 'test version'. You see, typically the data are selected and processed before being fitted, that is, it's not straightforward to provide test data. While there is a nice feature of a graphical array view, there is no possibility to extract the data: cut and paste does not work using the array display.

 

Wolfgang

0 Kudos
Message 5 of 15
(4,744 Views)

Hi Wolfgang,

 

If I understand your suggestion correctly, this might be something that you can already do. If you view the array variable in the Array Display (to do so, right-click on the variable and select "View Variable Value") you then have the option to export the array values, either in binary or in text form, by clicking File>>Output>>...

 

Luis

Message 6 of 15
(4,731 Views)

Hi Luis,

 

Thanks! I wasn't aware of this output option. I have been searching for such a possibility already earlier but was blinded by the many other features 🙂

 

Good to know and have!

 

Wolfgang

 

0 Kudos
Message 7 of 15
(4,726 Views)

Hi Norbert,

 

I have prepared a simpler example where I could reproduce the error reported: In the function call 

fit_status = NonLinearFitWithWeight ( x, y, w, y_fit, 781, 4, NLFitFunction, coeff_array, 3, &squared_error, &iterations_1 );

 

the function returns with a fit_status of 0 (no error), although the number of iterations needed, iterations_1, is also 4.

If you call the same function and allow 5 iterations, instead of 4, it will return, without error, telling that it needed 5 iterations...

Repeating this for 6 and larger works fine in this case, and for 1,2, or three iterations it indeed reports an error as expected.

 

However, it does not return an error for 4 or 5 iterations! I have other, more complex situations where even at 1000 iterations no error is reported, although the number of iterations is even larger...

 

OK, here the definition of the fit function NLFitFunction (a simple Gaussian):

 

double NLFitFunction ( double x_value, double a [], int ncoef )

{
    double result = 0.0;

    if ( ( a [ 2 ] != 0.0 ) &&
         ( x_value > 0.0 ) )
    {   
        result = ( x_value - fabs ( a [ 1 ] ) ) / fabs ( a [ 2 ] );
        result = fabs ( a [ 0 ] ) * exp ( -0.5 * result * result ) / ( sqrt ( 2.0 * PI ) * fabs ( a [ 2 ] ) );
    }
    else
    {
        result = 0.0;
    }
    return ( result );
}

and the data to reproduce the behavior:

 

 

Download All
0 Kudos
Message 8 of 15
(4,594 Views)
because only three attachments are possible per post, here are the two other data files:
Download All
0 Kudos
Message 9 of 15
(4,593 Views)

This morning, preparing a demo code, I just realized that I have provided wrong data files - sorry for the confusion.

 

So now I am attaching the demo code to demonstrate the erroneous behavior and the corrected data files: With this everybody should be able to reproduce this bug.

 

It would be nice to hear about it, and I would also like to hear when this bug will be fixed: Essentially it means that the nonlinear fitting routine cannot be trusted: it may claim that the fit has converged while it has not. This is severe!!

 

In the code, I am calling the same routine three times; the first time it correctly claims that the maximum number of iterations was exceeded.

 

However, it does not report this error for 4 and 5 iterations, although 5 iterations are needed!

0 Kudos
Message 10 of 15
(4,524 Views)