While loop in While loop: Problem
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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 Kommentare
KL
am 21 Nov. 2017
Bearbeitet: KL
am 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.
read: https://blogs.mathworks.com/community/2009/05/11/keep-your-code-readable-with-smart-indenting/
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?
Antworten (0)
Siehe auch
Kategorien
Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!