Register to reply

Minimizing function in Matlab and C

by a.mlw.walker
Tags: function, matlab, minimizing
Share this thread:
I like Serena
#73
Aug22-11, 05:03 PM
HW Helper
I like Serena's Avatar
P: 6,187
Quote Quote by a.mlw.walker View Post
Im getting an error on this line:
double *Actual_Data[5] = (double *)adata;
Invalid Initializer.
Sorry, I intended:
double *Actual_Data = (double *)adata;


Quote Quote by a.mlw.walker View Post
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).
a.mlw.walker
#74
Aug24-11, 09:31 AM
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
I like Serena
#75
Aug24-11, 11:50 AM
HW Helper
I like Serena's Avatar
P: 6,187
Quote Quote by a.mlw.walker View Post
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 Quote by a.mlw.walker View Post
I need to pass the parameter as an array though I think not a variable
I can't recall the list of parameters to your Nelder-Mead function.
But if it doesn't have a "void *data" pointer as a parameter, you need to use a different mechanism, which is as follows.

The way to do it, is to define a global variable to pass your Actual_Data[] array.
You use this global variable in your function.
And just before you call Nelder-Mead, you copy your Actual_Data[] array into the global variable.
a.mlw.walker
#76
Aug24-11, 12:33 PM
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]
a.mlw.walker
#77
Aug24-11, 12:40 PM
P: 153
I found the problem. z is getting there howere the maths is going haywire! v0 is not being calculated correctly - is this because of a problem with multiplying floats and doubles?
Attached Thumbnails
Capture.JPG  
I like Serena
#78
Aug24-11, 02:42 PM
HW Helper
I like Serena's Avatar
P: 6,187
It's weird to see a parameter like "float ti[6]".
I suspect it should be "double ti[6]".
The mismatch could cause your problems.

Don't you get a compiler warning?
What is the prototype of your Nelder-Mead function, and what function prototype does it expect exactly as a parameter?
a.mlw.walker
#79
Sep9-11, 08:34 AM
P: 153
Hey how are you, been a while.
I am working more with the levenberg marquardt code now.
I am trying to get it to use just single precision - this is an option in levmar.h
Out of interest, what happens if a variable has been declared as double but in levmar.h i state i want single precision?
when I choose single precision I get an error - a strange one at that.
Axb_core.c|1103|error: 'FLT_EPSILON' undeclared (first use in this function)

It works fine with double but not with single. I find this odd considering the author offeres single precision accuracy.
Any ideas what the problem could be?


Register to reply

Related Discussions
Minimizing cost function Statistics Calculus & Beyond Homework 9
Minimizing a function with a minimum constraint... Calculus & Beyond Homework 0
Minimizing error function -> sloving linear system of equations Calculus & Beyond Homework 2
Minimizing a Function Calculus & Beyond Homework 2
Minimizing, function fit... General Math 0