IMSL C Stat Library
Return Values from User-Supplied Functions
All values returned by user-supplied functions must be valid real numbers. It is the user’s responsibility to check that the values returned by a user-supplied function do not contain NaN, infinity, or negative infinity values.
In addition to the techniques described below, it is also possible to instruct the IMSL C Stat Library to return control to the calling program in case an unrecoverable error occurs within a user-supplied function. See function imsls_set_user_fcn_return_flag for a description of this feature.
Example
 
#include <imsls.h>
#include <math.h>
#include <stdio.h>
 
float fcn(int, float[], int, float[]);
 
int main ()
{
#define N_OBSERVATIONS 4
int n_independent = 1;
int n_parameters = 2;
float *theta_hat;
float x[N_OBSERVATIONS][1] = {10.0, 20.0, 30.0, 40.0};
float y[N_OBSERVATIONS] = {0.48, 0.42, 0.40, 0.39};
/* Nonlinear regression */
theta_hat = imsls_f_nonlinear_regression(fcn, n_parameters,
N_OBSERVATIONS, n_independent, (float *)x, y, 0);
/* Print estimates */
imsls_f_write_matrix("estimated coefficients", 1, n_parameters,
theta_hat, 0);
} /* End of main */
 
float fcn(int n_independent, float x[], int n_parameters,
float theta[])
{
float result, exparg;
exparg = theta[1]*(x[0] - 8);
/* check that argument to exp does not get too large */
if (exparg > 10.) {
result = 22000.;
} else {
result = theta[0] + (0.49 - theta[0])*exp(exparg);
}
}