フィルターのクリア

Taylor Series Expansions for sin(x)

12 ビュー (過去 30 日間)
Spaceman
Spaceman 2024 年 3 月 22 日
コメント済み: Spaceman 2024 年 4 月 8 日
Given: sin(x)=(-1)^i*x^(2*i=1)/(2i+1)!...or... ...
Find: Code that calculates the Taylor series for sin(pi/3) using the equation above
Issue: It just seems like my code isn't approximating right, anything I am overlooking here?
My Solution: I also am unsure of how to compare the CORRECT summation results to realsin which happens to be 0.866
x=pi/3;
sum1=0;
N=10;
for i=0:N
a=(2*i+1);
sum1=(-1).^i.*(x.^(2i+1))./factorial(a);
fprintf('This is the estimate of sin(pi/3): %10e \n',sum1)
% diff=sum1-sin(pi/3);
end
% Could also use the input function to have the user enter a number>10, factorial is accurate up to N<=21
% summation here is always negative every other value?
% realsin=pi/3=0.8660
% Need a way to display how close my estimate was, difference between,
% howclose=diff(sum1, realsin)
% fprintf('Estimate was ... off of actual value: %0.10e', howclose)

採用された回答

Voss
Voss 2024 年 3 月 22 日
2i should be 2*i.
And you need to accumulate the sum; as it is now sum1 is only the current term and it's never added to anything.
  11 件のコメント
Voss
Voss 2024 年 4 月 8 日
You're welcome!
Spaceman
Spaceman 2024 年 4 月 8 日
You're the best, Voss!

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

製品


リリース

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by