Why is N not being displayed?

3 ビュー (過去 30 日間)
Kyle Donk
Kyle Donk 2020 年 1 月 17 日
回答済み: Image Analyst 2020 年 1 月 17 日
My problem: I am trying to find the smallest value of N for which error is less than 10^-4. I am just wondering why N is not being displayed at the end of the code.
N=10;
error=1;
while error<10^-4
N=N+1;
sum=0;
for n=1:N
y=1/n^2;
sum=sum+y;
end
error=((pi^2)/6)-sum;
disp(N)
end

採用された回答

Matt J
Matt J 2020 年 1 月 17 日
編集済み: Matt J 2020 年 1 月 17 日
Because the loop condition is never satisfied and therefore nothing within it is ever executed. I think you meant to have,
while error>10^-4

その他の回答 (1 件)

Image Analyst
Image Analyst 2020 年 1 月 17 日
Because you initialized error to 1 so it never enters the while loop at all.
By the way, do not call your variables "error", "sum" or any other built-in function name or you will run into problems.

カテゴリ

Help Center および 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