Program to approximate e^x using the Taylor Series

I am attempting to write a function called expSeries which uses another function factFunc to evaluate e^x. I have already written the function factFunc, as shown below:
function fact = factFunc(n)
f = 1;
for a = 1:b
f = f*a;
end
fact = f;
end
I am now attempting to write the function expSeries which evaulates e^x using the Taylor series. This is what I have so far:
function expo = exponentialFunc(x)
terms = input('Enter the number of terms');
b = 0;
for i = 1:terms
b = x/factFunc(terms);
end
expo = b;
end
And in the main program, I have
n = exponentialFunc(4);
disp(n);
Where in this instance I am trying to find e^4. However, the output is not what expected. Does anyone have any idea where I am going wrong?

回答 (2 件)

Walter Roberson
Walter Roberson 2018 年 11 月 8 日

0 投票

Taylor series involve the sum of terms but your
for i = 1:terms
b = x/factFunc(terms);
end
Is overwriting all of b each cycle.

3 件のコメント

Torsten
Torsten 2018 年 11 月 8 日
編集済み: Torsten 2018 年 11 月 8 日
And in "factFunc", you must use "n" instead of "b".
Walter Roberson
Walter Roberson 2018 年 11 月 8 日
? You would use i not n
Torsten
Torsten 2018 年 11 月 8 日
for a = 1: *b*
should read
for a = 1: *n*
Best wishes
Torsten.

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

カテゴリ

ヘルプ センター および File ExchangeMathematics についてさらに検索

質問済み:

2018 年 11 月 8 日

コメント済み:

2018 年 11 月 8 日

Community Treasure Hunt

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

Start Hunting!

Translated by