tags:

views:

96

answers:

1

I've the following simple code in mathematica that is what I want to a single output. But I want to get hundreds or thousands of it. How can I do?

Clear["Global`*"]


k = 2; Put["phiout"]; Put["omegadiffout"];

Random[NormalDistribution[0, 0.1]];

For[i = 1, i < 31,
 rnd[i] = Random[NormalDistribution[0, 0.1]]; i++]

Table[rnd[i], {i, 1, 30}]

For[i = 1, i < 30,
 rnddf[i] = rnd[i + 1] - rnd[i]; i++
 ]

diffomega = Table [rnddf[i], {i, 1, 29}];


Table[
  Table [rnddf[i], {i, 1, 29}], {j, 1, 100}];

PutAppend[Table[
  diffomega, {j, 1, 100}] , "diffomega"]


eqs0 = Table  [
   k*phi[i + 1] + k*phi[i - 1] - 2*k*phi[i] - rnddf[i] == 0, {i, 1, 
    28}];

eqs1 = eqs0 /. {phi[0] -> phi[30], phi[31] -> phi[1]};

Sum[phi[i], {i, 1, 29}];

eqs2 = Append[eqs1, - phi[1] - phi[27] - 3 phi[29] == 0];

eqs3 = eqs2 /. {phi[30] -> -Sum[phi[i], {i, 1, 29}]};

vars = Table  [phi[i], {i, 1, 29}];

eqs = NSolve[eqs3, vars];

PutAppend[diffomega, eqs , "phiout"]

This put on file "phiout" and "omegadiffout" only the last value. I need hundreds of them. For each random generations I need an output.

Thanks in advance

+5  A: 

The first thing you need to do, @Paulo, is tidy up your Mathematica so that you, and we, can see the wood for the trees. For example, your 8th statement:

Table[
  Table [rnddf[i], {i, 1, 29}], {j, 1, 100}];

makes a large table, but the table isn't assigned to a variable or used in any other way. There seem to be other statements whose results aren't used either.

Next you should abandon your For loops and use the Mathematica idioms -- they are clearer for we who use Mathematica regularly to understand, easier for you to write, and probably more efficient too. Your statements

For[i = 1, i < 31,
 rnd[i] = Random[NormalDistribution[0, 0.1]]; i++]

Table[rnd[i], {i, 1, 30}]

For[i = 1, i < 30,
 rnddf[i] = rnd[i + 1] - rnd[i]; i++
 ]

diffomega = Table [rnddf[i], {i, 1, 29}];

can, if I understand correctly, be replaced by:

diffomega = Differences[RandomReal[NormalDistribution[0,0.1],{30}]];

Always remember that if you are writing loops in Mathematica you are probably making a mistake. The next change you should make is to stop using Put and PutAppend until you can build, in memory, at least a small example of the entire output that you want to write. Then write that to a file in one go with Save or Export or one of the other high-level I/O functions.

When you have done that, edit your code and explain what you are trying to do and I, and other SOers, will try to help further. Unfortunately, right now, your code is so un-Mathematica-al that I'm having trouble figuring it out.

High Performance Mark
You don't even need the `Table` when you want to draw a whole lot of reals from a random distribution; just use the second argument of `RandomReal`.
Pillsy
@Pillsy: thanks, I edited my post.
High Performance Mark