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

conditional is giving wrong value

  • To: mathgroup at smc.vnet.net
  • Subject: [mg73709] conditional is giving wrong value
  • From: p at dirac.org (Peter Jay Salzman)
  • Date: Sun, 25 Feb 2007 04:36:21 -0500 (EST)

I'm implementing the method of steepest descent to minimize a function in
Mathematica and have a few nagging problems, the most serious being that a
conditional is giving a wrong value.

Here's my code:


(* Cell one ========================================================= *)

Clear[a, delf, delta, f, iteration, min, s, theA, tolerance, x, xNew ];
<< Calculus`VectorAnalysis`;


(* Function to minimize *)
f[x_,y_] := 1/2 * x^2  +  1/2 * y^2

(* Initial Guess. *)
x  =  { {5.0`20, 1.0`20} }

(* Direction that points "downhill" from current location. *)
s  =  {};

(* The gradient of the function to minimize *)
(*
   This doesn't work:
   delf[x_,y_] := - Grad[f[x,y], Cartesian[x,y,z]];
   delf[3,1]
)*
delf[x_,y_] := { x, 5*y };


(* Cell two ========================================================= *)


iteration = 0;
tolerance = 10^(-30);
delta = 10;


While delta > tolerance,

   Print[{delta, N[tolerance], delta > tolerance}];

   (* Get direction to travel in (downhill) from grad f.  Ugly syntax! *)
   s = Append[s, -delf[Last[x][[1]], Last[x][[2]]]];
   Print["s: ", s];

   (* 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]]];

   (* Minimize f wrt a.  Can I do this without using temp var theA? *)
   {min, theA} = Minimize[f[xNew[[1]], xNew[[2]]], {a}];
   Print["f at minimum of ", min, " when ", theA];

   (* Update x using the direction *)
   xNew = xNew /. theA;
   delta = Norm[Last[x] - xNew];
   Print[delta];
   x = Append[x, xNew /. theA];

   Print["The new x is ", N[xNew], 20];
   iteration += 1;
]

Print["Convergence in ", iteration, " iterations."];
Print["Minimum point at ", Last[x]];
Print["Value of f at min point: ", f[Last[x][[1]], Last[x][[2]]] ];

(* ================================================================== *)



The most serious problem is that this program always terminates at the 23rd
iteration.   At the last iteration, this line:

   Print[{delta, N[tolerance], delta > tolerance}];

prints:

   { 0.00046, 1.0x10^(-30), True }

which indicates that 4.6^-4 > 1.0^-30 is true.  What am I doing wrong??



Other less serious issues that I can live with:

1. I'd like to define the gradient of the trial function without me
explicitly finding the gradient.  For this f, it's nothing, but in
principle, finding the gradient can be very tedious.  This doesn't work:

    delf[x_,y_] := - Grad[f[x,y], Cartesian[x,y,z]];

because when I type delf[3,2], the arguments get passed to Cartesian[] as
{3,2,z}.  How can I get around this?


2. The first time I run this, Mathematica complains that "tolerance" is
similar to "Tolerance" and "min" is similar to "Min.  How can I supress
that?


3. When I run the first cell (the material above "new cell") Mathematica
   prints out "Null^6".  What does this mean and why is it getting printed?


4. Any coding tips I should keep in mind to become a better Mathematica programmer?

Many thanks!
Pete


  • Prev by Date: Re: Integral question 2
  • Next by Date: Re: Integral question
  • Previous by thread: Re: Integral question 2
  • Next by thread: Re: conditional is giving wrong value