Output argument 'd' is not assigned on some execution paths.

I am not understand what's wrong about it. Hope someone to help me find the error. Like the title, the error refers to the d in the first line and the following is the code. Many thanks
function d = fcn(i,Vo)
%%initialize all parameter
persistent PPo LPo detaPo PVo LVo detaVo D detaD n;
if isempty(PPo)% present PV array output power (t[n+1])
PPo=0;
end
if isempty(LPo)% last time PV array output power (t[n])
LPo=0;
end
if isempty(detaPo)% the difference of power output between present one and last one
detaPo=0;
end
if isempty(PVo)% present PV array output voltage (t[n+1])
PVo=0;
end
if isempty(LVo)% last time PV array output voltage (t[n])
LVo=0;
end
if isempty(detaVo)% the difference of output voltage between the last one and present one
detaVo=0;
end
if isempty(D)%duty circle
D=0.3;
end
if isempty(detaD)%min step of changing duty circle
detaD=0.1;
end
if isempty(n)% counting number
n=1;
end
%%main coding
while (detaPo~=0)
d=D;
PVo=-Vo;
PPo=PVo*i;
detaVo=PVo-LVo;
detaPo=PPo-LPo;
if detaPo>=0
if PVo>=0
d=d-detaD;
else
d=d+detaD;
end
else
if PVo>=0
d=d+detaD;
else
d=d-detaD;
end
end
PVo=LVo;
PPo=LPo;
end

 Accepted Answer

What happens if detaPo is equal to zero?
while (detaPo~=0)
d = D;
...
end
then your function will never enter the while loop, and d will never be defined. Thus the error. Perhaps detaPo is defined somewhere else and you are sure that it will never be zero, but the MATLAB code analyzer does not know this.
The easiest solution is to specify some default output:
d = NaN;
while (detaPo~=0)
d = D;
...
end
and make sure that when you call the function fcn that you consider that it might return this value.

5 Comments

Sounds right to me. Zhi, did that solve the problem? If so, please "Accept this answer".
Also, you don't need to double space your code -- read this
hi it seems to be solved, but it shows another problem when I try to combine with my Simulink model: An error occurred while running the simulation and the simulation was terminated
Simulink cannot solve the algebraic loop containing 'pvbuckboost_pwmgenerator_02/PV Array/Diode Rsh/Product5' at time 3.1E-5 using the TrustRegion-based algorithm due to one of the following reasons: the model is ill-defined i.e., the system equations do not have a solution; or the nonlinear equation solver failed to converge due to numerical issues. To rule out solver convergence as the cause of this error, either a) switch to LineSearch-based algorithm using set_param('pvbuckboost_pwmgenerator_02','AlgebraicLoopSolver','LineSearch') b) reducing the VariableStepDiscrete solver RelTol parameter so that the solver takes smaller time steps. If the error persists in spite of the above changes, then the model is likely ill-defined and requires modification.
@Zhi Lu: this is a totally different topic. Please accept this answer (which correctly answers your original question) and then create a new question asking about the simulink error.
thank you and one more question. When I run the code and it doesn't get into the loop although I set a default output, how can I adjust it?
@Zhi Lu: adjust what? You wrote the code so that it does not enter the loop under certain conditions. Now you are complaining that under those conditions it does not enter the loop. You need to decide which you want: the conditions are correct (and it does not enter the loop under those conditions) or you can change the conditions.

Sign in to comment.

More Answers (0)

Asked:

on 27 Jul 2016

Edited:

on 30 Jul 2016

Community Treasure Hunt

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

Start Hunting!