Find cosine accurate to 6 digits after decimal point

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

Answers (2)

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.
Jan
Jan on 7 Nov 2012
Edited: Jan on 7 Nov 2012
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

Tags

Asked:

on 7 Nov 2012

Community Treasure Hunt

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

Start Hunting!