02-25-2010 11:39 AM
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
Solved! Go to Solution.
02-26-2010 03:11 AM
Wolfgang,
could you post your code for the parameters you have configured for the NonLinearFitWithWeight-function?
thanks,
Norbert
02-26-2010 05:35 AM
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
02-26-2010 07:05 AM
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
02-26-2010 07:34 AM
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
02-26-2010 10:56 AM
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
02-26-2010 11:09 AM
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
04-28-2010 06:17 AM
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:
04-28-2010 06:18 AM
05-04-2010 03:30 AM
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!