# How do I write a script that calculates and prints all values ​​for N according to the following expressions and limits?

조회 수: 3(최근 30일)
Jennifer Lundberg 2020년 10월 6일
편집: Stephen23 2020년 10월 6일
I have earlier used a similar expression to calculate the sum using a for loop (without limits) and it looked like this:
for N=1:100
S(N)=S(N-1)+(1/N^2)
end
In this case I need to create a while loop how do I start?
/ I am a super-beginner at matlab
##### 댓글 수: 1표시숨기기 없음
You can use two while loops: one for the starting point and the other for the end point.

댓글을 달려면 로그인하십시오.

### 채택된 답변

Stephen23 2020년 10월 6일
편집: Stephen23 2020년 10월 6일
"How do I write a script that calculates and prints all values for N according to the following expressions and limits?"
k = 0;
s = 0;
while s<=1.62
if s>=1.6
disp(k)
end
k = k+1;
s = s+1/k^2;
end

댓글을 달려면 로그인하십시오.

### 추가 답변(1개)

N=1;
% sum function uses an internal loop
% find the first N that sum is >= 1.6
while sum(1./[1:N].^2) < 1.6
N = N+1;
end
disp(N)
% find the end N
while sum(1./[1:N].^2) <= 1.62
N = N+1;
end
disp(N-1)
% the answer is 22 through 39

댓글을 달려면 로그인하십시오.

### Community Treasure Hunt

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

Start Hunting!

Translated by