MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn moreOpportunities for recent engineering grads.

Apply Today**New to MATLAB?**

Asked by John
on 24 Feb 2013

Hi, I've been working on a hw assignment for a while trying to self teach optimization and its proving difficult. I have to minimize the function x1^3-6x1^2+11x1+x3 with the following constraints:

x1^2+x2^2-x3^2<=0 4-x1^2-x2^2-x3^2<=0 x3-5<=0 -x1<=0 -x2<=0 -x3<=0 the starting feasible point is .1,.1,3 So far, by looking at the tutorials I've come up with this coding, but I'm getting an error message about the function definition, saying "x and fval may not be used". Also, the code runs and in the command window k>> appears. Any help would be appreciated.

function f=mae342hw5p2(x) f=x1.^3-6.*x1.^2+11.*x1+x3; function [c,ceq]=confun(x) c=[ x1^2+x2^2-x3^2; 4-x1^2-x2^2-x3^2; x3-5;-x1;-x2;-x3]; ceq = []; x0=[.1 .1 3]; options=optimset('Algorithm','active-set'); [x,fval]=... fmincon(@mae342hw5p2,x0,[],[],[],[],[],[],@confun,options);

*No products are associated with this question.*

Answer by Jing
on 25 Feb 2013

Accepted answer

The input of every function includes 'x', but there's no x in the function body. Actually, you should write your function as follows:(and it's a bit weird without x(2) in your target function...)

function f=mae342hw5p2(x) f=x(1).^3-6.*x(1).^2+11.*x(1)+x(3);

For the constraints, it's better to separate the linear and non-linear constraints, which makes the non-linear constraints function to be:

function [c,ceq]=confun(x) c=[ x(1)^2+x(2)^2-x(3)^2; 4-x(1)^2-x(2)^2-x(3)^2]; ceq=[];

the linear constraints can be set by upper and lower boundary when you call fmincon: lb=[-Inf,-Inf,0];ub=[0,0,5];

Hope you understand!

## 0 Comments