I need to find out what this program is missing

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 件のコメント

Yash
Yash 2012 年 7 月 12 日
what error did u got?
Aaron
Aaron 2012 年 7 月 12 日
There was no error. It was just blank. And it was like Matlab was stuck. I could not even input anything new, like "clc" to clear everything.
Albert Yam
Albert Yam 2012 年 7 月 12 日
If you are stuck in a loop. (See 'Busy' in the bottom left.) hit Ctrl+C
Ryan
Ryan 2012 年 7 月 12 日

サインインしてコメントする。

 採用された回答

Albert Yam
Albert Yam 2012 年 7 月 12 日

0 投票

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 件のコメント

Aaron
Aaron 2012 年 7 月 12 日
I need to have my condition for x be accurate to 6 digits after the decimal. I've made the changes below:
A=input('Enter angle:');%angle in degrees x=(pi/180)*A; % radians n=input('Enter the number of places; ') format long a=0;c=0;sg=1; while a~=n % New condition. c=c+sg*((x^(2*a))/(factorial(2*a))); % Infinite series calculation. a=a+1;sg=-sg; end fprintf('%6f',c);
This worked but is there a way to prevent from needing this new condition?
Also how to I return the ">>" instead of leaving it after the answer shown beloow:
>> cosx Enter angle:-30 Enter the number of places; 6 0.866025>> % I need the >> to be on the next line.
Jan
Jan 2012 年 7 月 12 日
Please follow the "About Matlab Answers" link to learn more about the forum, e.g. how code is formatted. Thanks.
Albert Yam
Albert Yam 2012 年 7 月 12 日
A=input('Enter angle:');%angle in degrees
x=(pi/180)*A; % radians
n=input('Enter the number of places; ')
format long
a=0;c=0;sg=1;
while a~=n % New condition.
c=c+sg*((x^(2*a))/(factorial(2*a))); % Infinite series calculation.
a=a+1;sg=-sg;
end
fprintf('%6f',c);
You can always just hardcode n=6, in the condition, since you said it is a 'need'. While loops need a condition, how else would it know to stop looping (your previous problem)? Unless you know exactly how many times to loop (For loop). For the '>>' on the next line,
fprintf('%6f\n',c);
Please use the {}code formatting next time. It is easier for people to copy/paste into Matlab to see what is wrong.
A=input('Enter angle:');%angle in degrees x=(pi/180)*A; % radians n=input('Enter the number of places; ') format long a=0;c=0;sg=1; while a~=n % New condition. c=c+sg*((x^(2*a))/(factorial(2*a))); % Infinite series calculation. a=a+1;sg=-sg; end fprintf('%6f',c);
Comments out everything after the A=input
Aaron
Aaron 2012 年 7 月 12 日
I will remember to use the {} next time. Thank you for your help.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by