Breaking a loop and displaying iteration

Hi, I have a simple loop for simulating pressure in a bicycle pump and tire. I have a for loop to simulate the pressure for each stroke of the pump, but I need to display only the values up until the pressure reaches 120 psi. How do I stop the loop once the value reaches this? Is there a way to display how many iterations it took to achieve this value?
Here is my code as of now, (I'm relatively new to Matlab and not very good)
p=14.7;
for i=1:101
p = ((patm*v1+p*vtire)/(v2+vtire))
end
while p <= 120
p = ((patm*v1+p*vtire)/(v2+vtire))
if p>120, break, end
disp(p)
end

回答 (1 件)

Azzi Abdelmalek
Azzi Abdelmalek 2013 年 10 月 2 日
編集済み: Azzi Abdelmalek 2013 年 10 月 2 日

0 投票

while p <= 120
p = ((patm*v1+p*vtire)/(v2+vtire))
end
disp(p)
You don't need to break, when p>120 it leaves automatically the loop
You will need to initialize p

3 件のコメント

Nick
Nick 2013 年 10 月 2 日
I tried taking the break function out, but when I ran the script it still outputs all the values. There are 101 simulated strokes and the value of p gets up to around 10,000.
Azzi Abdelmalek
Azzi Abdelmalek 2013 年 10 月 2 日
post the entire code to allow testing it
Image Analyst
Image Analyst 2013 年 10 月 2 日
You probably still left that first "for" loop in there. Get rid of that. You also need to define v2.

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

カテゴリ

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

質問済み:

2013 年 10 月 2 日

コメント済み:

2013 年 10 月 2 日

Community Treasure Hunt

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

Start Hunting!

Translated by