Message Boards Message Boards

0
|
4408 Views
|
3 Replies
|
0 Total Likes
View groups...
Share
Share this post:

Nest two "For" loops?

Hi All!

I have generated a "For" loop which results random number array called v1, I need to sum it with the same "For" loop (this time this "For" loop generates another number set). And I need to continue summing this for 2000 set of different random number set .

How can I build the code? does it sound like my "For" loop in another for loop?

\[Delta]t = 0.2; tmax = 5
 imax = tmax/\[Delta]t
f = {1, 0}
T = 273
th = E^-\[Delta]t
nu = {}

For[v = {0, 0}; i = 1, i <= 7, i++,
 If[RandomReal[] < th, v = v + f \[Delta]t, 
  v = RandomVariate[NormalDistribution[0, Sqrt[T]], {2}]];
 v1 = AppendTo[nu, v]]
v1

Thank you very much for your kind help. Thanks, Lakshitha

3 Replies

This is much easier to do if you build up the program functionally. You have a simple function at the core of the problem: it deserves its own name.

newV[v_] := 
 If[RandomReal[] < th, v + f \[Delta]t, 
  RandomVariate[NormalDistribution[0, Sqrt[T]], {2}]]

Note that defining with := means the whole function will be reevaluated each time it is invoked, so you'll get new random numbers. Try it:

newV[{0, 0}]
(* {0.2, 0.} *)

It's easy to repeatedly feed a function's output back into its input in Mathematica. Here, since you want a list of the results, use NestList:

vrun[] := NestList[newV, {0, 0}, 7]

Try that:

vrun[]
(* {{0, 0}, {0.2, 0.}, {0.4, 0.}, {0.6, 0.}, {0.8, 0.}, {1., 0.}, {45.5901, 4.74823}, {45.7901, 4.74823}} *)

To add up a list, use Total. Here's what you get for the test above:

Total[%]
(* {94.3802, 9.49646} *)

You want the totals from 2000 runs. I'll give you ten here:

Table[Total[vrun[]], {10}]
(* {{-12.1303, -196.93}, {5.6, 0.}, {-15.5244, 
  18.5211}, {-32.818, -71.7292}, {-28.6207, 
  10.4847}, {-39.1328, -52.8502}, {20.7677, 28.3427}, {5.6, 
  0.}, {-79.5229, 57.5149}, {-79.6053, -52.024}} *)

Unlike procedural programming languages, Mathematica makes it easy to build up computations from simple functions. Simple function are much easier to construct and test than complicated loops.

POSTED BY: John Doty

Several comments. RandomReal can construct an array of random numbers, so you don't need the For loop, which is not very efficient in Mathematica. Table is preferred. AppendTo is also inefficient. It's better to set up an array ahead of time and put numbers into it.

POSTED BY: Frank Kampas

You can wrap the body of the 'action' part of a For loop in parentheses, but I would suggest that you look at a more idiomatic way to solve the problem.

There are multiple ways to eliminate the need for loops using functional programming. Stephen Wolfram's introduction discusses this, as well as the documentation.

I started out with Mathematica from a background in c and other similar languages. It took a while to make the transition, but it is worth the effort. Cleaner code, easier to maintain, and in most cases faster.

Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract