MathGroup Archive 2007

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

Search the Archive

Re: conditional is giving wrong value

  • To: mathgroup at smc.vnet.net
  • Subject: [mg73795] Re: conditional is giving wrong value
  • From: Jens-Peer Kuska <kuska at informatik.uni-leipzig.de>
  • Date: Wed, 28 Feb 2007 04:42:03 -0500 (EST)
  • Organization: Uni Leipzig
  • References: <errljn$86m$1@smc.vnet.net>
  • Reply-to: kuska at informatik.uni-leipzig.de

Hi,

and you are sure that you don't mean

While[delta > tolerance,
...
]

and you can't use

myGrad[fun_,var_]:=D[fun,#] & /@ var

to define your own gradient in any dimension you like.

If you don't like the spell and spell1 message than turn it of
with

Off[General::spell]
Off[General::spell1]

and you can name your variables tolerance and min and you
with out a message.

In Mathematica every function return a value, but some guys
like to do procedual programming and to return "No Value"
as a function result the "No Value" has the name Null

Regrads
   Jens

Peter Jay Salzman wrote:
> 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: all the possible minors of a matrix
  • Next by Date: Re: Re: conditional is giving wrong value
  • Previous by thread: Re: Re: conditional is giving wrong value
  • Next by thread: performance of tail-recursion