AndyB
11-11-2011, 03:22 PM
I have been reading the code in the section on Brent's method.
I can see that the code choses Inverse Quadratic Interpolation, linear interpolation and bisection for the next position. But the book keeping leaves me with a question.
Variables b and c bracket the zero and variable a (mostly) holds the last value of b. Of the two bracket values it is arranged that b is the one that has the function value closest to zero.
The code that arranges that b has the lowest function value does the following:
if (fabs(fc) < fabs(fb)){
a = b;
b = c;
c = a;
fa = fb;
fb = fc;
fc = fa;
}
which swaps b and c (and fb and fc) but also sets a to the higher of the bracket values (ie at the end a is the same as c). This is a situation where a no longer is the previous value of b.
Does anyone know why this code changes the value of a, rather than just swapping b and c?
Thanks
AndyB
I can see that the code choses Inverse Quadratic Interpolation, linear interpolation and bisection for the next position. But the book keeping leaves me with a question.
Variables b and c bracket the zero and variable a (mostly) holds the last value of b. Of the two bracket values it is arranged that b is the one that has the function value closest to zero.
The code that arranges that b has the lowest function value does the following:
if (fabs(fc) < fabs(fb)){
a = b;
b = c;
c = a;
fa = fb;
fb = fc;
fc = fa;
}
which swaps b and c (and fb and fc) but also sets a to the higher of the bracket values (ie at the end a is the same as c). This is a situation where a no longer is the previous value of b.
Does anyone know why this code changes the value of a, rather than just swapping b and c?
Thanks
AndyB