How to use binomial expansion to produce correct results?
6 次查看(过去 30 天)
显示 更早的评论
Hi, I'm working on numerical problems where I need to calculate the cumulative distribution function values using summation. Furthermore, my code utilizes binomial expansion like the following
I wrote a function to compute CDF values using bionomial expansion
function results = sum_testing(mu,z,n)
for i = 1:n+1
t= nchoosek(n,i-1);
results(i) = t.*(-1).^((n-i+1)).*exp(-u*(z).*(n-i+1))
end
My problem is that, for large n's (n>100), the CDF values are very large (1e+60). How can I modify the above code to produce accurate results (using bionomial expansion)?
3 个评论
采纳的回答
John D'Errico
2023-3-3
编辑:John D'Errico
2023-3-3
How? Using double precision arithmetic? You often can't. You need to remember that double preciion arithmetic lives in 16 significant digits (roughly). So adding and subtracting numbers that differ by more than 16 powers of 10 will yield numerical garbage.
In some cases, you can succeed by the use of logs to compute individual terms, if there would otherwise be an underflow or an overflow, but then multiplying a very large number by a very small one would result in something managable.
You can't just assume a computer can compute anything you throw at it. You might decide that using higher precision is warranted. So you might do those computations using syms and vpa, or perhaps a high precision float tool like my HPF toolbox. But beware that any such solution, while it can succeed, will generally be exceedingly slow. This is the price you must pay. Or, spend some time in learning good numerical methods for solving such problems, which would generally teach you pretty much what I said above.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Numbers and Precision 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!