Re: Why is 1 smaller than 0?
- To: mathgroup at smc.vnet.net
- Subject: [mg73762] Re: Why is 1 smaller than 0?
- From: Jens-Peer Kuska <kuska at informatik.uni-leipzig.de>
- Date: Wed, 28 Feb 2007 04:24:16 -0500 (EST)
- Organization: Uni Leipzig
- References: <es12vk$nsk$1@smc.vnet.net>
- Reply-to: kuska at informatik.uni-leipzig.de
Hi,
you call Minimize[] and every call loose some digits of
precision, at least one per iteration.
When the precision is sufficently decreased it turn out
that
0.0274 plus minus 0.1 > 0.00001
is True. You see the loss of precision in your
Print[] statements because the printed, trustable
digist become less and less in every iteration.
To restore the precision you have to add a SetPrecision[]
and
While[f @@ Last[x] > .00001,
Print[f @@ Last[x], " ", f @@ Last[x] > 0.00001`50];
(*Get direction to travel in (downhill) from grad f.*)
s = Append[s, -delf @@ Last[x]];
(*a tells us how far to travel. Need to minimize f to find it.*)
xNew = Last[x] + a*delf @@ Last[x];
(*Minimizes f with respect to a.*)
{min, theA} = Minimize[f @@ xNew, {a}];
(*Update x using the direction*)
(* HERE RESTORE THE PRECISION *)
xNew = SetPrecision[xNew /. theA, 50];
delta = Norm[Last[x] - xNew];
x = Append[x, xNew /. theA];
++iteration;
];
work fine.
Regards
Jens
Peter Jay Salzman wrote:
> 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];
>