how can I create this function in matlab? I am a new user and I am having a hard time.

1 回表示 (過去 30 日間)
Josue Esparza
Josue Esparza 2015 年 3 月 10 日
コメント済み: Chandrasekhar 2015 年 3 月 12 日
Create code that solves sum x^n/n!= x/1! + x^2/2! + x^3/3! + x^4/4! + .... for any value x>0. The function calculates the sum by adding the terms and stoping when the value of the term<=0.00001 or the number of terms exceeds 40.

回答 (1 件)

Chandrasekhar
Chandrasekhar 2015 年 3 月 11 日
function Summantion(x)
n=1;
Sum = 0;
while( n <= 40)
term = pow(x,n)/factorial(n);
if term <= 0.00001
break;
end
Sum = Sum + (x^n)/factorial(n);
n = n+1;
end
disp(['Sum = ' num2str(Sum)])
disp(['Terms = ' num2str(n)])
  2 件のコメント
Josue Esparza
Josue Esparza 2015 年 3 月 11 日
so i tried running the function and it wouldn't work. I tried making some changes but i cant get it to work.
Chandrasekhar
Chandrasekhar 2015 年 3 月 12 日
change pow(x,n) tp x^n. This should work now. In command window you should give the command Summantion(2).. whre x is 2

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

カテゴリ

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