필터 지우기
필터 지우기

Error in implemented function

조회 수: 1 (최근 30일)
Nik joung
Nik joung 2017년 11월 14일
댓글: Nik joung 2017년 11월 15일
Hello,
I have the function written above. It was calculated by WolframAlpha and the result is 0.
I've tried to implement the formula in Matlab with the code below:
tmp=0;
for s=0:24
if (48-s >= 24)
bin1 = nchoosek(48-s,24);
else
bin1 = 0;
end
if (47-s >= 24)
bin2 = nchoosek(47-s,24);
else
bin2 = 0;
end
tmp = tmp + (-1)^s * nchoosek(24,s) * (bin1-bin2);
end
But my code outputs (variable tmp) the result -2 instead of 0. I am not seeing, where the mistake is.
I would appreciate, if someone could help me.

채택된 답변

Michal
Michal 2017년 11월 14일
편집: Michal 2017년 11월 14일
The double precision (default numerical values class in Matlab) is not enough for this kind of computation!!! You need to use symbolic math toolbox, to get multi-precision accuracy. Wolfram alpha (with Mathematica engine) using this approach by default.
Just a simple code using matlab symbolic toolbox: tmp = 0 !!!
tmp=sym(0);
syms s
for s=0:24
bin1 = nchoosek(sym(48)-s,sym(24));
bin2 = nchoosek(sym(47)-s,sym(24));
aux = (-1)^s * nchoosek(sym(24),s) * (bin1-bin2);
tmp = tmp + aux;
end
tmp
But you must know, that symbolic computations are significantly slower than numerical computations (double class).
  댓글 수: 2
Stephen23
Stephen23 2017년 11월 14일
+1 nice explanation.
Nik joung
Nik joung 2017년 11월 15일
Thank you, for your nice explanation! It helps me a lot to get behind such problems.
And if I want to do further calculations with the result, I just use the double()-function on the resulting sym-variable.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Number Theory에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by