Fix NaN error when n>300

3 ビュー (過去 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 Center および File ExchangeNumbers and Precision についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by