simple "While Loop" questions

1 回表示 (過去 30 日間)
Selcuk Fidan
Selcuk Fidan 2013 年 10 月 21 日
コメント済み: Selcuk Fidan 2013 年 10 月 21 日
Can anyone tell me why this while loop doesn't give me what I want? I want to get simple values such that starting from 0.001 to 0.01 with increment 0.001,0.01 to 0.1 with increment of 0.01,0.1 to 1 with increment 0.1, and 1 to 10 with increment 1. Second question of mine, when I have vsl ==0.1, why it doesn't go into the if loop? What am doing wrong? Thank you!
%% Calculate the gas fraction vsl = 0.001; vsl1 = vsl; vslSave = [];
while vsl<=10
if vsl >=0.001 && vsl<=0.01
increment = 0.001;
elseif vsl >=0.01 && vsl< 0.1
increment = 0.01;
elseif vsl >=0.1 && vsl<=1.0
increment = 0.1;
else
increment = 1;
end
if (vsl==0.1)
increment = 0.1;
end
vsl = vsl + increment;
vslSave = [vslSave;vsl];
end
vslSave = [vsl1;vslSave];

採用された回答

Image Analyst
Image Analyst 2013 年 10 月 21 日
編集済み: Image Analyst 2013 年 10 月 21 日
vsl will never equal 0.1. See the FAQ: http://matlab.wikia.com/wiki/FAQ#Why_is_0.3_-_0.2_-_0.1_.28or_similar.29_not_equal_to_zero.3F And you don't need all those double ranges - checking if it's in the middle. Just check if it's less than the number - it will go into the first one it meets the criteria for.
% Calculate the gas fraction
vsl = 0.001;
vsl1 = vsl;
vslSave = [];
while vsl<=10
if vsl < 0.01
increment = 0.001;
elseif vsl < 0.1
increment = 0.01;
elseif vsl < 1.0
increment = 0.1;
else
increment = 1;
end
vsl = vsl + increment;
vslSave = [vslSave;vsl];
end
vslSave = [vsl1;vslSave]
  1 件のコメント
Selcuk Fidan
Selcuk Fidan 2013 年 10 月 21 日
Thank you for the info!

サインインしてコメントする。

その他の回答 (1 件)

FRANCISCO JAVIER
FRANCISCO JAVIER 2013 年 10 月 21 日
function [vslSave]=prueba(x)
vsl=x; vslSave=[];
while vsl<=10
if vsl >=0.001 && vsl<=0.01
increment = 0.001;
elseif vsl >=0.01 && vsl< 0.1
increment = 0.01;
elseif vsl >=0.1 && vsl<=1.0
increment = 0.1;
else
increment = 1;
end
if (vsl==0.1)
increment = 0.1;
end
vsl = vsl + increment;
vslSave = [vslSave; vsl];
end
end
  1 件のコメント
Selcuk Fidan
Selcuk Fidan 2013 年 10 月 21 日
Hi Francisco,
Thank you for spending time for my question. However, I don't get what you are trying to do? You are just putting my code and serving me as a function, sorry this is not what I asked! I suggest Read answer by Image Analyst.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeProgramming についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by