MathGroup Archive 2005

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

Search the Archive

Re: Do loops in Mathematica

  • To: mathgroup at smc.vnet.net
  • Subject: [mg55187] Re: [mg55170] Do loops in Mathematica
  • From: "David Park" <djmp at earthlink.net>
  • Date: Wed, 16 Mar 2005 05:36:03 -0500 (EST)
  • Sender: owner-wri-mathgroup at wolfram.com

You probably don't want to use a Do loop and your syntax is incorrect for
Mathematica. You probably have to spend a little more time working through
Part I of The Mathematica Book. But that's all right because you have to
plunge in somewhere.

First, let's write a routine that will do the kind of calculation you are
talking about. Here is an example along the lines you specified.

generateAnswer[c_] /; 0 <= c <= 1 :=
  Module[{x, integral1, integral2},
    x = x /. FindRoot[Cos[x] == c, {x, \[Pi]/4}];
    integral1 = NIntegrate[1/(y^2 + x^2), {y, 1, 2}];
    integral2 = NIntegrate[y^2/(y^2 + x^2), {y, 1, 2}];
    integral2 - integral1
    ]

We can test it out by calling individual cases.

generateAnswer[0.5]
0.347105

In fact, in developing the routine I would probably have only the initial
lines first and then add additional steps after testing. Once we have a
working routine for an individual point we can consider generating tables of
values.

Then, instead of using a Do command, use the humble but very useful Table
command.

Table[generateAnswer[c], {c, 0.0, 1.0, 0.1}]
{0.253648, 0.26966, 0.286798, 0.305256, 0.325265, 0.347105, 0.371118,
0.397743, 0.427545, 0.461281, 0.5}

Or we could plot the solution...

Plot[generateAnswer[c], {c, 0, 1},
    Frame -> True,
    PlotRange -> {-0.01, 0.5}];

David Park
djmp at earthlink.net
http://home.earthlink.net/~djmp/




From: dumb_founded [mailto:andreajagger_8 at hotmail.com]
To: mathgroup at smc.vnet.net

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.




  • Prev by Date: Re: Does Mathematica 5 use multiple processors on SPARC?
  • Next by Date: Re: Plotting a super ellipse
  • Previous by thread: Re: Do loops in Mathematica
  • Next by thread: Wolfram plugin for Google Desktop Search