Clear Filters
Clear Filters

While loop in While loop: Problem

1 view (last 30 days)
Miroslav Mitev
Miroslav Mitev on 21 Nov 2017
Edited: KL on 21 Nov 2017
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 Comments
Miroslav Mitev
Miroslav Mitev on 21 Nov 2017
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 on 21 Nov 2017
Edited: KL on 21 Nov 2017
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?

Sign in to comment.

Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!