Repeating while loop until the conditions are met

7 Ansichten (letzte 30 Tage)
Fatima Yusuf
Fatima Yusuf am 10 Mai 2021
Beantwortet: Jan am 11 Mai 2021
The following code should calculate the powers: P1, P2, and P3, I found them correctly, but there are limits for each power.
P1 should be within the limits written in 1st row of the limits matrix
P2 should be within the limits written in 2nd row of the limits matrix, and so on..
If any power isn't within the specified range, the while loop should be repeated to recalculate the powers. Once each power is within its limits, the while loop continues.
The rest of my code is running well. I comments with questions marks within the code to be easier for you to refer to the part I am asking about.
So, how to repeat the while loop?
clc;
clear all;
cost=[200 7 0.008
180 6.3 0.009
140 6.8 0.007];
b=[0.0218
0.0228
0.0179]; %per unit
limits=[10 125
10 150
35 225];
pd=150;
sb=100; %base
beta=b/sb;
%pl=beta/sb; %convert to actual
n= length(cost);
sumb=0;
suma=0;
for i=1:n
suma=cost(i,2)+suma;
sumb=cost(i,3)+sumb;
a(i)=cost(i,2);
b(i)=cost(i,3);
min(i)=limits(i,1);
max(i)=limits(i,2);
end
suma_2b=0;
sum1_2b=0;
slope=2*cost(:,3);
for i=1:n %find the summation
suma_2b=suma_2b+cost(i,2)/(2*cost(i,3));
sum1_2b=sum1_2b+1/(2*cost(i,3));
end
lambda_it0=(pd+suma_2b)/sum1_2b; %or inside the loop??
Pi=0;
sumPi=0;
lambda=lambda_it0;
oldlambda=lambda;
P=zeros(n,1);
for j=1:n
P(j)=(lambda-a(j))/(2*(b(j)+beta(j)*lambda));
Ptotal=Pi+P(j); %Ptotal for each loop
Pi=P(j)+Pi;
%sumPi=Pi;
end
Ptotal;
Powers_it0=P;
%Newton Raphson
f_lambda_p0=0;
pl=beta(1)*P(1)^2+beta(2)*P(2)^2+beta(3)*P(3)^2; %CHANGE IT !!!
pl_it0=pl;
f_lambda_it0=Pi-pd-pl;
f_lambda=f_lambda_it0;
for i=1:n
f_lambda_p_it0(i)=(b(i)+a(i)*beta(i))/(2*(b(i)+beta(i)*lambda_it0)^2);
f_lambda_p0=f_lambda_p_it0(i)+f_lambda_p0;
end
f_lambda_p=f_lambda_p0;
f_lambda_p_it0=f_lambda_p0;
lambda=lambda_it0;
lambda_it0=lambda;
iteration = 1;
E=8.854187817*10^-(12) ;
newlambda=1;
oldlambda=lambda_it0;
%while f_lambda~=0 || abs(newlambda-lambda)>E || f_lambda>E
% while f_lambda~=0 || abs(newlambda-lambda)>E
iter=1;
while abs(newlambda-oldlambda)>E
if round(f_lambda,4)==0
break;
end
oldlambda=lambda;
lambda=lambda-(f_lambda/f_lambda_p); %New Lambda
newlambda=lambda;
P=zeros(n,1);
Pi=0;
for j=1:n %New powers
P(j)=(lambda-a(j))/(2*(b(j)+beta(j)*lambda));
Ptotal=Pi+P(j); %Ptotal for each loop
Pi=P(j)+Pi; %sumPi=Pi;
%check each power within the limits or not, if not recalculate( repeat the loop)
for y=1:n
if P(n)>=max(n) && P(n)<=min
%????????????????????????????????????????????????????????????????????????????????????????????????????????????
%????????????????????????????????????????????????????????????????????????????????????????????????????????????
%????????????????????????????????????????????????????????????????????????????????????????????????????????????
%????????????????????????????????????????????????????????????????????????????????????????????????????????????
%????????????????????????????????????????????????????????????????????????????????????????????????????????????
%????????????????????????????????????????????????????????????????????????????????????????????????????????????
end
end
end
P;
f_lambda=Pi-pd-pl; %new f(lambda)
if round(f_lambda,4)==0
break;
end
f_lambda_p0=0; %new f_lambda_p
for i=1:n
f_lambda_p(i)=(b(i)+a(i)*beta(i))/(2*(b(i)+beta(i)*lambda)^2);
f_lambda_p0=f_lambda_p(i)+f_lambda_p0;
f_lambda_p= f_lambda_p0;
end
pl=beta(1)*P(1)^2+beta(2)*P(2)^2+beta(3)*P(3)^2;
ploss(iter)=pl;
Lambda(iter)=newlambda;
f_Lambda(iter)=f_lambda;
f_LambdaP(iter)=f_lambda_p;
iterations=iter;
iter=iter+1;
end
Iterations=iterations
Lambda=[lambda_it0, Lambda]'
fLambda=[f_lambda_it0,f_Lambda]'
fLambdaP=[f_lambda_p_it0,f_LambdaP]'
Ploss=[pl_it0,ploss]'
%disp('Powers at last iteration:')
Powers___P1_P2_P3___at_last_iteration =P

Antworten (1)

Jan
Jan am 11 Mai 2021
It is not getting clear to me, what you are asking for. So some hints at first:
Pi=0;
sumPi=0;
P=zeros(n,1);
for j=1:n
P(j)=(lambda-a(j))/(2*(b(j)+beta(j)*lambda));
Ptotal=Pi+P(j); % This variable is overwritten in each iteration
Pi=P(j)+Pi; % P(j) is counted twice?
end
Ptotal; % This statement does nothing than wasting time
A simplification of the code without loops:
P = (lambda - a) ./ (2 * (b + beta .* lambda));
Ptotal = sum(P);
It is a frequent source of troubles to redefine built-in function, e.g. "min" and "max":
limits=[10 125
10 150
35 225];
...
for i=1:n
min(i)=limits(i,1);
max(i)=limits(i,2);
end
Better:
limitLow = limits(:, 1);
limitUp = limits(:, 2);
Matlab is much nicer, if you omit the loops. The simpler the code, the less likely are typos and confusions.
Thois looks strange:
f_lambda_p0 = 0;
for i=1:n
% This looks, like f_lambda_p should be a vector:
f_lambda_p(i) = (b(i)+a(i)*beta(i)) / (2*(b(i)+beta(i)*lambda)^2);
% Then f_lambda_p0 is created as scalar:
f_lambda_p0 = f_lambda_p(i) + f_lambda_p0;
% Now f_lambda_p is overwritten with a scalar:
f_lambda_p = f_lambda_p0;
end
Is this really intented?
You use 3 different variables to count the number of iterations:
iter=1;
while abs(newlambda-oldlambda)>E
...
iterations = iter;
iter = iter+1;
end
Iterations = iterations
This increases the complexity and reduced the clarity of the code.
"n= length(cost);" LENGTH() is a frequent source of bugs, because it smartly decides for the longest dimension dynamically. If you mean a certain dimension, use size(cost, dim) instead.
Summary: The code looks far to complicated to be clear. Several loops seem to fail. In consequence, I cannot guess, what you try to insert.

Kategorien

Mehr zu Creating and Concatenating Matrices 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!

Translated by