MATLAB: Fmincon is giving error

fminconMATLAB

Hi everyone, I have a very simple problem to solve 3 linear and one non linear equation using fmincon. I am using fmincon as I need to constrain my solution between lower and upper bound.
I write a function file as,myfun.m
______________________________________________________________________
function F=myfun(x,H2O,alpha,O2,K)
F=[x(2)+x(3)+x(4)-1;
2*x(1)+4*x(4)-H2O-2*alpha;
x(2)+2*x(3)-O2-alpha;
K*x(4)-x(1)^2*(x(1)+x(2)+x(3)+x(4));
];
________________________________________________________________________
and the main file as, model.m, ______________________________________________________________________
H2O=0.8137;
O2=0.0785;
alpha=0.1;
K=1000;
x0=[.1,.1,.1,.1];
f = @(x)myfun(x,H2O,alpha,O2,K);
[x,fval]=fmincon(f,x0,[],[],[],[],[0;0;0;0],[1;1;1;1])
______________________________________________________________________
but fmincon is continuously giving error,as
Error using fmincon (line 618)
User supplied objective function must return a scalar value.
Error in model (line 10)
[x,fval]=fmincon(f,x0,[],[],[],[],[0;0;0;0],[1;1;1;1]);
I am using Matlab R2014b version.
Any help with be highly appreciable.
Thanks

Best Answer

Define your linear and nonlinear equations as constraints for fmincon, not as parts of the objective function.
For the objective function, simply use f=@(x)0
Best wishes
Torsten.