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

  • Context: Mathematica 
  • Thread starter Thread starter kaizen.moto
  • Start date Start date
  • Tags Tags
    Mathematica
Click For Summary

Discussion Overview

The discussion revolves around discrepancies in output results from a Do-Loop iteration in Mathematica compared to individual iterations. Participants explore the reasons behind these differences and seek solutions to align the outputs from the Do-Loop with those from separate cases.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Conceptual clarification

Main Points Raised

  • One participant notes that the outputs from the Do-Loop do not match the results obtained from individual iterations, providing specific examples of the discrepancies.
  • Another participant suggests that the issue may stem from not clearing a variable properly and requests the full code for further analysis.
  • A different participant recommends using Module to break the code into smaller, testable chunks, implying that this could help identify the source of the problem.
  • One participant expresses difficulty in breaking the lengthy code into smaller parts due to interdependencies and mentions an intention to work with Module.
  • Another participant emphasizes the importance of problem decomposition in programming, discussing different programming paradigms and suggesting that the participant should consider using functions to handle repetitive tasks in their code.

Areas of Agreement / Disagreement

Participants do not reach a consensus on the exact cause of the discrepancies in outputs. Multiple viewpoints are presented regarding code structure and variable management, indicating an ongoing debate about best practices in programming with Mathematica.

Contextual Notes

Participants mention issues related to variable clearing and code organization, but specific assumptions or limitations in the provided code are not fully explored.

Who May Find This Useful

Individuals interested in programming with Mathematica, particularly those dealing with iterative processes and code optimization, may find this discussion relevant.

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
4K
  • · 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 ·
Replies
2
Views
6K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K