Message Boards Message Boards

6 Replies
1 Total Likes
View groups...
Share this post:

Can others run the below code in Mathematica? Shift-Enter does nothing.

Posted 10 years ago
How Do I run the below code in Mathematica?
 I am new to Mathematica.  I am running Mathematica 9 on Windows.  I am having difficulty getting the below code to run.  When I pass the below into a new notebook and press shift-enter, nothing happens.  What are the correct steps to follow to run the below code from the below website.  Thank you in advance.   I saw it posted in this link:

 R[n_] := (SeedRandom[n]; RandomReal[])
     80 - m
     + .2*Sin[2 Pi*R[6*m]
             + Sum[4*Sin[2 Pi*R[4*m] + t + R[2 n*m]*2 Pi]*
                  Exp[-(.3*x + 30 - 1*100*R[2 n*m])^2/20],
              {n, 1, 30, 1}]]
    + Sum[3(1 + R[3*n*m])*Abs[Sin[t + R[n*m]*2 Pi]]*
          Exp[-(x - 1*100*R[n*m])^2/20],
      {n, 1, 4, 1}],
  {x, -50, 150},
   PlotStyle -> Directive[White, Thick],
   PlotRange -> {{-50, 150}, {0, 85}},
   Background -> Black, Filling -> Axis, FillingStyle -> Black, Axes -> False,
   AspectRatio -> Full, ImageSize -> {500, 630}],
 {m, 1, 80, 1}]],
{t, 0, 6.3*18/19, 6.3/19}],
AnimationRunning -> False]
POSTED BY: Burris Plac
6 Replies
Posted 10 years ago
Thank you George:  That fixed it.  I have another one that is not parsing.  Any ideas on the below:

 img =
     Sin[2 x - 20 Log[2 (Sin[y]^2 + 1), 2]],
  {x, 0, 16 Pi}, {y, 0, 32 Pi},
  PlotPoints -> 250, ColorFunction -> "SunsetColors",
  Frame -> False, ImageSize -> 600]
 LogPolar[x_, y_] := {Log[Sqrt[x^2 + y^2]], ArcTan[x, y]}
 d = ImageDimensions[img][/img]






    {1, 14*d/16}, {1 + (2 - 2 t)*d/32, (32 - 2 t)*d/32}],

    LogPolar[#[[1]], #[[2]]] &, DataRange -> {{-Pi, Pi}, {-Pi, Pi}}],


  {t, 0, 6/7, 1/7}]
POSTED BY: Burris Plac
I got the code to work. Takes about 5100 seconds.
 R3[n_] := (SeedRandom[n]; RandomReal[])
                     80-m+.2*Sin[2 Pi*R3[6*m]+
                         Sum[4*Sin[2 Pi*R3[4*m]+t+R3[2 n*m]*2 Pi]*
                             Exp[-(.3*x+30-1*100*R3[2 n*m])^2/20],{n,1,30,1}]
                        Sum[3(1+R3[3*n*m])*Abs[Sin[t+R3[n*m]*2 Pi]]*Exp[-(x-1*100*R3[n*m])^2/20],{n,1,4,1}
AnimationRunning->False] //Timing

If you are interested in exporting the graphics into a GIF (for example), you probably only want to go as far as the outer Table[], and not use ListAnimate. I just made a screen capture of the animation -- not as elegant, but quicker.

So, the code is ok, and there was just a minor problem with the code format as it was pasted originally. I'm not going to figure out exactly where. Most of the time, the Wolfram Language parser can make sense of just about anything, but the code for this has sums inside of sines, etc. and it can get confused. When I write code, I try to use indenting and keep track of where all the brackets go, and never ever but a carriage return in front of a multiplication -- implied or otherwise.

Pretty neat graphic.

I checked the timing to draw one curve. On my computer (2013 rMBPro), it took about 3 seconds. Since there are 80 curves per image and 19 images, the entire image will take about 80 minutes. The code as I copied it still does not work, and I suspect that it is an issue with linebreaks. I had to set the format to Code and redo the linebreaks to draw the single line.

Using the Code Stype (from the Format/Style menu) takes an Input formatted cell and removes all the automatic linebreaks. I then add explicit line breaks to keep the length of the line manageable.

I did this first to make one line, (3.08 seconds), then one image (267.6 seconds). Since the next step -- wrapping ListAnimate[Table[*** code for one image]] around what I did, I am sure that it will work.

Here is the code that works to draw one image. Note that I have renamed the R[] function and I left in place the //Timing command. Just copying nad pasting this as Input text has altered the formatting, so check it out.
 R3[n_] := (SeedRandom[n]; RandomReal[])
    Plot[80 - m + .2*Sin[2 Pi*R3[6*m]
         + Sum[4*Sin[2 Pi*R3[4*m] + t +
             R3[2 n*m]*2 Pi]*
           Exp[-(.3*x + 30 - 1*100*R3[2 n*m])^2/20], {n, 1, 30, 1}]] +
      Sum[3 (1 + R3[3*n*m])*Abs[Sin[t + R3[n*m]*2 Pi]]*
       Exp[-(x - 1*100*R3[n*m])^2/20],
      {n, 1, 4, 1}],
    {x, -50, 150}, PlotStyle -> Directive[White, Thick],
    PlotRange -> {{-50, 150}, {0, 85}},
    Background -> Black,
    Filling -> Axis,
    FillingStyle -> Black,
    Axes -> False,
    AspectRatio -> Full,
    ImageSize -> {500, 630}],
   {m, 1, 80, 1}]
  ] // Timing

I need to understand the problem better to understand why SeedRandom[] is used the way it is to draw the curves. Just using RandomReal[] makes a different plot.

I'm going to take a look at this later today.  The issue seems to be the large number of RandonSeed calls, but that is not the only thing.

If you want to work on your own, I plan to look at the code that draws one line -- basically the Plot[] statement and try to optimise that, or at least try to understand what it does. There are 80 of these lines per image, and 19 images in the animation, so that's a lot of use for the plot.

I see that the code in your post has been fixed.

I'll let you know what I find. 

Posted 10 years ago
George:  Thank you for the information.  The image that I was trying to create is below.  The code is from the below link.  Any suggestions on how to correct the code would be appreciated.
POSTED BY: Burris Plac
Ctrl-Y does nothing in Mathematica.

Once you get the code typed in, Shift-enter works -- see the documentation. On my Mac, I can use Shift-Return -- to the right of the " key --  or the Enter key on the numeric keypad. Windows may be different.

The code will execute on my system. It takes about a minute, and what I get is a totally black.

The function R[] does not use its argument, but the ListAnimate depends on the argument. Checking the original code, I see that 

R[n_] := (SeedRandom; RandomReal[])

I copied and pasted the original code. It takes a very long time to execute. Using SeedRandom[] in this way is not efficient. In addition, there are other problems with the code that indicate that this is a translation from another language.

I'd try copying (cut and paste) the code and see what happens. I got tired of waiting for the execution to stop (> 5 minutes). That, and using the right keys to execute the code -- you can always use the Evaluation Menu item -- should get you started.

It may be a bit advanced for your first Mathematica project, but you could go through the code to see what it is tryng to do and rewrite it so that it is more efficient.

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

Group Abstract Group Abstract