Cos Taylor Series script rounds for 4 decimal places but changing to 5 causes it to loop infinitely
1 次查看(过去 30 天)
显示 更早的评论
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 个评论
Moksh
2023-8-28
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!
采纳的回答
Ayush Anand
2023-9-4
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:
https://in.mathworks.com/matlabcentral/answers/269201-what-s-going-on-with-the-precision-calculation
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!
0 个评论
更多回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!