How can I obtain the right convergence number from what I have done so far??

1 回表示 (過去 30 日間)
Hello Everybody, I hope you are doing well.
I was asked to write a script which calculates the number of terms or iterations needed for a taylor series approximation of cos(x) to converge using a while.
The condition requires that the loop should continue as long as abs(Estimate – Previous Estimate) exceeds 0.00001.
Here is my script:
clc
clear
PreviousEstimate = inf;
x = input('Enter the angle in radians: ');
Count = 0;
Estimate = 1;
while abs(Estimate - PreviousEstimate) > 0.00001
Estimate(Count+1) = (-1)^Count*(x.^(2*Count))/factorial(2*Count);
Count = 0 + 1;
PreviousEstimate = Estimate;
end
fprintf('%i terms required for convergence. \n',Count)

採用された回答

Mathieu NOE
Mathieu NOE 2021 年 1 月 11 日
hello
this seems to work better - :)
clc
clear
x = input('Enter the angle in radians: ');
Count = 0;
Estimate = inf;
PreviousEstimate = 0;
while abs(Estimate - PreviousEstimate) > 1e-6
PreviousEstimate = Estimate;
Estimate = (-1)^Count*(x.^(2*Count))/factorial(2*Count);
Count = Count + 1;
end
fprintf('%i terms required for convergence. \n',Count)
  4 件のコメント
Edivaldo Bartolomeu
Edivaldo Bartolomeu 2021 年 1 月 12 日
My bad Mathieu you are absolutely right, I made a mistake in the first attempts.
Thanks for your help.
Mathieu NOE
Mathieu NOE 2021 年 1 月 12 日
ok ! good luck for the future !

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

その他の回答 (0 件)

カテゴリ

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

製品


リリース

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by