How do I write a script that calculates and prints all values for N according to the following expressions and limits?
2 次查看(过去 30 天)
显示 更早的评论
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 个评论
Asad (Mehrzad) Khoddam
2020-10-6
You can use two while loops: one for the starting point and the other for the end point.
采纳的回答
更多回答(1 个)
Asad (Mehrzad) Khoddam
2020-10-6
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
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!