Why is N not being displayed?
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
Kyle Donk
am 17 Jan. 2020
Beantwortet: Image Analyst
am 17 Jan. 2020
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
0 Kommentare
Akzeptierte Antwort
Weitere Antworten (1)
Image Analyst
am 17 Jan. 2020
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.
0 Kommentare
Siehe auch
Kategorien
Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!