--- nlopt-2.3/cobyla/cobyla.c	2012-07-20 20:13:42.000000000 +0200
+++ nlopt-2.3.1/cobyla/cobyla.c	2013-07-25 09:49:25.000000000 +0200
@@ -186,7 +186,7 @@
      unsigned i, j;
      func_wrap_state s;
      nlopt_result ret;
-     double rhobeg, rhoend;
+     double rhobeg, rhoend, t;
 
      s.f = f; s.f_data = f_data;
      s.m_orig = m;
@@ -204,11 +204,19 @@
      s.ub = nlopt_new_rescaled(n, s.scale, ub);
      if (!s.ub) { ret = NLOPT_OUT_OF_MEMORY; goto done; }
 
+     for (j = 0; j < n; ++j)
+         if (s.ub[j] < s.lb[j]) {
+             t = s.lb[j]; 
+             s.lb[j] = s.ub[j]; 
+             s.ub[j] = t;
+     }
+
+
      s.xtmp = (double *) malloc(sizeof(double) * n);
      if (!s.xtmp) { ret = NLOPT_OUT_OF_MEMORY; goto done; }
 
      /* SGJ, 2008: compute rhoend from NLopt stop info */
-     rhobeg = dx[0] / s.scale[0];
+     rhobeg = fabs(dx[0] / s.scale[0]);
      rhoend = stop->xtol_rel * (rhobeg);
      for (j = 0; j < n; ++j)
 	  if (rhoend < stop->xtol_abs[j] / s.scale[j])
