Mathematica: Problem with For loop increments

  • Context: Mathematica 
  • Thread starter Thread starter musicgirl
  • Start date Start date
  • Tags Tags
    Loop Mathematica
Click For Summary
SUMMARY

The discussion addresses a common issue in Mathematica related to the use of floating-point arithmetic within for loops. The user, Rachel, encounters problems when incrementing a variable with decimal values, leading to unexpected results in the output table. The solution involves using exact rationals or rationalizing the variable to ensure proper value matching during function lookups. This approach prevents discrepancies caused by the inherent imprecision of floating-point numbers.

PREREQUISITES
  • Understanding of Mathematica syntax and functions
  • Familiarity with floating-point arithmetic and its limitations
  • Knowledge of exact and inexact numbers in programming
  • Basic experience with loops in programming languages
NEXT STEPS
  • Learn about using exact rationals in Mathematica
  • Explore the concept of floating-point precision and its implications
  • Investigate the Rationalize function in Mathematica
  • Study best practices for numerical computations in programming
USEFUL FOR

Mathematica users, programmers dealing with numerical computations, and anyone interested in understanding floating-point arithmetic issues and solutions.

musicgirl
Messages
12
Reaction score
0
I'm having a recurring problem in mathematica when I try and introduce a simple for loop.
For instance, I have the code shown below. It should increment my var value, and calculate a value of 'initial' for each 'var' value. These should then be recorded under the name RMPvalue[var] so I can combine them into a table.

For[var = 1, var <= 3, var = var + 0.1,

A = 3;
α = 3;
γ = var* 0.05;
v0 = var*0.4;
w0 = var*0.4;
ε = 0.2;

initial =
Solve[wi == A*vi*(vi - α)*(1 - vi) - w0 && wi == (vi - v0)/γ, {vi, wi},
Reals];
RMPvalue[var] = initial[[1, 1, 2]]]

Table[RMPvalue, {i, 1, 3, 0.1}]

If I ran the code with each individual var value, I have no problem. However, once I introduce a for loop (I have also tried a while loop...) some values do not get computed and I get a table output such as:

{0.295541, 0.319624, 0.343382, 0.36693, 0.39038, 0.413848, 0.437453,
RMPvalue[1.7], 0.48562, RMPvalue[1.9], 0.536181, RMPvalue[2.1], 0.591052,
RMPvalue[2.3], 0.653529, 0.689508, 0.730596, RMPvalue[2.7], 0.843924,
RMPvalue[2.9], 1.7899}

where the entries listed as RMPvalue[x] have no value assigned to them. My instinct is that there's a problem in the loop increments but I have no idea how to fix it. I've encountered this problem with several different codes, but I've never found a solution, just reworked the code so I don't get in this situation. Does anyone have any ideas?

Thanks,

Rachel
 
Physics news on Phys.org
Your problem results from using approximate numbers, those containing decimal points. Those might display as the same value but differ by very very tiny amounts. But comparing values, like when you are looking up RMPValue[var], depends on exact values.

Compare the exact values from this
In[1]:= For[var=1,var≤3,var=var+0.1,Print[FullForm[var]]]

From In[1]:= 1
From In[1]:= 1.1`
From In[1]:= 1.2000000000000002`
From In[1]:= 1.3000000000000003`
From In[1]:= 1.4000000000000004`
From In[1]:= 1.5000000000000004`
From In[1]:= 1.6000000000000005`
From In[1]:= 1.7000000000000006`
From In[1]:= 1.8000000000000007`
From In[1]:= 1.9000000000000008`
From In[1]:= 2.000000000000001`
From In[1]:= 2.100000000000001`
From In[1]:= 2.200000000000001`
From In[1]:= 2.300000000000001`
From In[1]:= 2.4000000000000012`
From In[1]:= 2.5000000000000013`
From In[1]:= 2.6000000000000014`
From In[1]:= 2.7000000000000015`
From In[1]:= 2.8000000000000016`
From In[1]:= 2.9000000000000017`
From In[1]:= 3.0000000000000018`

with the exact values from this
In[2]:= Table[FullForm,{i,1,3,0.1}]

Out[2]= {1,1.1`,1.2`,1.3`,1.4`,1.5`,1.6`,1.7`,1.8`, 1.9000000000000001`,2.`,2.1`,2.2`, 2.3000000000000003`, 2.4`,2.5`,2.6`,2.7`,2.8000000000000003`,2.9`,3.`}

When both those match, where the precise definition of "match" is much more complicated and I don't want to dump you into that, then your function lookup works. When they don't match then your function lookup doesn't work.

Potential solutions make use of exact rationals like this

For[var = 1, var ≤ 3, var = var + 1/10,
...
RMPvalue[var] = initial[[1, 1, 2]]
]
Table[RMPvalue, {i, 1, 3, 1/10}]

Or use inexact approximates, mash them to be exact and hope for the best like this

For[var = 1, var ≤ 3, var = var + 0.1,
...
RMPvalue[Rationalize[var]] = initial[[1, 1, 2]]
]
Table[RMPvalue[Rationalize], {i, 1, 3, 0.1}]

Two sins that we pay for every day:
1: Programming languages do not provide exact decimal math by default.
2: Schools to not intensely train programmers to understand the tar pit of floating point math problems.
 
Last edited:
Thank you very much, that was very helpful! That was along the lines of what I was thinking, but didn't know how to fix it. Thanks again.
 

Similar threads

  • · Replies 8 ·
Replies
8
Views
4K
  • · Replies 6 ·
Replies
6
Views
5K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
4K
  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 3 ·
Replies
3
Views
10K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K