help with amoeba.c (simplex downhill)


lim83
08-28-2010, 12:54 AM
hi,

i am a basic user of c, and am totally new to numerical recipes. i hope to get some help with the routine amoeba.c.

i have read through the routine outlined on pp411-412 of the 2nd edition, but i do not really understand how i can implement it in my own code, with my own model function. can someone pls show me how i can call amoeba.c (and the related amotry.c) to work on my model function:

y(x) = A exp(x^2) + B cos(Cx) + D ln(x)

where A-D are model parameters to be optimized? i have 8 experimental data points that i would like to fit the model against, and these are, say, (x1, y1), (x2,y2)...(x8,y8).

thanks in advance, and apologies for this very basic question.

John_S
08-29-2010, 11:42 AM
I have the same problem exactly. I wish somebody could reply to this! if you find the solution please email me. thanks

lim83
08-29-2010, 09:33 PM
i think the lack of replies is partly because we are using the obsolete edition of the book. i am thinking of looking up the 3rd ed.

juan.vesa
09-26-2010, 07:59 AM
hi, not sure if i understanding your question correctly,
but i am solving a 6 dimensional problem with nelder_mead algorithm atm, i have a small utility function to create an initial simplex (dimxdim+1 matrix), basically you define your original parameters guess as an array, then feed it to the function and specify a 'radius' (scale) and the function will create the simplex starting guesses for you

double** simplex(double* x, double scale, int ndims)
{
int i=0,j=0;
double** v;
if((v = newmatrix(ndims, ndims+1))==NULL) return NULL;

for(i=0;i<ndims+1;++i) {
for(j=0;j<ndims;++j) {
printf("%i %i %g\n",i,j,x[j]);
v[j][i]=x[j];
}
}

for(i=1;i<=ndims;i++) {
v[i-1][i]=x[i-1]+scale;
}

return v;
}

i.e. a simple example give an initial guess [-1, 2] and radius 0.3 the function will create a simplex matrix of:
-1 -0.7 -1
2 2 2.3
with each column holding a different point in the simplex. as far as your objective function goes, you may want to do something like:
double function(double A, double B, ...) {
y = A*exp(x*x) + B*cos(C*x) + D*log(x);
return y;
}
you'll have to change the callback function signature in your amoeba code to match the objective functions signature,
hth