I am getting the error message "Variable 'D' is not fully defined on some execution paths." for this code. please help me

function D = mpp(V, T)
%Vinit = 14;
delD = 0.05;
Dmax = 0.8;
Dmin = 0.1;
persistent V0 n D0;
if isempty(V0)
V0 = 14;
n = 1;
D0 = 0.1;
end
dV = V - V0;
a = dV/delD;
if(T>0.02*n)
n=n+1;
if (a>0)
D = D0 + delD;
elseif (a<0)
D = D0- delD;
else
D= D0;
end
end
if (dV==0)|(delD==0)
D = D0;
end
if D>=Dmax | D<=Dmin
D = D0;
end
V0 = V;
D0 = D;

Answers (1)

Hi,

i think your code does not cover all possible cases. If variable T <= 0.02 when calling the function e.g. x = mpp(10, 0.005) then variable D will not be intialised (because n=1) due to the complete following section of code will not be executed:

if(T>0.02*n)
    n=n+1;
    if (a>0)
        D = D0 + delD;
    elseif (a<0)
        D = D0- delD;
    else
        D= D0;
    end
end

The next section of code will never be true (and due to that never be executed), because you initialise both with values different from zero (when starting your code V0 is always empty, afterwards it is also initialised with values different from Zero):

if (dV==0)|(delD==0)
    D = D0;
end

Finally the next section of code will produce error, because there is no value for D to compare with Dmax oder Dmin:

if D>=Dmax | D<=Dmin
    D = D0;
end

So when using values for T <= 0.02 when calling the function D will never get a value...

That helped?

Categories

Find more on Programming in Help Center and File Exchange

Products

Tags

Asked:

on 20 Mar 2018

Answered:

on 18 Apr 2018

Community Treasure Hunt

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

Start Hunting!