Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Rewriting a function

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

The error message is "Not enough input arguments."

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading