How to apply the sum function of a factorial equation without using the factorial function
現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
古いコメントを表示
How do I use SUM and nested FOR loops to calculate the sigma notation of (2^i)/(i!) when i=0..20 without using the function factorial(i)?
I've tried
A=2^i
SUM(A)=0
for i=1:20
SUM(A)=SUM(A)+i
but this already doesn't work, and I cannot add the nested for loop of the factorial until I have this. How would this nested for loop look without using the factorial() function?
採用された回答
Dear Anj, do you need something like this:
i = 1:20;
val = sum(2.^i);
10 件のコメント
Yes, I've gotten this far but I need to use a FOR Loop to divide each of these values by i! without using the factorial() function on matlab.
In other words SIGMA[(2^i)/(i!)] when i goes from 1 to 20.
you need this:
2^i / i!
in each loop iteration? and then sum up all values?
Yes, but without explicitly using the factorial() function.
ok. let me check it
Here is the code:
sum_all = 0;
for i = 1:20
power_val = 2^i;
fact = 1;
for j = 1:i
fact = fact * j;
end
sum_all = sum_all + power_val / fact;
end
disp(sum_all)
It works. Thank you!
you are welcome
Image Analyst
2013 年 10 月 20 日
編集済み: Image Analyst
2013 年 10 月 20 日
Your inner for loop can be replaced simply by
fact = prod(1:i);
Also we recommend using k or some other variable than i (the imaginary variable) for a loop counter (though it will work). So then your entire program above (now hidden) becomes simply:
sum_all = 0;
for k = 1:20
sum_all = sum_all + 2^k / prod(1:k);
end
disp(sum_all)
Yes you are right. But I used nested for loop because it was insisted by Anj to use nested for loop. Thanks for suggestions about 'i' as loop variable. I will try to avoid it although it is my habit to use 'i' as loop variable. Thanks a lot nevertheless
You're right. I don't know why teachers have students ignore the best part of MATLAB (vectorization) when that is exactly the perfect opportunity to demonstrate it.
その他の回答 (0 件)
カテゴリ
ヘルプ センター および File Exchange で Loops and Conditional Statements についてさらに検索
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の MathWorks のサイトは、お客様の地域からのアクセスが最適化されていません。
南北アメリカ
- América Latina (Español)
- Canada (English)
- United States (English)
ヨーロッパ
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
