I need to find out what this program is missing

1 view (last 30 days)
Aaron
Aaron on 12 Jul 2012
I'm trying to use this program to calculate the cos of a given(prompted) angle in degrees.
A=input('Enter angle:');
x=(pi/180)*A;
format long
a=0;c=0;sg=1;
while
c=c+sg*((x^(2*a))/(factorial(2*a))); % Infinite series calculation.
a=a+1;sg=-sg;
end
fprintf('cos = %6f',c);
I get no answer and I have to close Matlab and restart.
  4 Comments

Sign in to comment.

Accepted Answer

Albert Yam
Albert Yam on 12 Jul 2012
while [condition] %that should only be true to run the loop
%stuff in loop
end % make sure to end the loop, but should give a warning about that
If your condition is just a constant, than your loop is always running. But your code is not 'copy/paste'-able into Matlab for us to check.
  4 Comments
Aaron
Aaron on 12 Jul 2012
I will remember to use the {} next time. Thank you for your help.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!