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

  • Mathematica
  • Thread starter kaizen.moto
  • Start date
  • Tags
    Mathematica
In summary, the individual iterations of the Do-Loop produced different results than when run with the Do-Loop module.
  • #1
kaizen.moto
98
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
  • #2
You are probably not clearing a variable properly. Can you post the whole code for the loop here?
 
  • #3
This is another reason why it is a good idea to use Module and break your code up into small testable chunks.
 
  • #4
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
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:

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

1. Why does a Do-Loop output differ from the individual cases in Mathematica?

A Do-Loop in Mathematica is a programming structure that allows for the repeated execution of a set of instructions. While individual cases may produce the same output as the Do-Loop, the loop allows for the execution of the instructions multiple times, potentially altering the output.

2. How does Mathematica handle variables in a Do-Loop?

In a Do-Loop, Mathematica creates a new instance of the variable for each iteration. This means that the variable will have a different value for each iteration, which can lead to differing outputs compared to individual cases where the variable may have a fixed value.

3. Can I control the number of iterations in a Mathematica Do-Loop?

Yes, you can specify the number of iterations in a Do-Loop by using the "Do[expression, {i, n}]" syntax, where "n" is the desired number of iterations. This will ensure that the loop runs a fixed number of times and produces consistent output.

4. Are there any limitations to using Do-Loops in Mathematica?

While Do-Loops can be a powerful tool for repetitive tasks in Mathematica, it is important to note that they can potentially lead to infinite loops if not properly constructed. It is also important to consider the performance implications of using a Do-Loop compared to other programming structures in Mathematica.

5. Can a Do-Loop in Mathematica be used for parallel computing?

Yes, Do-Loops in Mathematica can be parallelized using the "ParallelDo" function. This allows for the execution of the loop on multiple processors or cores, potentially increasing the speed of the computation. However, it is important to note that not all tasks are suitable for parallelization and it may not always result in a faster execution time.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
8
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
10
Views
4K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
9
Views
9K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
5K
  • Programming and Computer Science
Replies
22
Views
3K
  • Programming and Computer Science
Replies
1
Views
3K
  • Programming and Computer Science
2
Replies
54
Views
4K
  • Linear and Abstract Algebra
Replies
2
Views
4K
  • Precalculus Mathematics Homework Help
Replies
4
Views
1K
  • Set Theory, Logic, Probability, Statistics
Replies
1
Views
2K
Back
Top