While loop in While loop: Problem

조회 수: 5 (최근 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