Mathematica 9 is now available
Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
2007
*January
*February
*March
*April
*May
*June
*July
*August
*September
*October
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 2007

[Date Index] [Thread Index] [Author Index]

Search the Archive

Why is 1 smaller than 0?

  • To: mathgroup at smc.vnet.net
  • Subject: [mg73749] Why is 1 smaller than 0?
  • From: p at dirac.org (Peter Jay Salzman)
  • Date: Tue, 27 Feb 2007 05:49:58 -0500 (EST)

This is an implementation of "steepest descent" to minimize a function.  It
runs for 36 iterations.  On the 36th iteration, it claims:

   0.0274 > .00001  True

which is true.  After the While loop exits, it claims:

   0.03 > .00001  False

which is true, not false, as Mathematica claims.


Why is the While loop exiting prematurely?  How do I write this so that it
runs for as long as f(x,y) is greater than .00001?

Thanks!
Pete






(* Implements Minimization via method of steepest descent. *)
Clear[f, x, s, delf, a , xNew, iteration, delta, tolerance, theA, min];
(* Function to minimize *)
f[x_, y_]     := 100`50*(y - x^2)^2 + (1 - x)^2;
delf[x_, y_]  := {-2*(1 - x) - 400.0`50*x(-x^2 + y), 200.0`50*(-x^2 + y)};


iteration = 0.0`50;
(* Initial guess. *)
x = { {5.0`50,1.0`50} };
(* Points "downhill" from the current position. *)
s = {};

While[ f[Last[x][[1]],Last[x][[2]]] > .00001,

	Print[f[Last[x][[1]],Last[x][[2]]], "  ", f[Last[x][[1]],Last[x][[2]]] > .00001];
	
	(* Get direction to travel in (downhill) from grad f. *)
	s = Append[s, -delf[Last[x][[1]], Last[x][[2]]]];
	
	(* a tells us how far to travel.  Need to minimize f to find it. *)
	xNew = Last[x] + a*delf[Last[x][[1]], Last[x][[2]]];

	(* Minimizes f with respect to a. *)
	{min, theA} = Minimize[f[xNew[[1]], xNew[[2]]], {a}];
	
	(* Update x using the direction *)
	xNew = xNew /. theA;
	delta = Norm[Last[x] - xNew];
	x = Append[x, xNew /. theA];
	++iteration;
];

Print["Convergence in ", iteration, " iterations.\n",
	"Delta: ", delta, ", tolerance: ", N[tolerance], "\n",
	"Minimum point at ", Last[x], "\n",
	"Value of f at min point: ", f[Last[x][[1]], Last[x][[2]]], "\n"];
	Print[f[Last[x][[1]],Last[x][[2]]], "  ", f[Last[x][[1]],Last[x][[2]]] > .00001];


  • Prev by Date: Re: Hold and Equal
  • Next by Date: all the possible minors of a matrix
  • Previous by thread: NMinimize and Constraints
  • Next by thread: Re: Why is 1 smaller than 0?