Mathematica: Problem with For loop increments

In summary, your problem results from approximating numbers with decimal points and the resulting discrepancy between exact and approximate values. You could try using inexact approximates or rationalizing the numbers to be exact, but it is possible that the solution you find is not perfect.
  • #1
musicgirl
12
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
  • #2
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:
  • #3
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.
 

1. What is a For loop in Mathematica?

A For loop is a programming construct in Mathematica that allows you to execute a series of commands repeatedly for a specific number of times. It is commonly used for tasks that require repetitive calculations or operations.

2. How do I specify the starting and ending values for a For loop in Mathematica?

In Mathematica, you can use the syntax "For [initialization, test, increment, body]" to specify the starting and ending values for a For loop. The "initialization" part sets the initial value of the loop variable, "test" defines the condition for the loop to continue, "increment" updates the loop variable after each iteration, and "body" contains the commands to be executed within the loop.

3. What is the default increment value for a For loop in Mathematica?

The default increment value for a For loop in Mathematica is 1. However, you can specify a different increment value by using the syntax "For [initialization, test, increment, body]" and replacing "increment" with the desired value.

4. How can I avoid problems with For loop increments in Mathematica?

To avoid problems with For loop increments in Mathematica, make sure to specify the correct starting and ending values, and choose an appropriate increment that will lead to the desired number of iterations. Additionally, it is important to carefully consider the logic in the "test" condition to ensure that the loop terminates correctly.

5. Can I use different variable names for the loop variable and the increment in a For loop in Mathematica?

Yes, you can use different variable names for the loop variable and the increment in a For loop in Mathematica. For example, you can write "For [i = 1, i < 10, j++, Print[i*j]]" to use "i" as the loop variable and "j" as the increment.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
9K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
2K
  • Classical Physics
Replies
8
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
9
Views
3K
Back
Top