I am trying to minimize a function for three parameters. The code for main and the function I am trying to minimize are below. It runs but doesnt get to the correct solution. I am running the same thing in Matlab and it gets it right.
Just wondering if anyone can see a reason it may not converge as accurately as the equivelent matlab.
Thanks
Alex
Code:
double Actual_Output[5];
double Input[5];
double a_global, b_global;
//double x_coeff_global;
float init_T[23][2]={0};
float eta_glob, phi_glob, omega_2_glob, c1_glob;
float Root(float left, float right, float tol, int *count);
int num;
float f(float x);
//Function to find physical parameters a and b (stored globally as a_global and b_global)
main()
{
float left, right, tol, fall_angle, fall_time, guess, c0, xx;
int n, i,num_inputs = 23; //number of parameters to search for
double x[3], y[3];
double length = 1.00;
double fopt;
int timeout = 10000;
double eps = 1.0e-12;//accuracy of Nelder Mead
left = 0;
tol = 0.0001;//bisection accuracy
FILE *fp;
//Starting estimates for parameters a and b (Check documentation (EQN 40))
x[0] = 0.5;//a
x[1] = 1;//b
x[2] = -1;//b
//Timing Values for 5 revolutions
Actual_Output[0] = 1.2;
Actual_Output[1] = 2.693;
Actual_Output[2] = 4.325;
Actual_Output[3] = 6.131;
Actual_Output[4] = 8.125;
//Revolution Number corresponding with revolution time above
Input[0] = 1;
Input[1] = 2;
Input[2] = 3;
Input[3] = 4;
Input[4] = 5;
n=3;
//calculating physical condition parameters a and b (stored as a_global and b_global)
if (NelderMeadSimplexMethod(n, function, x, length, &fopt, timeout, eps) == success) {
printf("reaching to minimum ");
} else {
printf("timeout ");
}
printf(" [ %lf %lf %lf] %lf\n", x[0], x[1],x[2], fopt);
//At this point a_global and b_global are assigned. These are required for fall parameters to calculate c1:
//beta:
//beta = a_global*b_global*b_global;
return 0;
}
static double function(int n, double x[])
{
double c;
double Fitted_Curve[5];
double Error_Vector[5];
int i;
double a, b, sum = 0;
// Actual_Output[5] = {1.2, 2.693, 4.325, 6.131, 8.125};
a = x[0];
b=x[1];
c=x[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];
}
for (i = 0; i <= 4; i++)
{
sum = sum + Error_Vector[i]*Error_Vector[i];
}
printf("sum = %f\n", sum);
a_global = a;
b_global = b;
// x_coeff_global = x_coeff;
return sum;
}