Is it possible not assigning value for output variable in matlab function?

7 views (last 30 days)
Hello everyone,
I have a problem in my sectional MPPT code in duty cycle output (Dk), since I would like to use previous value of Dk for the next loop when function runs. However, when running the code simulink shows the error
Variable 'Dk' is not fully defined on some execution paths.
The code is below
function [Dk,Pmax1,En1] = PandO_short(Pk,Voc,Vdc,Vk)
persistent Pold1 Dold1 Start1 End1 D_Start1 D_End1 Dk1 i
persistent Vold1
deltaD = 0.001;
diff = 0;
% Dk = 0; % problem variable
En1= 0;
Pmax1 =0;
if isempty(Vold1)
Dk1 = 0;
Start1 = 0.6*Voc; End1 = 0.9*Voc;
D_Start1 = 1-(Start1/Vdc);
Dold1 = D_Start1;
D_End1 = 1-(End1/Vdc);
Pold1 = 0;
Vold1 = Start1;
i = 1;
end
if i ==1
Dk = D_Start1;
end
if (Dk >= D_End1) && (Dk <=D_Start1)
if (Pk-Pold1) ~= diff
if (Pk - Pold1) < diff
if (Vk - Vold1) < diff
Dk = Dold1 - deltaD;
else
Dk = Dold1 + deltaD;
end
else
if (Vk-Vold1) <diff
Dk = Dold1 + deltaD;
else
Dk = Dold1 - deltaD;
end
end
else
Dk = Dold1;
Dk1 = Dold1;
Pmax1 = Pold1;
En1 = 1;
end
end
if (Dk > D_Start1) || (Dk< D_End1)
Dk =Dold1;
En1 = 1;
Pmax1 = Pold1;
Dk1 = Dold1;
end
Dold1 = Dk;
Pold1 = Pk;
Vold1 = Vk;
i =2;
I understand that the output variable need to be assigned at first so that simulink will know type of data, however; when the function is run in its second round , it will turn back to its declared value which I don't want. I still want the previous Dk value from first round in the function.
I would like to know is there anyway that can do without initial assigned value? any suggestion for me?
Thank you very much in advance Joy
  1 Comment
Sumeet Gadagkar
Sumeet Gadagkar on 8 Feb 2018
Edited: Sumeet Gadagkar on 8 Feb 2018
Hello Jirada,
Have you tried setting the "Dk" variable also as persistent? Maybe you can input "Dk" as an input argument to the function, that way you would not have to assign it to zero initially.
Both of these steps together should ideally help resolve your issue.

Sign in to comment.

Answers (0)

Community Treasure Hunt

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

Start Hunting!