Fix NaN error when n>300

조회 수: 4 (최근 30일)
Marissa Moore
Marissa Moore 2022년 1월 19일
답변: KSSV 2022년 1월 19일
I need to write the following equation so that it works when n is anything greater than 300. It currently works for anythig 300 or less but anything greater I get NaN.
Formula written into code: .
Current code:
n=100;
numerator=1;
for i=n:-2:2
numerator=numerator*i;
end
denominator=1;
for i=n-1:-2:1
denominator=denominator*i;
end
%percision 10^-4
digits(6);
%calculate r
R=numerator/denominator;
disp(R)

답변 (1개)

KSSV
KSSV 2022년 1월 19일
If you have symbolic toolbox use vpa. REad about it.
n=701;
numerator=vpa(1);
for i=n:-2:2
numerator=numerator*i;
end
denominator=vpa(1);
for i=n-1:-2:1
denominator=denominator*i;
end
%percision 10^-4
digits(6);
%calculate r
R=numerator/denominator;
disp(R)
21.1326

카테고리

Help CenterFile Exchange에서 Numbers and Precision에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by