Mercurial > hg > octave-lyh
diff scripts/optimization/fzero.m @ 17182:c3c1ebfaa7dc
maint: Use common indentation for switch statement.
* scripts/general/interp1.m, scripts/geometry/delaunay.m,
scripts/help/__unimplemented__.m, scripts/image/cmunique.m,
scripts/miscellaneous/edit.m, scripts/optimization/fzero.m,
scripts/optimization/sqp.m, scripts/plot/__gnuplot_drawnow__.m,
scripts/plot/hidden.m, scripts/plot/legend.m, scripts/plot/print.m,
scripts/plot/printd.m, scripts/plot/private/__contour__.m,
scripts/plot/private/__fltk_print__.m,
scripts/plot/private/__gnuplot_print__.m,
scripts/plot/private/__go_draw_axes__.m,
scripts/plot/private/__print_parse_opts__.m, scripts/signal/periodogram.m,
scripts/sparse/bicg.m, test/slice.tst, test/switch.tst:
Use common indentation for switch statement.
author | Rik <rik@octave.org> |
---|---|
date | Sun, 04 Aug 2013 15:11:34 -0700 |
parents | e7a059a9a644 |
children | bc924baa2c4e |
line wrap: on
line diff
--- a/scripts/optimization/fzero.m +++ b/scripts/optimization/fzero.m @@ -196,70 +196,70 @@ mba = mu*(b - a); while (niter < maxiter && nfev < maxfev) switch (itype) - case 1 - ## The initial test. - if (b - a <= 2*(2 * abs (u) * eps + tolx)) - x = u; fval = fu; - info = 1; - break; - endif - if (abs (fa) <= 1e3*abs (fb) && abs (fb) <= 1e3*abs (fa)) - ## Secant step. - c = u - (a - b) / (fa - fb) * fu; - else + case 1 + ## The initial test. + if (b - a <= 2*(2 * abs (u) * eps + tolx)) + x = u; fval = fu; + info = 1; + break; + endif + if (abs (fa) <= 1e3*abs (fb) && abs (fb) <= 1e3*abs (fa)) + ## Secant step. + c = u - (a - b) / (fa - fb) * fu; + else + ## Bisection step. + c = 0.5*(a + b); + endif + d = u; fd = fu; + itype = 5; + case {2, 3} + l = length (unique ([fa, fb, fd, fe])); + if (l == 4) + ## Inverse cubic interpolation. + q11 = (d - e) * fd / (fe - fd); + q21 = (b - d) * fb / (fd - fb); + q31 = (a - b) * fa / (fb - fa); + d21 = (b - d) * fd / (fd - fb); + d31 = (a - b) * fb / (fb - fa); + q22 = (d21 - q11) * fb / (fe - fb); + q32 = (d31 - q21) * fa / (fd - fa); + d32 = (d31 - q21) * fd / (fd - fa); + q33 = (d32 - q22) * fa / (fe - fa); + c = a + q31 + q32 + q33; + endif + if (l < 4 || sign (c - a) * sign (c - b) > 0) + ## Quadratic interpolation + newton. + a0 = fa; + a1 = (fb - fa)/(b - a); + a2 = ((fd - fb)/(d - b) - a1) / (d - a); + ## Modification 1: this is simpler and does not seem to be worse. + c = a - a0/a1; + if (a2 != 0) + c = a - a0/a1; + for i = 1:itype + pc = a0 + (a1 + a2*(c - b))*(c - a); + pdc = a1 + a2*(2*c - a - b); + if (pdc == 0) + c = a - a0/a1; + break; + endif + c -= pc/pdc; + endfor + endif + endif + itype += 1; + case 4 + ## Double secant step. + c = u - 2*(b - a)/(fb - fa)*fu; + ## Bisect if too far. + if (abs (c - u) > 0.5*(b - a)) + c = 0.5 * (b + a); + endif + itype = 5; + case 5 ## Bisection step. - c = 0.5*(a + b); - endif - d = u; fd = fu; - itype = 5; - case {2, 3} - l = length (unique ([fa, fb, fd, fe])); - if (l == 4) - ## Inverse cubic interpolation. - q11 = (d - e) * fd / (fe - fd); - q21 = (b - d) * fb / (fd - fb); - q31 = (a - b) * fa / (fb - fa); - d21 = (b - d) * fd / (fd - fb); - d31 = (a - b) * fb / (fb - fa); - q22 = (d21 - q11) * fb / (fe - fb); - q32 = (d31 - q21) * fa / (fd - fa); - d32 = (d31 - q21) * fd / (fd - fa); - q33 = (d32 - q22) * fa / (fe - fa); - c = a + q31 + q32 + q33; - endif - if (l < 4 || sign (c - a) * sign (c - b) > 0) - ## Quadratic interpolation + newton. - a0 = fa; - a1 = (fb - fa)/(b - a); - a2 = ((fd - fb)/(d - b) - a1) / (d - a); - ## Modification 1: this is simpler and does not seem to be worse. - c = a - a0/a1; - if (a2 != 0) - c = a - a0/a1; - for i = 1:itype - pc = a0 + (a1 + a2*(c - b))*(c - a); - pdc = a1 + a2*(2*c - a - b); - if (pdc == 0) - c = a - a0/a1; - break; - endif - c -= pc/pdc; - endfor - endif - endif - itype += 1; - case 4 - ## Double secant step. - c = u - 2*(b - a)/(fb - fa)*fu; - ## Bisect if too far. - if (abs (c - u) > 0.5*(b - a)) c = 0.5 * (b + a); - endif - itype = 5; - case 5 - ## Bisection step. - c = 0.5 * (b + a); - itype = 2; + itype = 2; endswitch ## Don't let c come too close to a or b.