Minimizing function in Matlab and C

by a.mlw.walker
Tags: function, matlab, minimizing
HW Helper
P: 6,189
 Quote by a.mlw.walker Im getting an error on this line: double *Actual_Data[5] = (double *)adata; Invalid Initializer.
Sorry, I intended:

 Quote by a.mlw.walker This function (my other function) runs but produces very different values from the matlab I had to adjust it from the nelder mead (where it ran) because of the way levmar wants x[] passed back to it however the nelder mead matched the matlab. This issue is reason 2 (small Dp) again
Well, is it a good solution that it found?
What is info[1], which is the found SSE?

To be honest, I'm not sure Levenberg-Marquardt will work as intended with your function, since it is not intended for a sum of absolute errors, but for a sum of squared errors.
I believe it should still work, but it may not find an optimal solution.
Nelder-Mead may be a better choice for this problem (minimizing the sum of absolute errors).
 P: 153 Hi so I have integrated the levmar and neldermead into one program so that I can use levmar to solve function1 and neldermead to solve function 2.What about prototypes tho? When do you need them and when do you not. For instance my functions function() and function2() dont have prototypes, however some other functions do have prototypes. My third function: static double rotor_function(int n, double z[],float ti[6])//equations for parameters eta, phi and omega^2 that hold wheel properties { double kappa = z[0], sum = 0,Rotor_Curve[6], Error_Vector[6], v0; //parameters int i; v0 = (1 + (kappa/2)*ti[0]*ti[0])/ti[0]; for (i = 0; i<6; i++) { Rotor_Curve[i] = (v0*ti[i]-(kappa/2)*pow(ti[i],2)); Error_Vector[i] = (i+1) - Rotor_Curve[i]; } for (i = 0; i<6; i++) { sum = sum + Error_Vector[i]*Error_Vector[i]; } printf("\n z = %f v0 = %f, sum = %f\n", z[0], v0, sum); return sum; } doesnt have a prototype - however it also doesnt receive the initital guess at the parameter. It is called like: case 2: /* function 2*/ z[0] = 0.004; n=1; fopt = 0; //This is still not running correctly, check matlab for correct parameter values: if (NelderMeadSimplexMethod(n, rotor_function, z, length, &fopt, timeout, eps, ti) == success) { printf("reaching to minimum "); } else { printf("timeout "); } kappa = z[0]; i need to pass the parameter as an array though I think not a variable
HW Helper
P: 6,189
 Quote by a.mlw.walker Hi so I have integrated the levmar and neldermead into one program so that I can use levmar to solve function1 and neldermead to solve function 2.What about prototypes tho? When do you need them and when do you not. For instance my functions function() and function2() dont have prototypes, however some other functions do have prototypes.
As long as you define your function (including the body) before using it, you don't need a function prototype.
If you need a prototype, you'll know, because the compiler will complain he doesn't know the function.

 Quote by a.mlw.walker I need to pass the parameter as an array though I think not a variable
 P: 153 Neldermead function is like this: status NelderMeadSimplexMethod(n, f, xinit, length, fopt, timeout, eps, array1) my function that is failing is like this: static double rotor_function(int n, double z[],float ti[6])//equations for parameters eta, phi and omega^2 that hold wheel properties { double kappa = z[0], sum = 0,Rotor_Curve[6], Error_Vector[6], v0; //parameters int i; v0 = (1 + (kappa/2)*ti[0]*ti[0])/ti[0]; for (i = 0; i<6; i++) { Rotor_Curve[i] = (v0*ti[i]-(kappa/2)*pow(ti[i],2)); Error_Vector[i] = (i+1) - Rotor_Curve[i]; } for (i = 0; i<6; i++) { sum = sum + Error_Vector[i]*Error_Vector[i]; } printf("\n z = %f v0 = %f, sum = %f\n", z[0], v0, sum); return sum; } I am calling both like this: z[0] = 0.004; n=1; fopt = 0; //This is still not running correctly, check matlab for correct parameter values: if (NelderMeadSimplexMethod(n, rotor_function, z, length, &fopt, timeout, eps, ti) == success) { printf("reaching to minimum "); } else { printf("timeout "); } kappa = z[0]; It seems to me like function2 however this one doesnt ever have a value for z[0]