keep getting "too many output arguments" error while using the optimization tool box
7 次查看(过去 30 天)
显示 更早的评论
i was trying to use the tool box to do an optimazation but it just not working. i keep getting the same error meaasage.
here's my objective function
function f=spring_fun(x)
f=(pi^2*x(2)*x(3)^2*(x(1)+2))/4;
end
and my constraints function:
function g=springcon(x)
G=25.5e6; %shear modulus A
%G=41.4e6; %shear modulus T
%G=79.3e6; %shear modulus S
Fmax=5000;
Cf=(4*(x(2)/x(3))-1)/(4*(x(2)/x(3))-4)+(0.615*x(3))/x(2);
K=(G*x(3)^4)/(8*x(1)*x(2)^3);
singma_p=1500/K;
lf=(Fmax/K)+(1.05*(x(1)+2)*x(3));
smax=1.3e9;
sigma_pm=6;
Dmax=3;
dmin=0.2;
lmax=14;
sigma_w=1.25;
Fp=1500;
g(1)=(8*Cf*Fmax*x(2)/pi*x(3)^3)-smax;
g(2)=lf-lmax;
g(3)=dmin-x(3);
g(4)=x(2)-Dmax;
g(5)=3-(x(2)/x(3));
g(6)=sigma_p-sigma_pm;
g(7)=sigma_p+((Fmax-Fp)/K)+1.05*(x(1)+2)*x(3)-lf;
g(8)=sigma_w-((Fmax-Fp)/K);
it just say "too many output argument" can someone tell me what's going on?please.
thank you.
0 个评论
回答(1 个)
Brendan Hamm
2018-11-30
Your non-linear constraint function needs to pass 2 output arguments back with the following calling signature:
function [c,ceq] = nonlcon(x)
as described here https://www.mathworks.com/help/optim/ug/nonlinear-equality-and-inequality-constraints.html
The outputs will enforce the constraints,
If you do not have one of the constraints then you can assign the output the empty array. For example with no non-linear equality you pass the empty array for that output.
function [c,ceq] = nonlcon(x)
c = x.^4; % Constraint we do have
ceq = []; % No equality constraint
end
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Get Started with Optimization Toolbox 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!