フィルターのクリア

how do I use the factorial function in a for loop

4 ビュー (過去 30 日間)
juan Ortiz
juan Ortiz 2018 年 12 月 2 日
編集済み: John D'Errico 2018 年 12 月 3 日
I have the followig formula
W = N!/h!(N-h)! how do I write a loop when 30, and h =30
for h=1:30
W(h)= factorial(30)/factorial(30)*factorial((30-h))
end
This is what I have so far
  1 件のコメント
Walter Roberson
Walter Roberson 2018 年 12 月 3 日
You lose a lot of precision by actually calculating the factorial

サインインしてコメントする。

回答 (1 件)

John D'Errico
John D'Errico 2018 年 12 月 3 日
編集済み: John D'Errico 2018 年 12 月 3 日
I see one other problem in the code you have written.
I would guess that really, the formula you WANTED to write is:
W(h) = N! / (h! * (N-h)!)
where both of these terms are in the denominator. Since that formula is pretty classic, I'm willing to bet this is true.
So what is the problem? Look at the expresion:
A/B*C
For example, what is 9/3*3 in MATLAB? Should it be 1? Or how about 9? Any guesses? It is 9.
9/3*3
ans =
9
So really, what you wanted to write is this:
W(h) = factorial(30)/(factorial(h)*factorial(30-h));
Or, you could have written it as
W(h) = factorial(30)/factorial(h)/factorial(30-h);
Either will work. Well, I'm willing to bet it is something like that.

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by