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

1. Aug 22, 2011

### kaizen.moto

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.

2. Aug 22, 2011

### FunkyDwarf

You are probably not clearing a variable properly. Can you post the whole code for the loop here?

3. Aug 23, 2011

### Staff: Mentor

This is another reason why it is a good idea to use Module and break your code up into small testable chunks.

4. Aug 23, 2011

### kaizen.moto

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.

5. Aug 23, 2011

### Staff: Mentor

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: Aug 23, 2011