Re: Nestwhile
- To: mathgroup at smc.vnet.net
- Subject: [mg57228] Re: [mg57201] Nestwhile
- From: DrBob <drbob at bigfoot.com>
- Date: Sat, 21 May 2005 02:39:58 -0400 (EDT)
- References: <200505200844.EAA00668@smc.vnet.net>
- Reply-to: drbob at bigfoot.com
- Sender: owner-wri-mathgroup at wolfram.com
First of all, d[e_,dw_] should be defined (like that) with patterns on the left. Secondly, d is used with three arguments but not defined in that case. Third, NestWhile's first and third arguments should be functions, for instance {e += 0.01, dw += 0.1} &, not just a loop body. Fourth, NestWhile stops after one iteration because the test fails immediately -- e==1 isn't true, and the other test fails even before that. Maybe you meant NestUntil -- but there is no such thing in Mathematica. Fifth, w is undefined. Sixth, never, ever use Return. If it's the last thing in a Module (the only time it doesn't promote spaghetti-code), it's a waste of keystrokes, since the last expression is returned anyway. Possibly you meant something more like this: Module[{e = 0.05, dw = 0.04}, NestWhile[(e += 0.01; dw += 0.1) &, anythingAtAllBecauseItIsNotUsed, ! (10^-2 < d[e, dw] < 0 || e == 1) &]; Print@{dw,e}; d[e, dw] ] Seventh, there's no reason to group statements with List, when semicolons do just as well. Eighth, you're not actually nesting anything, so While is far more appropriate: Module[{e = 0.05, dw = 0.04}, While[! (10^-2 < d[e, dw] < 0 || e == 1), e += 0.01; dw += 0.1]; Print@{dw, e}; d[e, dw]] Finally, there's nothing in your code to select a minimum. Try Minimize or NMinimize. Or something like this: Module[{e = 0.05, dw = 0.04, best}, best = {Infinity, e, dw}; While[! (10^-2 < d[e, dw] < 0 || e == 1), If[d[e, dw] < First@best, best = { d[e, dw], e, dw}]; e += 0.01; dw += 0.1]; best] or Module[{e = 0.05, dw = 0.04, best, now}, best = {Infinity, e, dw}; While[now = d[e, dw]; ! (10^-2 < now < 0 || e == 1), If[now < First@best, best = {now, e, dw}]; e += 0.01; dw += 0.1]; best] That doesn't work without a value for w, of course, and it varies e and dw in lock-step, rather than independently (like your code). Bobby On Fri, 20 May 2005 04:44:10 -0400 (EDT), Pratik Desai <pdesai1 at umbc.edu> wrote: > Hi, > I am having problem with using NestWhile in a code which basically boils > down to finding values for the variables (e,dw) for which a function > d[e, dw] is "minimized" while one variable (e) goes up to 1. I have been > using the following approach, which unfortunately loops for 1 > iteration..There is definitely a better probably a simpler way to do > this...Thanks in advance for your help > > Here is the attempt : > > d[ e, dw] = > Abs[0. + 1143.8189785870038/((-2.0224920432639264 + > e)*(22.50249204326393 + e)) + > (e^3*(-2.3561944901923435 - > 1.3322676295501878*^-15*w))/((-2.0224920432639264 + > e)*(22.50249204326393 + e)) - > 56.548667764616276*w - (2573.5927018207585*dw)/((-2.0224920432639264 > + e)*(22.50249204326393 + e)) + > (e^2*(-73.38760438785755 + > 56.54866776461624*dw))/((-2.0224920432639264 + e)*(22.50249204326393 + > e)) + > (e*(-407.4855111216201 + > 1158.1167158193412*dw))/((-2.0224920432639264 + e)*(22.50249204326393 + > e))] + > 8.*Abs[0. + 0./((-2.0224920432639264 + e)*(22.50249204326393 + e)) + > (0.*dw)/((-2.0224920432639264 + e)*(22.50249204326393 + e)) + > e^2*(0./((-2.0224920432639264 + e)*(22.50249204326393 + e)) + > (0.*dw)/((-2.0224920432639264 + e)* > (22.50249204326393 + e))) + e*(0. + 0./((-2.0224920432639264 + > e)*(22.50249204326393 + e)) + > (0.*dw)/((-2.0224920432639264 + e)*(22.50249204326393 + e)))] + > Abs[0. + (e*(-107.23302924253159 - > 7.105427357601*^-14*dw))/((-2.0224920432639264 + e)*(22.50249204326393 + > e)) + > (e^3*(2.3561944901923453 - > 4.440892098500626*^-16*dw))/((-2.0224920432639264 + > e)*(22.50249204326393 + e)) + > e^2*(48.25486315913923/((-2.0224920432639264 + e)*(22.50249204326393 > + e)) + > (0.*dw)/((-2.0224920432639264 + e)*(22.50249204326393 + e)))] > > Module[{e = 0.05, dw = 0.04}, NestWhile[{e += 0.01,dw += 0.1}, > d[ e, dw],10^-2 <d[ e, dw] <0&&e==1 ]; Print[dw,d[ e, dw],e]; > Return[d[j, e, dw], Module]] > > 0.14 > 25.415 > -- DrBob at bigfoot.com
- References:
- Nestwhile
- From: Pratik Desai <pdesai1@umbc.edu>
- Nestwhile