How do I write a script that calculates and prints all values for N according to the following expressions and limits?
1 view (last 30 days)
Show older comments
Jennifer Lundberg on 6 Oct 2020
I have earlier used a similar expression to calculate the sum using a for loop (without limits) and it looked like this:
In this case I need to create a while loop how do I start?
/ I am a super-beginner at matlab
You can use two while loops: one for the starting point and the other for the end point.
Stephen23 on 6 Oct 2020
Edited: Stephen23 on 6 Oct 2020
"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;
k = k+1;
s = s+1/k^2;
More Answers (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;
% find the end N
while sum(1./[1:N].^2) <= 1.62
N = N+1;
% the answer is 22 through 39
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!Start Hunting!