Re: Do loops in Mathematica
- To: mathgroup at smc.vnet.net
- Subject: [mg55181] Re: Do loops in Mathematica
- From: Torsten Coym <torsten.coym at eas.iis.fraunhofer.de>
- Date: Wed, 16 Mar 2005 05:35:56 -0500 (EST)
- References: <d15s6o$9l2$1@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
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. > dumb_founded, first of all: your Do-loop does not make sense (since it evaluates p exactly once), but you are probably aware of that already. Second, I suggest reading Mathematica's online help about Do[] and Solve[]. Do[] returns Null if not otherwise specified. That does not mean, that your assignment p=Solve[...] has not been evaluated. However, Solve[] returns a list of rules, so you probably might want to do the following p = Solve[x^2 + 3*x + 1 == 0, x] 2*x/.p Unfortunately, I could not figure out, what exactly the problem is, you want to solve. Try to describe it more precisely (specify the equations) and I'm sure that someone will help. In the meantime you should try to get more involved in Mathematicas functional and rule based programming approaches. I'm sure, your problem will reduce to a one-liner (that is encountering Do[] will not be necessary at all). Torsten