MPD78
11-04-2011, 01:08 PM
Hi all,
I am looking for some help setting up the calling routine to properly call amoeba.h and solve the problem listed below.
Use the Nelder-Mead method to find the minimum of
f(x,y) =x^2-4x+y^2-y-xy
The result can be found here: http://math.fullerton.edu/mathews/n2003/neldermead/NelderMeadMod/Links/NelderMeadMod_lnk_2.html
Thanks
Matt
davekw7x
11-04-2011, 11:39 PM
...amoeba.h...to find the minimum of
f(x,y) =x^2-4x+y^2-y-xy
#include "../code/nr3.h"
#include "../code/amoeba.h"
//
// Example using amoeba
//
// Function to be minimized is
// x^2 - 4x + y^2 - y - xy
//
// The function argument is a vector whose size is 2.
// z[0] corresponds to x and z[1] corresponds to y
//
// davekw7x
//
Doub func(VecDoub_I & z)
{
return SQR(z[0]) - 4.0*z[0] + SQR(z[1]) - z[1] - z[0]*z[1];
}
int main()
{
const Doub FTOL = 1.0e-16;
const Int mp = 3;
const Int np = 2;
Doub p_array[mp*np] = {
0.0, 0.0,
1.2, 0.0,
0.0, 0.8
};
MatDoub p(mp, np, p_array);
cout << fixed << setprecision(6);
cout << "Vertices of initial simplex:" << endl << endl;
cout << setw(3) << "i" << setw(10) << "x[i]" << setw(12) << "y[i]" << endl;
cout << " --------------------------" << endl;
for (Int i = 0; i < mp; i++) {
cout << setw(3) << i;
for (Int j = 0; j < np; j++) {
cout << setw(12) << p[i][j];
}
cout << endl;
}
cout << endl;
Amoeba a(FTOL);
a.minimize(p, func);
cout << "Number of function evaluations: " << a.nfunc << endl << endl;
cout << "Vertices of final simplex and function" << endl;
cout << "values at the vertices:" << endl << endl;
cout << setw(3) << "i" << setw(10) << "x[i]" << setw(12) << "y[i]";
cout << setw(14) << "function" << endl;
cout << " --------------------------------------" << endl;
for (Int i = 0; i < mp; i++) {
cout << setw(3) << i;
for (Int j = 0; j < np; j++) {
cout << setw(12) << a.p[i][j];
}
cout << setw(12) << a.y[i] << endl;
}
return 0;
}
Output
Vertices of initial simplex:
i x[i] y[i]
--------------------------
0 0.000000 0.000000
1 1.200000 0.000000
2 0.000000 0.800000
Number of function evaluations: 118
Vertices of final simplex and function
values at the vertices:
i x[i] y[i] function
--------------------------------------
0 3.000000 2.000000 -7.000000
1 3.000000 2.000000 -7.000000
2 3.000000 2.000000 -7.000000
Regards,
Dave