Output argument "rmax" (and maybe others) not assigned during call to "bestdesign".

1 次查看(过去 30 天)
Output argument "rmax" (and maybe others) not assigned during call to "bestdesign".
M-File
function [max,rmax,hmax]=bestdesign(L,W,H,start,stop)
r=start;
max=0;
if r<=stop
num = InsectContainers(L,W,H,r);
if num > max
max=num;
rmax=r;
else
r=r+0.0001;
end
else hmax=1000/(pi*rmax^2);
end
Can someone tell me why this isn't working please?
Here is the code for InsectContainers.m if it helps
function numContainer = InsectContainers(L,W,H,r)
V = 1000;
h = V/(pi*(r^2));
Stacks = fix(H/h);
Rows = fix(L/(2*r));
Num = fix(W/(2*r));
numContainer = Stacks*Rows*Num;
end

回答(2 个)

Star Strider
Star Strider 2016-10-6
The easiest way to correct that is to assign all your return variables as something, then replace them as necessary in the code:
function [max,rmax,hmax]=bestdesign(L,W,H,start,stop)
[max,rmax,hmax] = deal(NaN, NaN, NaN); % <— ADD THIS LINE
r=start;
max=0;
if r<=stop
num = InsectContainers(L,W,H,r);
if num > max
max=num;
rmax=r;
else
r=r+0.0001;
end
else hmax=1000/(pi*rmax^2);
end

Marc Jakobi
Marc Jakobi 2016-10-6
You should initialize rmax and hmax at the beginning of your function. If r is greater than stop, hmax is never defined and if num is smaller than or equal to max, rmax is never defined.

类别

Help CenterFile Exchange 中查找有关 Mathematics 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by