trying to use the fmincon function

2 次查看(过去 30 天)
Hello,
I am trying to use the fmincon with a external function definition.
I ran the example at https://www.mathworks.com/help/optim/ug/fmincon.html and it wroked as described
I tried to define the function as an external function as shown below.
% script calling fmincon start
clc
clear all
x0=[-1,2];
A=[1,2];
b=1;
zeta=fmincon(fun,x0,A,b);
% script calling fmincon end
% script defining fun start
function q=fun(x)
q=100*(x(2)-x(1)^2)^2+(1-x(1))^2;
end
% script defining fun end
I get this error: Not renough input arguments from the fun script
Not sure how to fix this probem
Thank you

采纳的回答

Torsten
Torsten 2022-6-22
Works for me after changing "fun" in "@fun":
% script calling fmincon start
clc
clear all
x0=[-1,2];
A=[1,2];
b=1;
zeta=fmincon(@fun,x0,A,b)
Local minimum found that satisfies the constraints. Optimization completed because the objective function is non-decreasing in feasible directions, to within the value of the optimality tolerance, and constraints are satisfied to within the value of the constraint tolerance.
zeta = 1×2
0.5022 0.2489
% script calling fmincon end
% script defining fun start
function q=fun(x)
q=100*(x(2)-x(1)^2)^2+(1-x(1))^2;
end
% script defining fun end

更多回答(0 个)

类别

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

产品


版本

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by