MathGroup Archive 2006

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Lost Values after For[ ] loop

  • To: mathgroup at smc.vnet.net
  • Subject: [mg65540] Re: [mg65487] Lost Values after For[ ] loop
  • From: "Tony Harker" <a.harker at ucl.ac.uk>
  • Date: Thu, 6 Apr 2006 06:53:19 -0400 (EDT)
  • Sender: owner-wri-mathgroup at wolfram.com

Let's be clear just what is strange.  You are dealing with floating point
numbers, and they aren't stored exactly. If you wrap a FullForm round the
output in your second notebook you'll see that what's actually stored is,
for example, xt[90.47999999999999] rather than xt[90.48].  The iterator in
the Table command, however, looks for xt[90.48]. So what you are actually
seeing is a subtle difference between what is done in your For loop and what
is done in Table's iterator. If you used the same constructions to generate
and to collect your results, they'd match.

  Tony  

Dr A.H. Harker
Department of Physics and Astronomy
University College London
Gower Street
London
WC1E 6BT



]->-----Original Message-----
]->From: rondeau at uvic.ca [mailto:rondeau at uvic.ca] 
To: mathgroup at smc.vnet.net
]->Subject: [mg65540] [mg65487] Lost Values after For[ ] loop
]->
]->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[9
]->4.8][3.2]}}
]->
]->


  • Prev by Date: Re: Re: simplify a trig expression
  • Next by Date: Re: another problem with Infinite Products
  • Previous by thread: Re: Lost Values after For[ ] loop
  • Next by thread: Complex number after NIntegrate