Filter löschen
Filter löschen

Cos Taylor Series script rounds for 4 decimal places but changing to 5 causes it to loop infinitely

5 Ansichten (letzte 30 Tage)
I have to write a script to count the nth number of terms it takes to get a 4 decimal match in cos(x) and the taylor series expansion of cos(x). I am using a while loop and round function to do this, and when i have the round set to 4 decimal places, the script runs as expected, but when I change it to 5 decimal places, it seems to run infinitely. I am also not using the built in factorial function, as the professor took points off for using it in a previous assignmet, so that is what the nested for loop is doing, running the factorial, which may be part of the issue
clear, clc
cts = 1; % Initial sum of cos taylor series, and also first term
x = pi/7;
f= 1;
ii = 1;
while round(cts,4) ~= round(cos(x),4)
ii = ii * 2;
for jj = 1:ii
f = f * jj;
end
nth_term = ( (-1)^(ii / 2) ) * ( (x^(ii) ) / (f));
cts = cts + nth_term;
f = 1;
end
terms = (ii / 2) + 1; % Number of terms (including the first '1' term to
% reach 4 decimal agreement
disp(round(cts,4));
disp(round(cos(x),4));
fprintf('The number of terms needed to reach 4 decimal place is %g.\n', terms)
This returns the expected answers of 0.9010 for both cts and cos(x) and prints 3 terms, but when I change round(..., 4) to round(...,5), it runs the while loop infinitely. Any help would greatly appreciated as to why this happens.
  4 Kommentare
Stephen23
Stephen23 am 10 Nov. 2022
Bearbeitet: Stephen23 am 10 Nov. 2022
"’m also curious as to why 4 decimal places works, but 5 doesn’t though."
Possibly because ROUND is not a robust approach:
The standard robust approach is to compare the absolute difference against a tolerance:
abs(A-B)<tol
Moksh
Moksh am 28 Aug. 2023
Hi Spencer,
You can try using the standard tolerance method and try printing the values for 'cts' and 'cos(x)', to get your required stopping criteria.
Sample code for this:
%% Try using different values for tolerance until the required results
while abs(cts - cos(x)) > tol:
%% Your logic
end
Hope this helps!

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Ayush Anand
Ayush Anand am 4 Sep. 2023
Hi Spencer,
I understand you want to write a script to count the nth number of terms it takes to get a 4 decimal match in cos(x) and the taylor series expansion of cos(x). Setting an equality constraint as the stopping criteria is probably not a good idea, as decimal representation in machine precision works a bit differently than expected.
You can refer to this MATLAB Answer for more understanding on the same:
So, I would recommend using a tolerance factor instead(you can set this according to your precision standards) and comparing it to the absolute difference between the two values of cos(x). This is a standard way of comparison used in code, and as the difference between the values being compared falls below the tolerance factor, we terminate(assume they are close enough to consider them equal). Here is an example of how you could write this in code:
tol= 1e-4 %Set this value according to your requirement
while abs(cts - cos(x)) > tol:
%% Your code
end
I hope this helps!

Weitere Antworten (0)

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by