MathGroup Archive 2008

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

Search the Archive

Re: DO loop

  • To: mathgroup at
  • Subject: [mg91306] Re: DO loop
  • From: Jean-Marc Gulliet <jeanmarc.gulliet at>
  • Date: Thu, 14 Aug 2008 07:01:07 -0400 (EDT)
  • Organization: The Open University, Milton Keynes, UK
  • References: <g7uo2f$5uj$>

maria wrote:

> I am new with mathematica and I have the following problem:
> I want  to repeat a procedure that involves a two main functions for a
> precise number of times. Which would be the format of a do loop in
> mathematica in order to calculate them and write the results of each
> iteration to a matrix?
> In order to be more exact I want to solve the following :
> I have a function........
> Vz[r_]=1/r
> intE = EA + e
> .........................which I solve and the real solutions are the
> intergartion limits at  the intergral that follows
> Solve[Vz[r] == intE, r]
> olok = NIntegrate[f1[r], {r, Ra, Rb}] // N;
> I would like to repeat this procure for several e values and write a
> matrix.
> I future I  would like to include a second function in the do loop
> which is also a function of e.

Speaking about iteration Do and Table have the same syntax and do the 
same thing iterating process, except that Do does not returned or build 
anything by itself, while Table builds and returns a list (or list of 
lists depending on the number of iterators involved). So I would 
definitely go with Table (or with some even more functional constructs, 
but this is another story).

(Note that both examples below are optimistic: they assume that Solve 
will always returned two real solutions. They are just starters for you 
own complete code with error checking and/or exception catching.)

In[1]:= EA = r; Vz[r_] = 1/r; intE = EA + e; f1[r_] := r^2

m = {};
Module[{sols, Ra, Rb},
   sols = Solve[Vz[r] == intE, r];
   Ra = r /. sols[[1]];
   Rb = r /. sols[[2]];
   AppendTo[m, NIntegrate[f1[r], {r, Ra, Rb}]], {e, 3}

Out[4]= {1.49071, 4.71405, 12.0185}

In[5]:= Module[{sols},
   sols = Solve[Vz[r] == intE, r];
   NIntegrate[f1[r], {r, r /. sols[[1]], r /. sols[[2]]}], {e, 3}

Out[5]= {1.49071, 4.71405, 12.0185}

-- Jean-Marc

  • Prev by Date: Re: minimize with complex numbers
  • Next by Date: Re: Re: More Inquiries
  • Previous by thread: DO loop
  • Next by thread: Re: DO loop