- #1

- 8

- 0

## Homework Statement

I am working on a program that queries an electronic instrument in order to find a value x for which the instrument will return a minimum value. I can provide any value for x that I want and the instrument will return the result f(x). The function has only one minimum. In order to test my algorithm, I created a function and I would like to know how can find the minimum of this function using as few tries as possible. Once I am pretty happy with the algorithm, I will implement it in the real program. I thought of a simple algorithm (which I admit is pretty dumb and it may not converge for a slightly different function or for any start value of x). With my algorithm, if I start with x = 0, it will take me 35 tries to identify x for which f(x) is minimum (when x = -4.8671, f(x) = 0.9426).

## Homework Equations

f(x) = 0.5*(0.29*x*(x+2)+0.22*x+3-3*sqrt(x*x-3*x+9)+12.88-x/1.33);

## The Attempt at a Solution

a. choose a random start value

b. choose a random step

c. query the function what is the result yleft = f(xstart - step)

d. query the function what is the result yright = f(xstart + step)

e. query the function what is the result of y = f(xstart)

f. if yleft < yright, then x must be somwhere to the left, so xleft = x-2*step, xright = x

g. if yright < yleft, xleft = x, xright = x+2*step

h. More details in the sample program provided below.

x = 0;

xStep = 2.0;

y = Function(x);

xl = x - xStep;

xr = x + xStep;

do {

n++;

yl = Function(xl);

yr = Function(xr);

x = (xl + xr) / 2;

y = Function(x);

if(yl <= yr && yl <= y) {

Dir = Left; //minimum is probably to the left of X, but not necessarily!

x = (xl + xr) / 2;

xr = x;

xl = xl - 2 * xStep;

}

else if(yr <= yl && yr <= y) {

Dir = Right; //minimum is probably to the right of X, but not necessarily!

x = (xl + xr) / 2;

xl = x;

xr = xr + 2 * xStep;

}

else if(y <= yl && y <= yr) {

Dir = Center;

xStep = xStep * 0.70; // I found by trial and error that 0.70 is a value that converges decently fast

xl = xl + xStep;

xr = xr - xStep;

}

}

while(fabs(yr - yl) > 0.0001 && n < 100);

Certainly there are much better algorithms than this one, so any suggestions are welcome.

Kind regards,

Nicolae

Last edited: