2
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}
Michael E2
  • 235,386
  • 17
  • 334
  • 747
  • Welcome to Mathematica.SE! I suggest the following:
    1. As you receive help, try to give it too, by answering questions in your area of expertise.
    2. Read the [faq]!
    3. When you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge.

    Also, please remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign!

    –  Apr 06 '15 at 04:52
  • are you saying the correct valuate should be 0.02 exactly and M is not doing so? Could you post the source code to reproduce? hard to see the image, too small and need magnifying glasses to see. – Nasser Apr 06 '15 at 05:02
  • Yes it should be .02 exact and M is not doing so. – Daniel Berkowitz Apr 06 '15 at 05:36
  • 3
    Try this. 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. – ciao Apr 06 '15 at 05:52
  • 2
    If you want exact numerics, use exact numbers: 2/1000 instead of 0.002 and avoid N. – István Zachar Apr 06 '15 at 08:15
  • 3
    Note that 0.01 + 0.01 equals 0.02`, while 0.03 - 0.01 equals 019999999999999997`. This is due to numerical round-off error consistent with IEEE 754 binary64 floating-point numbers. Note that 0.02 - (0.03 - 0.01) equals 2^(-58), a one-bit loss of precision. The problem with your code is subtler, though, since 0.019999999999999997` == 0.02` returns True, but as patterns, 0.019999999999999997` does not match 0.02`. – Michael E2 Apr 06 '15 at 10:30
  • To the close-voters: This Q. might be a duplicate, if one can be found, but it is not a simple mistake or an abuse of Which. This issue has appeared before, but I haven't found a duplicate yet. – Michael E2 Apr 06 '15 at 10:33
  • Somewhat related: http://mathematica.stackexchange.com/a/18485 - but it does not discuss the difference between Equal, MatchQ and/or pattern-matching in function definitions. – Michael E2 Apr 06 '15 at 10:40
  • 2
    The methods here (1072) might help you construct a solution to this problem. – Michael E2 Apr 06 '15 at 12:35
  • 2
    @MichaelE2 Perhaps a duplicate of this: The difference between 0. and 0? It has a lot of good answers patternmatching-wise. – István Zachar Apr 07 '15 at 09:54
  • @IstvánZachar The main issue is definitely the same, but the problem of defining yy remains. For instance, using Equal to get around minor round-off error, e.g., With[{x1 = x0, t1 = t0 + 3./10000.}, yy[x_ /; x == x1, t_ /; t == t1] = yy[x0 + 1./100., t0] + yy[x0 - 1./100., t0]] in the Table, is about 100 times slower than the Round method in my answer to 79179 or your suggestion of using exact numbers for arguments. – Michael E2 Apr 07 '15 at 12:47

1 Answers1

6

What you experience is not Mathematica not dealing with 0.02 correctly, but your yy[...] being called with arguments, for which it is not defined.

The following modification will show you the reason:

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, Print["x==", x, " t==", t];"yy[" <> ToString@x <> "," <>
     ToString@t <> "] not defined!"]

The problem is t==0.00002, since the case is not covered by your specification of yy. If you fix this, you should experience no further issues.

Besides:

Regarding numerical calculations in Mathematica, you might find interest in the reference documentation on Precision and Accuracy.


Extension/Working Solution

Defining yz in a way, that allows for function pattern matching, you will be successful:

yz[0 | 1, _] = 0; yz[x_, 0] := Exp[-1000. (x - .3)^2];
yz[x_, .00002] := yz[x + 1/100, 0] + yz[x - 1/100, 0];

And now:

Flatten@Table[yz[x, t], {t, {0, .00002}}, {x, 1/100, 99/100, 1/100}]

calls no undefined functions anymore (just copy the whole next expression into your notebook and execute):

Uncompress@"1:eJxdk3tYTHkYx4fB2FXLFt00XVXDTEZJ03SZU0q6sdkorcImMbFt3rKkMTOyWZVQEom\
onmdH2xJSbbdZhdRuY9imclu5JWl2C0mbae2ZM+d3dp+dP+aP85zz+73v9/P9WMd8/\
XlcC41GS5qE/4UIk5JhisMMhhFEukFP5rJ3PZd/d4PBhuJ9b2Kj+\
FCyLiMtwa2LD0FDTR3+KSHukLZKUPNNT6M7qC/ZMJ6cnesBiwI4f5+\
elusB8min4us3hj0g2KwwMPdYmCco66+\
bcrDznmBmZ9tawKR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K/\
BwOpn1O1c/8ABg4X1Zr098MYiNz4e4trx5BxGgyS0RNr9I4QfcVE5+\
ijk3PRXUXo9hI0jz+\
asB7NzEFbsNFe1KbpaHcmSmNUReZDR4lRGVKpUjk3acjkpYhF8xySjj3iNbyWJFimIZkKE\
OUn10ju35kVKV9pm3DG08DfSuvgtKKa9au0DlbQ1z5iaB3cWFrl6a11cKKRZP8yrYNvQ14\
lSrUOchf1jRAO1oobBwgHg++OfEk4+IuYbT+\
gdbCn72AN4aDLi4l8wkHhIV9nwkEwUXAJB1kFjErCQcP4yFTCwYMPHzwgHJT2dq0nHNy+\
pLCTcND4ZNNuwsGmHal/EA5KEo/\
qHPR1439BOJjg2K5zsLb4B52Duap2nYO5XXt1DuZ9qNA5yI18qnMwspCtc9Bw97jOQYtyp\
s5B5c5DOgenm4fqHBRdJR1siScdbM0mHYzKIx0U5v/PwX8ArqJBfw=="
Jinxed
  • 3,753
  • 10
  • 24
  • I'm still confused. yy[x,t] is the boundary and initial conditions of a wave on a string. 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] evaluates yy at t=0 to find y at t+.00002. Because the value of y[x,.00002] is stored according to my table function I don't see why it's returning this null message. – Daniel Berkowitz Apr 06 '15 at 23:19
  • @DanielBerkowitz: You just don't actually do that in all cases, i.e.: For pattern matching, 0 e.g. does not equal 0.0. I'll extend the answer to provide a working solution. – Jinxed Apr 07 '15 at 06:36