MATLAB: Output argument “D” (and maybe others) not assigned during call to “E:\PRO\NN​EWPTR\MRG.​m>MRG”.

MATLAB

Hi , I am using forth order RK method to solve my probolem.But while calling one subfunction MRG.m to main function i am getting error message
Output argument "D" (and maybe others) not assigned during call to
"E:\PRO\NNEWPTR\MRG.m>MRG".
here is the code of MRG.m
function [D]= MRG(p_cp,p_t)
rho=2.389;
drg=.032;
dh=.04E-3;
lrg=.8;
mu=15.21E-6;
phi=.7;
r=2074;
t1=(rho*pi*drg*drg*dh*dh)/(600*lrg*mu);
t2=(phi*phi*phi)/(1-phi);
if(p_cp>p_t)
D=t1*t2*(p_cp-p_t);
elseif(p_cp>p_t)
D=t1*t2*(p_t-p_cp);
elseif(p_cp==p_t)
D=0;
end
The main function code which is related to this function is
for m=1:1:100
v_cp=vo+(0.5*vs*(1+sin(omega*t(m))));
v_cpdot=vs*pi*f*cos(omega*t(m));
m_o=MO(p_t(m),p_r(m));
m_rg=MRG(p_cp(m),p_t(m));
end,
Actually it is a part of code of main function,I have not attached the detailed code becz it will be difficult to understtod if required i will send.
plz help……………

Best Answer

Debashis - if you put a breakpoint in your function MRG, call it, and then step through the code using the debugger you will probably find that none of the conditions in the if and elseif block are satisfied, and so D is never instantiated...hence the error that the output parameter D is not assigned any value.
The first line of MRG should initialize this value (and any other output values if they exist) to some default value. In this case,
D = [];
would be sufficient. This will solve the problem given by the error message, but it will not solve the why none of the conditions are being satisfied. Again, use the debugger to make sure that the provided inputs make sense and that the conditions make sense too. I suspect the problem is with your second condition which is the same as the first
if(p_cp>p_t)
D=t1*t2*(p_cp-p_t);
elseif(p_cp>p_t)
D=t1*t2*(p_t-p_cp);
The second condition should probably be
elseif(p_cp<p_t)
(just swap the greater than to a less than).