yy[x_, t_] := Which[x == 0, yy[0, t] = 0, x == 1, yy[1, t] = 0, t == 0,yy[x, 0] = Exp[-1000 (x - .3)^2]];
N[Flatten[Table[yy[x, t + .00002] = yy[x + .01, t] + yy[x - .01, t], {t, 0,
0.00002, 0.00002}, {x, .01, .99, .01}], 1], 5]
As can be seen below, I get several Null
values which shouldn't be there. This is due to Mathematica making .02
into
0.019999999999999997`
. Can anyone tell me how to get Mathematica to stop doing this?
{8.93949*10^-35, 2.18745*10^-32, 4.3823*10^-30, 7.188*10^-28, 9.65289*10^-26, 1.06133*10^-23, 9.55413*10^-22,7.04172*10^-20,4.24931*10^-18,2.0995*10^-16,8.49329*10^-15,2.81328*10^-13,7.63035*10^-12,1.69471*10^-10,3.0825*10^-9,4.59226*10^-8,5.60465*10^-7,5.60527*10^-6,0.0000459573,0.000309099,0.00170696,0.00775012,0.0289853,0.0895316,0.22922,0.488655,0.872217,1.31141,1.67032,1.80967,1.67032,1.31141,0.872217,0.488655,0.22922,0.0895316,0.0289853,0.00775012,0.00170696,0.000309099,0.0000459573,5.60527*10^-6,5.60465*10^-7,4.59226*10^-8,3.0825*10^-9,1.69471*10^-10,7.63035*10^-12,2.81328*10^-13,8.49329*10^-15,2.0995*10^-16,4.24931*10^-18,7.04172*10^-20,9.55413*10^-22,1.06133*10^-23,9.65289*10^-26,7.188*10^-28,4.3823*10^-30,2.18745*10^-32,8.93957*10^-35,2.99114*10^-37,8.19405*10^-40,1.83781*10^-42,3.37478*10^-45,5.07378*10^-48,6.24537*10^-51,6.29399*10^-54,5.1932*10^-57,3.50821*10^-60,1.94034*10^-63,8.78637*10^-67,3.25749*10^-70,9.88775*10^-74,2.45727*10^-77,4.99977*10^-81,8.32891*10^-85,1.13597*10^-88,1.26849*10^-92,1.15971*10^-96,8.68063*10^-101,5.31979*10^-105,2.66919*10^-109,1.09649*10^-113,3.68784*10^-118,1.0155*10^-122,2.28944*10^-127,4.2259*10^-132,6.38632*10^-137,7.90174*10^-142,8.00454*10^-147,6.63882*10^-152,4.50803*10^-157,2.50624*10^-162,1.14078*10^-167,4.25128*10^-173,1.29712*10^-178,3.24027*10^-184,6.6271*10^-190,1.1097*10^-195,1.52136*10^-201,2.18745*10^-32,4.38239*10^-30,
7.188*10^-28 + Null, 9.65333*10^-26, 1.0614*10^-23, 9.55509*10^-22, 2. Null, 4.25026*10^-18,2.1002*10^-16,4.24931*10^-18+Null,2.81538*10^-13,7.63885*10^-12,1.69471*10^-10+Null,3.09013*10^-9,4.6092*10^-8,5.63548*10^-7,5.65119*10^-6,5.60465*10^-7+Null,0.000309099 +Null,0.00175291,0.00805922,0.0306922,0.0972817,0.258206,0.488655 +Null,1.10144,1.80006,2.54254,3.12108,3.34064,3.12108,2.54254,1.80006,1.10144,0.578186,0.22922 +Null,0.00775012 +Null,0.0306922,0.00805922,0.00175291,0.000314704,0.0000465178,5.65119*10^-6,5.63548*10^-7,4.6092*10^-8,3.09013*10^-9,1.69752*10^-10,7.63035*10^-12+Null,2.0995*10^-16+Null,8.49754*10^-15,2.1002*10^-16,4.25026*10^-18,7.04278*10^-20,9.55509*10^-22,1.0614*10^-23,9.65333*10^-26,7.18822*10^-28,4.38239*10^-30,2.18748*10^-32,8.93965*10^-35,2.99116*10^-37,8.19408*10^-40,1.83782*10^-42,3.37479*10^-45,5.07378*10^-48,6.24538*10^-51,6.294*10^-54,5.19321*10^-57,3.50821*10^-60,1.94034*10^-63,8.78637*10^-67+Null,2.45727*10^-77+Null,9.88775*10^-74,2.45727*10^-77,4.99977*10^-81,8.32891*10^-85,1.13597*10^-88,1.26849*10^-92,1.15971*10^-96,8.68063*10^-101,5.31979*10^-105,2.66919*10^-109,1.09649*10^-113,3.68784*10^-118,1.0155*10^-122,2.28944*10^-127,4.2259*10^-132,6.38632*10^-137,7.90174*10^-142,8.00454*10^-147,6.63882*10^-152,4.50803*10^-157,2.50624*10^-162,1.14078*10^-167,4.25128*10^-173,1.29712*10^-178+Null,1.1097*10^-195+Null,6.6271*10^-190,1.1097*10^-195}
yy[x_, t_] := Which[x == 0, yy[0, t] = 0, x == 1, yy[1, t] = 0, t == 0, yy[x, 0] = Exp[-1000 (x - .3)^2], True, "Doh!"];
Then read the friggin documentation, I'm sure you can figure it out from there. $\endgroup$2/1000
instead of0.002
and avoidN
. $\endgroup$0.01 + 0.01
equals0.02`
, while0.03 - 0.01
equals019999999999999997`
. This is due to numerical round-off error consistent with IEEE 754 binary64 floating-point numbers. Note that0.02 - (0.03 - 0.01)
equals2^(-58)
, a one-bit loss of precision. The problem with your code is subtler, though, since0.019999999999999997` == 0.02`
returnsTrue
, but as patterns,0.019999999999999997`
does not match0.02`
. $\endgroup$