- 16,335
- 258
If you upload neldermead.c, I'll take a look at it.
a =
1
b =
1
c =
-1
Fitted_Curve =
6.1378 12.4210 18.7041 24.9873 31.2705
Error_Vector =
-4.9378 -9.7280 -14.3791 -18.8563 -23.1455
sse =
1.2171e+003
Iteration Func-count min f(x) Procedure
0 1 1217.05
a =
1.0500
b =
1
c =
-1
Fitted_Curve =
6.1447 12.4279 18.7111 24.9943 31.2774
Error_Vector =
-4.9447 -9.7349 -14.3861 -18.8633 -23.1524
sse =
1.2180e+003
a =
1
b =
1.0500
c =
-1
Fitted_Curve =
5.8455 11.8295 17.8135 23.7975 29.7814
Error_Vector =
-4.6455 -9.1365 -13.4885 -17.6665 -21.6564
sse =
1.0681e+003
% Perform an inside contraction
xcc = (1-psi)*xbar + psi*v(:,end);
x(:) = xcc; fxcc = funfcn(x,varargin{:});
func_evals = func_evals+1;
if fxcc < fv(:,end)
v(:,end) = xcc;
fv(:,end) = fxcc;
how = 'contract inside';
else
% perform a shrink
how = 'shrink';
end
end
if strcmp(how,'shrink')
for j=two2np1
v(:,j)=v(:,1)+sigma*(v(:,j) - v(:,1));
x(:) = v(:,j); fv(:,j) = funfcn(x,varargin{:});
end
func_evals = func_evals + n;
end
mathworks said:The algorithm first makes a simplex around the initial guess x0 by adding 5% of each component x0(i) to x0, and using these n vectors as elements of the simplex in addition to x0.
void ros(double *p, double *x, int m, int n, void *data)
{
register int i;
for(i=0; i<n; ++i)
x[i]=((1.0-p[0])*(1.0-p[0]) + ROSD*(p[1]-p[0]*p[0])*(p[1]-p[0]*p[0]));
}
static double function(double *p, double *sum, int m, int n, void *data)
{
double Fitted_Curve[5] = {0};
double Error_Vector[5] = {0};
int i;
double a, b, c;
sum = 0;
// double v1[5], v2[5], geom_inv[5], norm = 0, norm_2 = 0, v2sum = 0, x_coeff = 0;
// Actual_Output[5] = {1.2, 2.693, 4.325, 6.131, 8.125};
// printf("Actual_Output[0[ = %f", Actual_Output[0]);
a = p[0];//parameters
b=p[1];
c=p[2];
for (i = 0; i <= 4; i++)
{
Fitted_Curve[i] = (1/(a*b))*(c-asinh(sinh(c)*exp(a*Input[i]*2*pi)));
Error_Vector[i] = Actual_Output[i]-Fitted_Curve[i];
printf("a(x[0]) = %f, b(x[1]) = %f, c(x[2]) = %f\n",p[0], p[1], p[2]);
printf("Fitted_Curve= %f\n", Fitted_Curve[i]);
printf("Error_Vector= %f\n", Error_Vector[i]);
}
for (i = 0; i <= 4; i++)
{
sum = sum + pow(Error_Vector[i],2);
printf("sum= %f\n", sum);
}
a_global = a;
b_global = b;
// x_coeff_global = x_coeff;
return sum;
}
int dlevmar_dif(
void (*func)(double *p, double *hx, int m, int n, void *adata), /* functional relation describing measurements.
* A p \in R^m yields a \hat{x} \in R^n
*/
double *p, /* I/O: initial parameter estimates. On output contains the estimated solution */
double *x, /* I: measurement vector. NULL implies a zero vector */
int m, /* I: parameter vector dimension (i.e. #unknowns) */
int n, /* I: measurement vector dimension */
int itmax, /* I: maximum number of iterations */
double opts[5], /* I: opts[0-4] = minim. options [\tau, \epsilon1, \epsilon2, \epsilon3, \delta]. Respectively the
* scale factor for initial \mu, stopping thresholds for ||J^T e||_inf, ||Dp||_2 and ||e||_2 and the
* step used in difference approximation to the Jacobian. If \delta<0, the Jacobian is approximated
* with central differences which are more accurate (but slower!) compared to the forward differences
* employed by default. Set to NULL for defaults to be used.
m=3; n=5;
p[0]=1.0; p[1]=1.0; p[2]=-1.0;
for(i=0; i<n; i++) x[i]=0.0;
//ret=dlevmar_der(ros, jacros, p, x, m, n, 1000, opts, info, NULL, NULL, NULL); // with analytic Jacobian
ret=dlevmar_dif(function, p, x, m, n, 10000, NULL, info, NULL, NULL, NULL); // no Jacobian
break;
double p[5], // 5 is max(2, 3, 5)
x[16]; // 16 is max(2, 3, 5, 6, 16)
for(i=0; i<LM_INFO_SZ; ++i)
printf("%g ", info[i]);
printf("\n");
O: information regarding the minimization. Set to NULL if don't care
* info[0]= ||e||_2 at initial p.
* info[1-4]=[ ||e||_2, ||J^T e||_inf, ||Dp||_2, \mu/max[J^T J]_ii ], all computed at estimated p.
* info[5]= # iterations,
* info[6]=reason for terminating: 1 - stopped by small gradient J^T e
* 2 - stopped by small Dp
* 3 - stopped by itmax
* 4 - singular matrix. Restart from current p with increased \mu
* 5 - no further error reduction is possible. Restart with increased mu
* 6 - stopped by small ||e||_2
* 7 - stopped by invalid (i.e. NaN or Inf) "func" values; a user error
* info[7]= # function evaluations
* info[8]= # Jacobian evaluations
* info[9]= # linear systems solved, i.e. # attempts for reducing error