Home > Blockchain >  Rewriting a function
Rewriting a function

Time:11-04

In the following code I would like to replace the definition of the function f

f=@(x)-x(1)*x(2)*x(3);
x0=[1 1 1];
lb=[0 0 0];

nonlincon=@constr;

x=fmincon(f,x0,[],[],[],[],lb,[],nonlincon)

function [c,ceq] = constr(x)
c = [2*x(1)*x(2) 2*x(1)*x(3) 2*x(2)*x(3)-100 ; 1-x(1)*x(2)];
ceq = [];
end

I replaced it with

function erg = f(x)
erg = -x(1)*x(2)*x(3);
end

but unfortunatly it doesn't work. What am I doing wrong?

The error message is "Not enough input arguments."

CodePudding user response:

In your first code snippet, f is a function handle, while in the second, its a function.

You can easily make it a function handle to be called by fmincon by calling x=fmincon(@f,x0,[],[],[],[],lb,[],nonlincon)

Otherwise matlab is trying to call f and give its output to fmincon, which is not what you want to do.

  • Related