Message Boards Message Boards

0
|
29691 Views
|
13 Replies
|
0 Total Likes
View groups...
Share
Share this post:

For loop with a multiple line body only executes the first line of the body

Posted 10 years ago

I'm really scratching my head on this one. I have a simple For[] loop:

For[rPos = 0, rPos<=(2 N[Pi]), rPos += radiusInc,
    zPotential = 0;
    xPos = rDistance Cos[rPos];
    yPos = rDistance Sin[rPos];
    zPos = planeDepth;
    AppendTo[zField, {xPos,yPos, zPos, zPotential}];     
];

The only line that is executed in the body is

zPotential = 0;

I know that For[] loops are discouraged - but I'm not seeing why this For[] loop only executes the first line in the body.

POSTED BY: Doug Kimzey
13 Replies

Thank again to Bill and all who took a look at this. This one is a bit odd. I have multiple copies of Mathematica on desktops and laptops - so I will try this on different machines.

I am also noticing that some parameters being passed into functions with default values do not show up in the variables debug window as numeric values. If arguments with numeric default values are seen by Mathematica as objects instead of numeric values, comparison operators will probably not work as expected and arithmetic operations may be returning a type of object instead of numeric value.

This begs the question: what type of variable is a function argument where the default value is used?

If I write:

H[ depth_, opFreq_: 1000, relPermittivity_: 78] := Module[...];

and call this with H[1.8]. Are opFreq and relPermittivity numeric types or objects? Is there a way to use strong typing in Mathematica?

In languages such as C++ and C#, the type of object or variable determine how comparison operators (such as ==, <=, >=) are interpreted.

POSTED BY: Doug Kimzey
Posted 10 years ago

Or get rid of all the approximations and do it exactly

For[rPos = 0, MAXRPOS <= 2 Pi, radiusInc += Pi/10,
  xPos = cellRadius Cos[rPos];
  yPos = cellRadius Sin[rPos];
  ];

That will avoid 20*ReallyReallyCloseToPiOverTen being either being slightly less than or slightly greater than ReallyReallyCloseToTwoPi and thus having 19 or 20 or 21 iterations and you can't figure out where the extra one is or the one that is missing went.

Then, if you absolutely can't resist the compulsion, you can stick in an N where you really really need a decimal approximation and that isn't being handled automatically by Mathematica hiding in the backgrount. Most things should be just as happy or even happier with 3*Pi/10 than with 0.942478.

Tracking down exactly what the root problem really really was might teach something and avoid that problem in the future.

A brain cell somewhere is making me think that years and years ago I stumbled across "unexpected behavior" with For. Maybe I'm remembering that the way I stumbled onto that was For[n=0, n<10, n=SomeFunctionOf[n], LoopBody] instead of n++ or n=n+constant. I think it was being more creative in the changing of the variable that didn't work as I expected. I told someone at the time and they reported it. Maybe there is an entry in the version logs that describe that being changed because I can't seem to reproduce that now. Or maybe I just can't remember the details correctly. But I can't get any of your examples to not work for me.

POSTED BY: Bill Simpson

Replacing all instances of N[Pi] with the numeric value of Pi fixed the problem.

rPos = 0; 
       (* Replaced N[Pi] with value of Pi *)
    MAXRPOS = (2*3.14159265358979);  
    radiusInc = 3.14159265358979/10;

       While[rPos<=MAXRPOS,

         xPos = N[cellRadius] Cos[rPos];
         yPos = N[cellRadius] Sin[rPos];

         rPos = rPos + radiusInc
       ];

Thanks once again for all the help!

POSTED BY: Doug Kimzey
Posted 10 years ago

Inquiring minds want to know... what happens with a fresh start of Mathematica, no loading any other notebooks, etc, and exactly this

Print["does it work?"]

is typed into a cell and evaluated once, nothing more, nothing less.

(Trying to eliminate all the seemingly inessential detail)

If that doesn't display anything then I'd go hunting for initialization that might have gotten farbled and perhaps redirected some of your output, but you say that workbench is still telling you things, so that probably means some things haven't been completely broken.

POSTED BY: Bill Simpson

Hi Bill,

Here is the output:

Print["Does it work?"]

Does it work?

I'm wondering if there are any conditions that could disable the execution of a loop. While the basic code above works, the loop below does nothing:

rPos = 0; 
    MAXRPOS = (2 N[Pi]);
    radiusInc = N[Pi]/10;


    While[rPos<=MAXRPOS,
       Print@ToString@rPos;
       rPos = rPos + radiusInc];
POSTED BY: Doug Kimzey

rDistance = 5.4; radiusInc = N[Pi]/10; planeDepth = 0.5; zField = {};

For[rPos = 0, rPos <= (2 N[Pi]), rPos += radiusInc, zPotential = 0; Print@ToString@rPos; xPos = rDistance Cos[rPos]; yPos = rDistance Sin[rPos]; zPos = planeDepth; AppendTo[zField, {xPos, yPos, zPotential}]; <a href="mailto:Print@ToString@Length[zField]]">Print@ToString@Length[zField]];

0

1

0.314159

2

0.628319

3

0.942478

4

1.25664

5

1.5708

6

1.88496

7

2.19911

8

2.51327

9

2.82743

10

3.14159

11

3.45575

12

3.76991

13

4.08407

14

4.39823

15

4.71239

16

5.02655

17

5.34071

18

5.65487

19

5.96903

20

6.28319

21

POSTED BY: Frank Kampas

Hi Frank - Thanks for the snippet! I just wish I could get that to happen on my box! I'm not quite sure what's going on. Basically if I execute the same code and step through in the workbench, it highlights the zPotential=0, jumps back to the top of the For[] and falls out.

Not sure why...

POSTED BY: Doug Kimzey

Thanks all! I am going to completely delete this project and start from scratch. No clue whatsoever. I've never seen anything like it.

POSTED BY: Doug Kimzey

Sorry below is a more complete code snippet:

rDistance = 5.4;
radiusInc = N[Pi]/10;
planeDepth = 0.5;
zField = {}

For[rPos = 0, rPos<=(2 N[Pi]), rPos += radiusInc,
       zPotential = 0;
       Print@ToString@rPos;
       xPos = rDistance Cos[rPos];
       yPos = rDistance Sin[rPos];
       zPos = planeDepth;
       AppendTo[zField, {xPos,yPos, zPotential}];
       Print@ToString@Length[zField] 
    ];

The Print statements show no output. When I step into the loop in the Wolfram Workbench, the only line executed is:

zPotential = 0;

The debugger hits this line twice and falls out. It does not appear that rPos is incremented, although the value of radiusInc is 0.31415...

Also - if I transpose the first two lines in the body of the For[] - so that the Print@ToString@rPos precedes the assignment of zPotential as shown below, the debugger does not hit the Print@ToString@rPos and no output is produced. Oddly it goes to the zPotential=0; line and loops back to the For[]:

For[rPos = 0, rPos<=(2 N[Pi]), rPos += radiusInc,
       Print@ToString@rPos;
       zPotential = 0;
       xPos = rDistance Cos[rPos];
       yPos = rDistance Sin[rPos];
       zPos = planeDepth;
       AppendTo[zField, {xPos,yPos, zPotential}];
       Print@ToString@Length[zField] 
    ];

This one has me completely stumped.

Changing to a While[] loop produces the same result.

rPos = 0; 
MAXRPOS = (2 N[Pi]);
While[rPos<=MAXPOS, 
    Print@ToString@rPos;
    zPotential = 0;
    xPos = rDistance Cos[rPos];
    yPos = rDistance Sin[rPos];
    zPos = planeDepth;
    AppendTo[zField, {xPos,yPos, zPotential}];
    Print@ToString@Length[zField];
    rPos += radiusInc  
];

Once again, the first line in the body that the debugger hits is zPotential=0; This line is hit twice and the loop is exited.

POSTED BY: Doug Kimzey

If radiusInc is greater than 2 Pi, only one step will execute.

POSTED BY: Frank Kampas

This is true but radiusInc is initialized to N[Pi]/10 or 0.31415926...

POSTED BY: Doug Kimzey

Try inserting Print statements in between some of the lines

For[rPos = 0, rPos<=(2 N[Pi]), rPos += radiusInc,
    zPotential = 0;
    Print@ToString@rPos;
    xPos = rDistance Cos[rPos];
    yPos = rDistance Sin[rPos];
    Print@ToString@{xPos,yPos};
    zPos = planeDepth;
    AppendTo[zField, {xPos,yPos, zPos, zPotential}];     
];
POSTED BY: Sean Clarke

Please post self contained complete working/not-working example. You have values in your example that are not defined before the loop starts. The code posted should be in one cell that one can copy and paste and run. Showing a fragment of the code makes it hard to guess what you had before it.

POSTED BY: Nasser M. Abbasi
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