Setting condition for a variable in an iteration loop

I have a iteration loop in which i want to keep 0<z<1 I added if at start and end of loop, the code runs without errors but I still get z=-3, code is like this(not exact code)
x_1=4; y_1=5; for w = 1:1000 z = (2.5*x_1)/(2.5*y_1 -2); if (z > 1) (z < 0), break; else . . x_1=x_2; y_1=y_2; if (z > 1) (z < 0), break; else if abs(x_1 - Q) < 1e-03, break; end end end end So how can I force the condition for z? Thanks in advance

3 件のコメント

Jan
Jan 2012 年 12 月 2 日
Where do you get "z=-3" and how do you detect this? Are you sure, that the posted code contains the cause of the problem?
Exergy
Exergy 2012 年 12 月 3 日
After the iteration since z depends on x_1 and y_1 then you can get its value.
Jan
Jan 2012 年 12 月 3 日
I suggest to format the code.

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

回答 (2 件)

Azzi Abdelmalek
Azzi Abdelmalek 2012 年 12 月 2 日
編集済み: Azzi Abdelmalek 2012 年 12 月 2 日

0 投票

use
if z>0 & z<1

2 件のコメント

Exergy
Exergy 2012 年 12 月 3 日
Is there a difference between "if z>0 && z<1, continue;" and "if z<0 z>1, break;"? Can you explain a bit more what you mean?
Azzi Abdelmalek
Azzi Abdelmalek 2012 年 12 月 3 日
Use whatever you want, the problem is in your syntax, if (z > 1) (z < 0),
The correct syntax is
if z<0 | z>1
or
if z>0 & z<1
or
z>0 && z<1

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

Image Analyst
Image Analyst 2012 年 12 月 2 日
編集済み: Image Analyst 2012 年 12 月 2 日

0 投票

Did you want to recalculate z after calculating x_1 = x_2 and y_1=y_2? Because z will be the same there if you don't. And you may get a -3 for z but it should break after that - doesn't it?

カテゴリ

ヘルプ センター および File Exchange で MATLAB についてさらに検索

製品

質問済み:

2012 年 12 月 2 日

Community Treasure Hunt

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

Start Hunting!

Translated by