- You forgot to increment your loop counter, the (badly-named) i.
- You forgot to index x so you're just overwriting it every time.
while loop to count partail sum of series
2 ビュー (過去 30 日間)
I'm having problems making while loop to count partial sum (110 iterations) of this series: 1/i*(i+1). There is antoher condition that counting precision must be 1e-4. So the loops breaks when all iterations are done or the precision is achieved. Unfotunetely I'm new to MATLAB and haven't ever done series using while only using for.
Here's what I managed to do, as you can see I haven't done antyhing regarding precision since I don't how to do it and the loop is infinite becasue I don't know to make a proper condiiton to break.
i=1; N=110; x=0;
Image Analyst 2022 年 5 月 16 日
編集済み: Image Analyst 2022 年 5 月 16 日
N = 110; % Max iterations.
x = zeros(1, N);
x(1) = 1 / (1 * 2);
loopCounter = 2;
while loopCounter <= N
thisTerm = 1 / (loopCounter * (loopCounter+1));
x(loopCounter) = x(loopCounter - 1) + thisTerm;
loopCounter = loopCounter + 1;
plot(x, 'b-', 'LineWidth', 2);
ylabel('x, partial sum')