Computing values of a formula

3 ビュー (過去 30 日間)
Fatemeh Salar
Fatemeh Salar 2022 年 7 月 2 日
回答済み: Fatemeh Salar 2022 年 7 月 2 日
Hi,
I tried to solve this equation but apperantly my output is wrong, can you tell me what am I missing ?
clc
clear
close all
n=input('Please enter n : ')
x=input('Please enter x : ')
for k=0:floor(n/2);
H=((-1)^k)*factorial(n)*(x^(n-2*k))/(2^k*factorial(k)*factorial(n-2*k));
H=H+((-1)^k)*factorial(n)*(x^(n-2*k))/(2^k*factorial(k)*factorial(n-2*k));
end
H

採用された回答

Abhishek Tiwari
Abhishek Tiwari 2022 年 7 月 2 日
編集済み: Abhishek Tiwari 2022 年 7 月 2 日
Hi,
It appears that it is updating and doubling H with each cycle. So, the code you provided just doubles the result of the final step, i.e. when k = floor (n/2). Try initialising H before the loop and remove first line in the 'for' loop as demonstrated
clc
clear
close all
n = 10;
x = 2;
H = 0;
for k=0:floor(n/2);
H=H+((-1)^k)*factorial(n)*(x^(n-2*k))/(2^k*factorial(k)*factorial(n-2*k));
end
H
H = -2621

その他の回答 (1 件)

Fatemeh Salar
Fatemeh Salar 2022 年 7 月 2 日
Thanks for your help!

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by