use a loop to print out 15 rational number between 1/12 and 2/12

4 ビュー (過去 30 日間)
Liliana Rios
Liliana Rios 2018 年 11 月 28 日
回答済み: KSSV 2018 年 11 月 28 日
I know there are an infinite amount of numbers between the 2 fractions.
i=1/12;limit=2/12;while i <= limit display(i);i=i+1;end
When the above is entered into MATLAB, it'll give one decimal answer

回答 (1 件)

KSSV
KSSV 2018 年 11 月 28 日
YOu should not add 1 to the i. YOu must add some small value like fix a tolerance and add. Check the below code:
i=1/12;
limit=2/12;
tol = 10^-3 ;
iwant = zeros([],1) ;
iwant(1) = i ;
count = 1 ;
while i <= limit
display(i);
i=i+tol;
count = count+1 ;
iwant(count) = i ;
end

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by