Azzera filtri
Azzera filtri

How can i use any variable once for initialization and then the variable is updated and should be replaced with new value? I tried one way, but it isn't working

5 visualizzazioni (ultimi 30 giorni)
This is one of the function file of main program. Here the variable V is needed in line no 13, but the variable V is output of another function file ([V,VSI,DGPI] = bibc_dg(Lc,Pvg,Qvg)), which is simulated after this. Now i tried an attempt to put iter_count variable, i thought that it would update itself and then new values of variable V will be considered. but it is also not working.
function [ O ] = ObjectiveFunction1 (x)
Pvg=x(:,1:4);
Lc=[16 17 18 32];
%% %%% This piece of code is only to start initialization process %%%%%%%%%%%%%
iterCount = 0;
if iterCount==0
V_base=[1.0000 0.9159 0.9139 0.9133 0.9965 0.9929 0.9922 0.9916 0.9794 0.9169 0.9166];
V=V_base;
else
V=V;
end
iterCount = iterCount + 1;
%%%%% These above lines are not part of main code, but coded here only to check whether the new V values were updated or not
V_q=[V(Lc(1)) V(Lc(2)) V(Lc(3)) V(Lc(4))];
S=6;
Pvgg=max(Pvg);
Q_max=sqrt(S.^2-Pvgg.^2);
for i=1:4
if V_q < 0.95
Qvg(i) = 0.5*Q_max;
elseif V_q >= 0.95 && V_q <= 1
K1=Q_max/0.95;
Vd=1-V_q(i);
Qvg(i)=K1.*Vd;
elseif V_q > 1 && V_q <=1.05
K2=Q_max/1.05;
Vd=1-V_q(i);
Qvg(i)=K2.*Vd;
else
Qvg(i) = 0.5*Q_max;
end
end
%%
[V,VSI,DGPI] = bibc_dg(Lc,Pvg,Qvg);
if (max(abs(V))-1.05)>0
% if (max(abs(1)-1.05)>0 || max(abs(1)-0.95)<0 )
O = inf; %% To make sure that voltage voilation sol never gets accepted
elseif (min(abs(V))-0.95)<0 %%% change to minimum and check DGPI
O = inf;
else
O = max(DGPI);
end
end

Risposta accettata

Walter Roberson
Walter Roberson il 25 Nov 2022
persistent V iter_count
if isempty(V)
V = [1.0000 0.9159 0.9139 0.9133 0.9965 0.9929 0.9922 0.9916 0.9794 0.9169 0.9166];
iter_count = 0;
end
However,
function [ O ] = ObjectiveFunction1 (x)
Objective Functions are used in the context of minimizers, and minimizers almost always need calculations to be repeatable: the same input x should give the same output no matter how many iterations there have been so far.
if V_q < 0.95
The majority of minimizers require that the calculations have continuous derivatives. Most of the time when there is an if statement or an interp1() or interp2() call, people have not taken care to match the derivatives at the boundary points, and so for most minimizers using if or interpolation will give problems. There are some minimizers that do not require continuous derivatives, such as genetic algorithm.

Più risposte (0)

Categorie

Scopri di più su Loops and Conditional Statements in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by