Function or constraints have too few arguments on the right to satisfy the left

I'm trying to optimize this equation:
f=@(x) 0.25*7850*pi^2*x(1).^2.*x(2).*x(3)
with this set of constraints:
g=@(x) [8*75./x(5)-7; 3.5-2*pi*sqrt(75^2/(4*75.*x(4)-x(5).^2)); 100*exp(-pi.*x(5)./sqrt(4*75.*x(4)-x(5).^2))-15; x(2)-0.75; 75*9.81.*(1.566.*x(1).^2-0.9295.*x(1).*x(2)-2.546.*x(2).^2)./(x(1).^3.*(x(1)-x(2)))-517.1*10^6; 78*10^9.*x(1).^4./(8.*x(2).^3.*x(3))-x(4)];
I'm using the fmincon function in the Optimization toolbox, but no matter what algorithm I select, I see the error "The right hand side of this assignment has too few values to satisfy the left hand side" with no further details. I have been trying to figure out the cause of this error, but I'm having difficulty figuring out what, specifically, the error is referring to.

2 Kommentare

could you provide the full call to fmincon, with any other constraints that you are passing in
I was literally entering those two functions into the Optimization toolbox, but otherwise I use:
>> f=@(x) 0.25*7850*pi^2*x(1).^2.*x(2).*x(3);
>> g=@(x) [8*75./x(5)-7; 3.5-2*pi*sqrt(75^2/(4*75.*x(4)-x(5).^2)); 100*exp(-pi.*x(5)./sqrt(4*75.*x(4)-x(5).^2))-15; x(2)-0.75; 75*9.81.*(1.566.*x(1).^2-0.9295.*x(1).*x(2)-2.546.*x(2).^2)./(x(1).^3.*(x(1)-x(2)))-517.1*10^6; 78*10^9.*x(1).^4./(8.*x(2).^3.*x(3))-x(4)];;
>> x0=[1;1;1;1;1];
>> [X,FVAL,EXITFLAG,OUTPUT,LAMBDA] = fmincon(f,x0,[],[],[],[],[],[],g)
It highlights some kind of issue with my constraint function (g), but I cannot see what the issue is.

Melden Sie sich an, um zu kommentieren.

 Akzeptierte Antwort

Your nonlinear constraints need to be in a function of the form
function [c, ceq] = g(x)
c = [8*75./x(5) .......
ceq = [];
end
MATLAB was complaining because there was no equality constraint information being passed out (even there are no equality constraints).

7 Kommentare

Okay, well now I have:
>> f=@(x) 0.25*7850*pi^2*x(1).^2.*x(2).*x(3);
>> c=@(x) [(8*75./x(5)-7) (3.5-2*pi*sqrt(75^2/(4*75.*x(4)-x(5).^2))) (100*exp(-pi.*x(5)./sqrt(4*75.*x(4)-x(5).^2))-15) (x(2)-0.75) (75*9.81.*(1.566.*x(1).^2-0.9295.*x(1).*x(2)-2.546.*x(2).^2)./(x(1).^3.*(x(1)-x(2)))-517.1*10^6)];
>> ceq=@(x) [(78*10^9.*x(1).^4./(8.*x(2).^3.*x(3))-x(4))];
>> g=@(x) [c(x),ceq(x)]
>> x0=[1;1;1;1;1];
>> [X,FVAL,EXITFLAG,OUTPUT,LAMBDA] = fmincon(f,x0,[],[],[],[],[],[],g)
Which eliminated my original error, but now I'm getting the error
"Error using feval
Output argument "varargout{2}" (and maybe others) not assigned during call to "@(x)[c(x),ceq(x)]"."
I'm really not sure what I'm supposed to be doing with this one.
You can't use an anonymous function, it still returns a single output (in this case an array with c and ceq in it) - save a function file that has the structure of the one I posted above.
Okay, well I've defined a Matlab function as you've specified, but now it's telling me I don't have enough input arguments for the constraints function. I don't know why; I have no problem evaluating it with any x-vector.
Odd ... I had it running before when I tried it:
try this function
http://dl.dropbox.com/u/10495173/foo.m
and use the same f and the same call to fmincon as before (but replace g with @foo)
You know what it was? I wasn't calling the function with the @ sign. It's running now, albeit very slowly on my terrible laptop.
There's a neat trick you can do with anonymous functions to make them return more than one value.
F = @(x) deal( x(1) + x(2), x(1) - x(2) )
[a,b] = F([5 2])
I sometimes use this when trying simple things out in scripts and I don't want to have to save a file.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Community Treasure Hunt

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

Start Hunting!

Translated by