Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Mathematica: Problem with For loop increments

  1. Jul 5, 2012 #1
    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},
    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?


  2. jcsd
  3. Jul 5, 2012 #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: Jul 5, 2012
  4. Jul 8, 2012 #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.
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook