Re: Lost Values after For[ ] loop
- To: mathgroup at smc.vnet.net
- Subject: [mg65512] Re: Lost Values after For[ ] loop
- From: dh <dh at metrohm.ch>
- Date: Thu, 6 Apr 2006 06:51:52 -0400 (EDT)
- References: <e1087b$ldg$1@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
Hello, it looks like everybody has to fall into the "real-arithmetic trap" at leat once. Consider the following: For[t1 = 90, t1 < 91, t1 = t1 + 0.24, Print[FullForm[t1]]; 0.24 is not accuratly representable in a binary format. If you keep adding up, it makes it worse. Convince yourself that 0.24 is gives a repeating never ending binary fraction representation e.g. by: RealDigits[24/100, 2, 100] Now, what is there to learn? Well, it is a bad idea to use real numbers as indices. Use integer arithmetic for indices. Or otherwise, use rational arithemtic. If you really need real numbers, you must round or chop or.. them before you use them in place of an integer. good (bug) hunting, Daniel rondeau at uvic.ca wrote: > Here is a problem I find completely baffling. > After several days of frustration, I have pin-pointed the failures of my > program to the the issue: > I run a For[] loop with an iterator I call "t1". > The loop is simply a successive evaluation of an NDSolve[] with subsequent > interpolation of the result. The way I have it right now, this simplified > problem always evaluates the same ODE numerically. > The problem, is that depending on how small or large the step for t1 is > (i.e. how many times I ask the ODE to be evaluated in the loop), I may or > may not be able to recover all solutions for the different executions of > NDSolve. > In the first notebook below, everything is fine. The increment in t1 is > 0.25. I get all the solutions I want, either by asking for a Print[] of > particular values of the interpolated function (e.g. 3.2 in the example > below) for any t1 I choose or by creating a table of values. > However, in the second notebook below, alI I change is change the increment > (from 0.25 to 0.24). Now, I can no longer recover all of my results. If I do > a Print[], I see that the values are there, but when I call the values > directly, I only get the first few of them. For most values of t1, I cannot > get the evaluation of the interpolated function at all and therefore cannot > re-use in later computations. How can it be that the increment results in a > change of behavior and how can it be that I can Print[] the values, but > cannot list them and use them all? > I know that this is not a machine or a memory problem. With a step of 0.25, > I can ask for any number of interations on the NDSolve I want and I can > always recover the solutions. I also get the same results on another > machine. > This is very bizarre to me. Any help would be greatly appreciated. > D.R. > FIRST NOTEBOOK > IN> > For[t1=90,t1<95,t1=t1+0.25, > sol1[t1]=NDSolve[{x'[t]?0.001*x[t],x[0]?1},{x},{t,0,10}]; > xt[t1]=Interpolation[Table[Flatten[{t,x[t]/.sol1[t1]}],{t,0,10}]]; > xt[t1][s];] > IN> > For[t1=90,t1<95,t1=t1+0.25,Print[{t1,xt[t1][3.2]}]]; > OUT> > {90,1.00321} > {90.25,1.00321} > {90.5,1.00321} > {90.75,1.00321} > {91.,1.00321} > {91.25,1.00321} > {91.5,1.00321} > {91.75,1.00321} > {92.,1.00321} > {92.25,1.00321} > {92.5,1.00321} > {92.75,1.00321} > {93.,1.00321} > {93.25,1.00321} > {93.5,1.00321} > IN> > Table[{t1,xt[t1][3.2]},{t1,90,94.999,0.25}]; > OUT> > {{90,1.00321},{90.25,1.00321},{90.5,1.00321},{90.75,1.00321},{91.,1.00321},{91.25,1.00321},{91.5,1.00321},{91.75,1.00321},{92.,1.00321},{92.25,1.00321},{92.5,1.00321},{92.75,1.00321},{93.,1.00321},{93.25,1.00321},{93.5,1.00321},{93.75,1.00321},{94.,1.00321},{94.25,1.00321},{94.5,1.00321},{94.75,1.00321}} > SECOND NOTEBOOK > IN> > For[t1=90,t1<95,t1=t1+0.24, > sol1[t1]=NDSolve[{x'[t]?0.001*x[t],x[0]?1},{x},{t,0,10}]; > xt[t1]=Interpolation[Table[Flatten[{t,x[t]/.sol1[t1]}],{t,0,10}]]; > xt[t1][s];] > IN> > For[t1=90,t1<95,t1=t1+0.24,Print[{t1,xt[t1][3.2]}]];{90,1.00321} > OUT> > {90.24,1.00321} > {90.48,1.00321} > {90.72,1.00321} > {90.96,1.00321} > {91.2,1.00321} > {91.44,1.00321} > {91.68,1.00321} > {91.92,1.00321} > {92.16,1.00321} > {92.4,1.00321} > {92.64,1.00321} > {92.88,1.00321} > {93.12,1.00321} > {93.36,1.00321} > {93.6,1.00321} > {93.84,1.00321} > {94.08,1.00321} > {94.32,1.00321} > {94.56,1.00321} > {94.8,1.00321} > IN> > Table[{t1,xt[t1][3.2]},{t1,90,94.999,0.24}] > OUT> > {{90,1.00321},{90.24,1.00321},{90.48,1.00321},{90.72,1.00321},{90.96,1.00321},{91.2,xt[91.2][3.2]},{91.44,xt[91.44][3.2]},{91.68,xt[91.68][3.2]},{91.92,xt[91.92][3.2]},{92.16,xt[92.16][3.2]},{92.4,xt[92.4][3.2]},{92.64,xt[92.64][3.2]},{92.88,xt[92.88][3.2]},{93.12,xt[93.12][3.2]},{93.36,xt[93.36][3.2]},{93.6,xt[93.6][3.2]},{93.84,xt[93.84][3.2]},{94.08,xt[94.08][3.2]},{94.32,xt[94.32][3.2]},{94.56,xt[94.56][3.2]},{94.8,xt[94.8][3.2]}} >