While loop in While loop: Problem

1 次查看(过去 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 CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by