0
|
4923 Views
|
4 Replies
|
1 Total Likes
View groups...
Share
GROUPS:

# 0+a small number = 0 ???

Posted 11 years ago
 Hi, I'll insert a code which produces wrong results. It is a recursion to calculate probabilities p[i] from the data which I put in the Matrix B: B := Array[a, {8, 8}] J := Dimensions[B][[1]] For[k = 1, k < J + 1, k++, For[i = 1, i < J + 1, i++, a[i, k] = 0]] For[k = 3, k < J + 1, k++, a[1, k] = (k - 2)*0.1; a[2, k] = "--"; a[k, 2] = "|"; a[k, 1] = (k - 2)] a[1, 2] := "proba"; a[2, 1] := "claim"; a[1, 1] := "--"; a[2, 2] := "amount"; a[4, 4] = 1000; a[6, 4] = 1000; a[8, 4] = 1000; B // MatrixForm p[0] = Product[a[1, k]^Sum [a[l, k], {l, 3, J}], {k, 3, J}] t[0, i, j] := 0 t[k, i, j_] := t[k, i, j] = (1 - a[1, j + 2])/ a[1, j + 2]*(If[k - i < 0, 0, p[k - i]] - If[k - i < 0, 0, t[k - i, i, j]]) p[k_] := p[k] = Sum[Sum[1/kia[i + 2, j + 2]*t[k, i, j], {j, 1, J - 2}], {i, 1, Min[J - 2, k]}] If you calculate the values of p[i] (for i=2 or 4 for example), it yields the value 0 even though it's not 0. In fact, this algorithm adds 0 to a very small number. This yields 0 which can't be correct.... Do you know how to avoid this error?? Maybe with the commands WorkingPrecision or AccuracyGoal? I wasn't successfull in trying out these commands. The NSum command instead of the Sum furnishs better results but with an error message. What shall I do? Thank you!
4 Replies
Sort By:
Posted 11 years ago
 Try using 1/10 (or, say SetPrecision[0.1, 100]) instead of 0.1 to avoid doing the entire computation at machine precision. Also the definition of t should probably use t[k_, i_, j_] instead of t[k, i, j_]. Then it returns positive numbers In[14]:= N[{p[2], p[4]}, 20] Out[14]= {4.9209276886444687077*10^-2094, 9.8369344496002929467*10^-2091} 
Posted 11 years ago
 Where did I use decimal Points in my code? As far as I see: nowhere. Typing in p[2], p[4], ... and so on yields 0 even though it shouldn't. It should rather calculate something larger than 0.
Posted 11 years ago
 "Where did I use decimal Points in my code? As far as I see: nowhere.""a[1, k] = (k - 2)*0.1;" <--- There.Any time anything with a decimal point is combined with anything else the result will then have a decimal point. (Well almost any)
Posted 11 years ago
 Use integers or fractions, but nothing with decimal points, to avoid this problem.There are other, but much higher, limits on the ability to calculate exact values without decimal points, but you may never encounter those in your work.