Taylor series calculation of sin(x)

10 ビュー (過去 30 日間)
Andy
Andy 2014 年 4 月 5 日
コメント済み: shikha sharma 2021 年 8 月 26 日
hello! The problem I am having trouble with is this:
Calculate g(x) = sin(x) using the Taylor series expansion for a given value of x. Solve for g(pi/3) using 5, 10, 20 and 100 terms in the Taylor series (use a loop)
So I tried the following in the script editor:
clear
clc
n = input('Enter number of iiterations (n): ' );
x = pi/3;
y = zeros(1,n);
for i = 1:n
y(i) = (-1)^i*x^(2*i+1)/factorial(2*i+1);
end
SINx = sum(y);
however when i run the script, the value of SINx that I get isn't what sin(pi/3) is supposed to be and I just can't figure out why.
  2 件のコメント
mehrab aslam
mehrab aslam 2019 年 10 月 21 日
recall the taylor series expansion of sin(x)=x-x^3/3!+... for i=1 , x is not calculated
shikha sharma
shikha sharma 2021 年 8 月 26 日
Angle is taken in radian by MATLAB

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

採用された回答

the cyclist
the cyclist 2014 年 4 月 5 日
With that series, you need to sum starting from 0, not 1:
for i = 0:n
y(i+1) = (-1)^i*x^(2*i+1)/factorial(2*i+1);
end
Notice that I offset your indexing into the y variable, so that the i=0 term is the first one in the vector.
  4 件のコメント
Fritz Ulysse
Fritz Ulysse 2016 年 11 月 7 日
how do you do it for f(x)=e^x
Walter Roberson
Walter Roberson 2016 年 11 月 7 日
exp(x) is sum of (x^N)/(N!) for N = 0 to infinity

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

その他の回答 (0 件)

カテゴリ

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