How to display certain iterations

%% Problem 3
clc;
n=1
k=1
sum=0
while n<15000
sum=sum+(2/(k^4))
n=n+1
k=n
end
I need to display the first 10 and the last 10 iterations, but I am not sure how to do it.
I was thinking using a ones or zeros function to store the values and then only display 1-10 and 14991-15000, but I cant remember how to execute that. Is that the way to go about this problem, if so how would I, or is there an easier way to solve it?

回答 (2 件)

Walter Roberson
Walter Roberson 2023 年 9 月 2 日

0 投票

if (n >= some_lower_bound && n <= some_upper_bound) || (n >= some_other_lower_bound && n <= some_other_upper_bound)
disp(something appropriate)
end
VINAYAK LUHA
VINAYAK LUHA 2023 年 9 月 2 日
編集済み: VINAYAK LUHA 2023 年 9 月 2 日

0 投票

%% Problem 3
clc;
n=1;
k=1;
sum=0;
dp = zeros(20,2);
i=1;
while n<15001
if(n<11 || n>14990)
dp(i,1)=n;
dp(i,2)=sum;
i=i+1;
disp(['n=', num2str(n),' sum=',num2str(sum)]);
end
sum=sum+(2/(k^4));
n=n+1;
k=n;
end

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

製品

質問済み:

2023 年 9 月 2 日

編集済み:

2023 年 9 月 2 日

Community Treasure Hunt

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

Start Hunting!

Translated by