Re: Re: Do loops in Mathematica

*To*: mathgroup at smc.vnet.net*Subject*: [mg55233] Re: [mg55174] Re: Do loops in Mathematica*From*: DrBob <drbob at bigfoot.com>*Date*: Thu, 17 Mar 2005 03:29:13 -0500 (EST)*References*: <d15s6o$9l2$1@smc.vnet.net> <200503161035.FAA23736@smc.vnet.net>*Reply-to*: drbob at bigfoot.com*Sender*: owner-wri-mathgroup at wolfram.com

There is no bug, and... This really works? Do[p = Solve[x2 + 3*x + 1 == 0, x]; Return[2*p], {i, 1}] {{2*(x -> (1/3)*(-1 - x2))}} Not the way you meant it to, I'll bet. x2 should be x^2, for instance. Anyway, I recommend limiting use of Do to once a week, and Return to once a year (at most). Do is a bad habit that will tend to keep you from realizing the benefits of Mathematica's functional programming constructs. Return encourages what we used to call "spaghetti code". Bobby On Wed, 16 Mar 2005 05:35:50 -0500 (EST), dh <dh at metrohm.ch> wrote: > Hi, > it looks like a bug to me. However,your Do loop is certainly written > rather strange. The way to do it (and that works!): > > Do[p = Solve[x2 + 3*x + 1 == 0, x]; Return[2*p], {i, 1}] > > Some notes concerning your original Do loop:Do[{p = Solve[x^2 + 3*x + 1 > == 0, x], Return[2*p]}, {i, 1}]: > The List {..} does not make sense. > It does not make sense to multiply a replacement rule by 2: Return[2*p] > > Daniel > > dumb_founded wrote: >> I am having trouble setting up Do loops in Mathematica. Even when I >> put in a return statement, I get no output. Below is a concrete >> example of code I am putting in: >> >> Do[{p = Solve[x^2 + 3*x + 1 == 0, x], Return[2*p]}, {i, 1}] >> >> >> I get nothing out of it. >> >> Here is a sketch of the code I want to construct but am scared to >> because of the above problem I keep encountering. >> >> (1) the first expression in the Do loop calls the FindRoot function on >> nonlinear function of x that involves a constant c. The constant c is >> in curly brackets at the end of the Do loop specifying a beginning >> value for c and an ending value for c. >> (2) I take the root found in the previous expression and I use it to >> calculate two numerical integrals >> (3) I add or subtract the two numerical integrals found in the previous >> expressions and I return the value of this expression (and this >> expression alone). >> >> >> I am having a headache figuring out this simple chore. Mathematica >> simply refuses to behave. >> > > > > -- DrBob at bigfoot.com

**References**:**Re: Do loops in Mathematica***From:*dh <dh@metrohm.ch>