Find cosine accurate to 6 digits after decimal point
Show older comments
I'm using the infinite series : cos(x)= 1 -(x^2)/2! + (x^3)/3!
n=0;
cos=1;
sign=1;
while abs(cos)>= 0.000001
n= n+2;
sign=-1*sign;
cos= cos + sign*Angle^n/factorial(n);
end
fprintf('the value of the cosine is %0.6f', cos)
When I run the program, MATLAB says "busy"
Thank you in advance for your help
1 Comment
Jan
on 7 Nov 2012
What is your question?
Answers (2)
Azzi Abdelmalek
on 7 Nov 2012
0 votes
Don't use cos as a variable, because it's a trigonometric matlab function. Also sign is a matlab function
"Busy" means your while loop is still running.
The condition abs(cos) >= 0.000001 is always true for some angles. The actual condition should be: "accurate to 6 digits after decimal point". So you have to use this in the test abs(accurateResult - value). For the accurateResult you need the original cos() function at first.
Another hint: The power operator is very expensive. But you run a loop at all...
Angle = 1.234;
AnglePowered = Angle;
fact = 1;
for ii = 1:5
fact = fact * ii
AnglePowered = -Angle * AnglePowered
end
Now the expnesive power operation is replaced by a cheap multiplication, and as cookie the sign can be included directly.
Categories
Find more on MATLAB in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!