// optmn.java optimize n parameters to achieve a minimum of a function // the double precision function y = f(double x[]) // srchn does a coarse global search to find initial values // provide below, function, f, for finding minimum or // modify this java to pass an external function class optmn { double f(double x[]) // user defined function for test -1 to 1 { return Math.sin(10.0*x[0]) + Math.sin(15.0*x[1]); } double optmn( // returned minimum value of function int n, // number of parameters double x[], // initial and returned optimum parameters double yfinal, // stop when y < yfinal int maxcnt, // maximum number of steps double tol, // relative tolerance on step double step[], // start and returned step size double xmin[], // minimum value for each x double xmax[]) // maximum value for each x // double (*f)(double x[]) ) // optional passed by user { double dx[] = new double[100]; // changeable step size in x double xl[] = new double[100]; // lower x value for test double xh[] = new double[100]; // higher x dalue for test double xb[] = new double[100]; // best x so far double xp[] = new double[100]; // next pivot point double dxv[] = new double[100]; double dv; double vxl, vxh, v1 ; // initial, -dx, +dx value of y double vb, vp; // best, pivot int best, i, j, k, mstep; boolean noimp; boolean debug = true; int cnt = 0; v1 =f(x); // initial evaluation noimp = true; // check for no improvement while(true) // coming in with cnt==0 gets return each iteration { // v1 and x are available vb = v1; // best value of y for(i=0; i maxcnt) break; } // end while return v1; } // end optmn double srchn( // returned minimum value of function int n, // number of parameters double x[], // returned best found int maxcnt, // maximum steps each dimension double xmin[], // minimum value for each x double xmax[]) // maximum value for each x // double (*f)(double x[]) ) // optional passed function { double dx[] = new double[100]; // step size for each parameter double step[] = new double[100]; // stem number for each parameter double xbest[] = new double[100]; // best values of x double ybest; // best value of y int i, j, k, mymax; boolean debug = false; // lots of printout double yy; // check input if(maxcnt<3) maxcnt=3; // two ends and middle of each parameter if(n>6 && maxcnt>3) maxcnt=3; if(n==6 && maxcnt>4) maxcnt=4; if(n==5 && maxcnt>10) maxcnt=10; if(n==4 && maxcnt>20) maxcnt=20; if(n==3 && maxcnt>200) maxcnt=200; mymax = 1; for(i=0; i