Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
2005
*January
*February
*March
*April
*May
*June
*July
*August
*September
*October
*November
*December
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 2005

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

Search the Archive

Re: Do loops in Mathematica

  • To: mathgroup at smc.vnet.net
  • Subject: [mg55200] Re: Do loops in Mathematica
  • From: Bill Rowe <readnewsciv at earthlink.net>
  • Date: Wed, 16 Mar 2005 05:36:40 -0500 (EST)
  • Sender: owner-wri-mathgroup at wolfram.com

On 3/15/05 at 12:22 AM, andreajagger_8 at hotmail.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}]

It is hard to see how this code could be useful. Nor does ask Mathematica to do something reasonable.

Ignoring the fact the equation supplied to Solve never changes in the Do loop (I assume this is just an example), the value of p gets over written on every iteration of the loop. So, it can only retain the last output of Solve. And Solve returns a list of rules. That is 2*p makes no sense, since p is a list of rules.

>I get nothing out of it.

Try

Do[p = Solve[x^2 + 3*x + 1 == 0, x], {i, 1}];
p

Note this still isn't very useful as it only retains the last output of Solve.

More useful would be

Table[Solve[x^2 + 3*x + n == 0, x], {n, 3}]

>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.

Easily done with Table. For example,

Table[f[c],{c,start,end}] were f is function that does your FindRoot step. But note this would go from start to end with a stepsizes of 1. So, it might be better to specifiy the stepsize using {c,start,end, stepsize} as the last arguement to Table.

>(2) I take the root found in the previous expression and I use it to
>calculate two numerical integrals 

This can be done as follows:

{g[x/.First@#],h[x/.First@#]}&/@roots

Here, roots would be the results of from the previous step. g and h would be functions to compute the two 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).


Letting ints be the output from step (2), adding the two integrals could be done as

Plus@@@ints

and subtracting the two integrals could be done as

Subtract@@@ints
--
To reply via email subtract one hundred and four


  • Prev by Date: Re: Surface Normal
  • Next by Date: Re: Plotting a super ellipse
  • Previous by thread: Re: Do loops in Mathematica
  • Next by thread: Re: Do loops in Mathematica