While loop in While loop: Problem

2 ビュー (過去 30 日間)
Miroslav Mitev
Miroslav Mitev 2017 年 11 月 21 日
編集済み: KL 2017 年 11 月 21 日
The code does not work properly. It stops after 2 iterations, instead it should make much more. The idea is:
For each main loop to store the value of x_2 when beta becomes greater that 0.1 in x_2_new. Then increase K and start again: i.e. find the values of P_1, P_2 when beta>0.1 for the specific K and so on.
%%NOTATIONS
P=5;
P_1=0.1;
K=1;
x_2_new=[0, 0];
i=2;
%%MAIN LOOP
while x_2_new(i)>=x_2_new(i-1)
K=K+1;
i=i+1;
beta=0;
%%SECOND LOOP
while beta<0.1
P_1=P_1+0.1;
P_2=P-P_1;
x_1=K*log(1+P_1/(2+1/P_1));
x_2=log(1+P_2);
beta=x_1/x_2;
end
x_2_new(i)=x_2;
end
  3 件のコメント
Miroslav Mitev
Miroslav Mitev 2017 年 11 月 21 日
Thank you. Your comment was helpful. Do you know how to make the loop:
while beta<0.1 do something
while beta>0.15 do something else.
Is it possible to make it in single loop?
KL
KL 2017 年 11 月 21 日
編集済み: KL 2017 年 11 月 21 日
There are so many issues.
But first, do proper indentations. This is very basic. Matlab does it for you automatically but even if you copy paste your code from somewhere, you can fix it easily by selecting all your code and pressing CTRL+I.
Next, what's the size of your x_2_new vector. You have assigned it as 1x2 vector but later you increment i, do you wanna keep increaming i until its last value becomes greater than the previous one? Or do you just want to replace the 2 values?

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

回答 (0 件)

カテゴリ

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