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

조회 수: 1 (최근 30일)
Bob Sagat
Bob Sagat 2016년 10월 6일
답변: Marc Jakobi 2016년 10월 6일
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