Matlab is giving me a zero answer when there is actually a real non zero answer !!!!

2 views (last 30 days)
So i noticed my long program give zero when i ever reach iteration number 78 , when i checked what is zeroing my numbers, it was this function
Y = nchoosek(78,4)*((( gamma(3+4)*gamma(60+78-3-4)*gamma(60))/(gamma(3)*gamma(60-3)*gamma(60+78))))
it seems Matlab is calculating the upper first ans assigning it inf and then doing the same for the lower part. then dividing and estimating which inf will win.
How can i stop this !!! This function should return a probability, iteration 77 return a value of 0.124 something !!!
What to do ?
  2 Comments
Adam
Adam on 31 Oct 2017
Edited: Adam on 31 Oct 2017
When I run that the upper part is not Inf (just a very large number of the order of 1e302), but the lower part is (I forget which is which in numerator and denominator so upper and lower will do me!) so something divided by infinity is considered to be 0.
I guess you need to get some better bounds on your calculations if you want a more meaningful answer as any maths involving Inf is going to be nonsense unless you are happy with Infinity as being valid.
ibrahim alturki
ibrahim alturki on 31 Oct 2017
I just realized all the inside of my gamma functions are integers , i could simplify them through factorials and then products

Sign in to comment.

Accepted Answer

Steven Lord
Steven Lord on 31 Oct 2017
Look at the values with which you're working.
gamma([7; 131; 60; 3; 57; 138])
While all of those are finite, and two of them are quite small, four of them are huge. The estimated number of atoms in the observable universe is 1e80. Two of your values are relatively close to that, and two of them are much, MUCH larger.
So now you're multiplying these very large numbers together. This causes both the numerator and denominator of your fraction to overflow to Inf.
Since you're calling gamma with integer values, you could use the factorial function instead. Based on the definition of the factorial function, you can compute factorial(n)/factorial(m) without computing either factorial(n) or factorial(m). That may help you avoid generating such huge numbers and encountering overflow.
  2 Comments
ibrahim alturki
ibrahim alturki on 1 Nov 2017
Thanks, quick question .. will matlab handle factorials like human do by eliminating first the redundant parts from the denomenator and nenomenator ? Or it will just compute each factorial by it self ?
Walter Roberson
Walter Roberson on 1 Nov 2017
When you use gamma() or factorial(), MATLAB will calculate each immediately, not cancelling numerator and denominator.

Sign in to comment.

More Answers (1)

Walter Roberson
Walter Roberson on 31 Oct 2017
n1 = sym(78);
n2 = sym(60);
d1 = sym(4);
d2 = sym(3);
Y = nchoosek(n1, d1)*((( gamma(3+4)*gamma(n1+n2-d2-d1)*gamma(n2))/(gamma(d2)*gamma(n2-d2)*gamma(n1+n2))));
  2 Comments
ibrahim alturki
ibrahim alturki on 1 Nov 2017
Why do we use sym ?
What i did now is changed the gammas with factorials then reduced them into functions of a product from a to b. The program work fine now but really slow , i had to come with a creative idea to speed ot up .
Walter Roberson
Walter Roberson on 1 Nov 2017
In the above expression, using sym(), the gamma() or factorial() are fully expanded to exact integers for arguments up to 999, and exact multiplication and division are used, leading to an exact rational solution rather than a numeric approximation.

Sign in to comment.

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!