필터 지우기
필터 지우기

Why is N not being displayed?

조회 수: 1 (최근 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 CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by