Mathematica Mathematica: why Do-Loop outputs are different from individual cases.

  • Thread starter Thread starter kaizen.moto
  • Start date Start date
  • Tags Tags
    Mathematica
Click For Summary
The discussion centers on a user experiencing discrepancies in outputs from a Do-Loop iteration compared to individual iterations in Mathematica. The user provided two cases: the first case uses a Do-Loop with multiple variable increments, while the second case runs individual iterations for each variable combination, yielding different results. The community suggests that the issue may stem from improper variable clearing within the loop and recommends using the Module construct for better code organization and variable management. Emphasis is placed on the importance of breaking complex code into smaller, manageable functions, especially in a functional programming context like Mathematica. This approach can help avoid repeated code and enhance clarity, ultimately leading to more accurate results.
kaizen.moto
Messages
94
Reaction score
0
Dear all,

I am just wondering why I did not get the right outputs from the Do-Loop iteration. When I run the Do-loop, I got the wrong results as compared to the results I obtained for an individual iteration.

For instance:
Case 1 with Do-Loop run from m = 1,3,5 and n = 1,3,5:

In[1]:= Do[Clear[...];...;Print[def],{m,1,5,2},{n,1,5,2}]
Out[2]:= def(1,1) = 4.153374;def(1,3) = -0.512811; def(1,5) = 0.18633; def(3,1) = -0.528550;def(3,3) = 0.127743; def(3,5) = -0.055111; def(5,1) = 0.174747; def(5,3) = -0.055370; def(5,5) = 0.027538;

Case 2 with Do-Loop run individually from (1,1), (1,3), (1,5),(3,1), (3,3), (3,5), (5,1), (5,3) and (5,5):

In[1]:= Do[Clear[...];...;Print[def],{m,1,1,1},{n,1,1,1}]
Out[1]:= def(1,1) = 4.153374

In[2]:= Do[Clear[...];...;Print[def],{m,1,1,1},{n,3,3,1}]
Out[2]:= def(1,3) = -0.372935

In[3]:= Do[Clear[...];...;Print[def],{m,1,1,1},{n,5,5,1}]
Out[3]:= def(1,5) = 0.116199

In[4]:= Do[Clear[...];...;Print[def],{m,3,3,1},{n,1,1,1}]
Out[4]:= def(3,1) = -0.4518628

In[5]:= Do[Clear[...];...;Print[def],{m,3,3,1},{n,3,3,1}]
Out[5]:= def(3,3) = 0.09195776

In[6]:= Do[Clear[...];...;Print[def],{m,3,3,1},{n,5,5,1}]
Out[6]:= def(3,5) = -0.02938316251

In[7]:= Do[Clear[...];...;Print[def],{m,5,5,1},{n,1,1,1}]
Out[7]:= def(5,1) = 0.19833664459

In[8]:= Do[Clear[...];...;Print[def],{m,5,5,1},{n,3,3,1}]
Out[8]:= def(5,3) = -0.06710907582

In[9]:= Do[Clear[...];...;Print[def],{m,5,5,1},{n,5,5,1}]
Out[9]:= def(5,5) = 0.021204373

The correct results are shown in case2.

Could anyone please let me know why case 1 is not the same as case 2. Please let me know how to fix this case1 so that it would match to the results to case 2.

Thank you for any feedback.
 
Physics news on Phys.org
You are probably not clearing a variable properly. Can you post the whole code for the loop here?
 
This is another reason why it is a good idea to use Module and break your code up into small testable chunks.
 
Thanks for the responses. Actually, the code is quite lengthy and I have no idea how to break it into smaller forms. I am in difficulty to break them since they are related to each other. I am also trying to work with Module, and hope it works. I prefer not to display the code here, as the way I created them not really in a professional way of creating code.
But I wish someone could help me out though to make it better.
 
kaizen.moto said:
Thanks for the responses. Actually, the code is quite lengthy and I have no idea how to break it into smaller forms.
Well, that is essentially the most important skill to learn for any computer programming. Learning the syntax of a specific language is not nearly as important as learning to take a complicated problem and break it up into more manageable parts.

In large measure, the main attribute of a programming language is the type of problem decomposition that is easy to do in that language. Structural programming languages decompose a problem into routines which change the state of the program through a series of specified steps. Object oriented languages decompose a problem into structures which merge the data and the operations that can be performed on the data. Functional languages decompose a problem into input and stateless functions which are performed on the input to generate the output.

Mathematica is primarily a functional language, so you should be thinking in terms of functions and arguments. For instance, I see many times in your code where you do the same thing for different matrices, that should be put into a function which is then called for the different matrices. Any time you find yourself doing the same thing multiple times you should probably use a function. Similarly with the conversation we had earlier about Clear'ing variables and using Module instead.
 
Last edited:

Similar threads

  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 10 ·
Replies
10
Views
4K
  • · Replies 9 ·
Replies
9
Views
9K
  • · Replies 5 ·
Replies
5
Views
5K
  • · Replies 22 ·
Replies
22
Views
4K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 54 ·
2
Replies
54
Views
5K
Replies
2
Views
5K
  • · Replies 4 ·
Replies
4
Views
1K
Replies
1
Views
2K