Message Boards Message Boards

0
|
29913 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
POSTED BY: Bill Simpson
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}]; 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
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
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