0
votes

Feel free to close this if I'm not understanding fminsearch or just butchering the lingo, but here's my question.

Can I use fminsearch with a constraint on multiple parameters together?

fminsearch(@(x) func(x(1), x(2)), [2,2], such that x(1) * x(2) = 4 or something like that)
1
for your example, you could create a function taking only one parameter:´func2(y) = func(y,4/y)´ and minimize this function. Then you can set ´x=[y,y/4]´ - vipers36
thanks, I ended up going with a penalty approach, matlab is so foreign to me, it's like learning my first programming language all over again, except I remember enjoying that. - Kingpin2k

1 Answers

2
votes

Nonlinear optimization is a very difficult problem, so no method is guaranteed to work for every case. For your case, you can solve x(2) analytically from x(1). So you can make it into an unconstrained optimization problem.

func_cstr = @(x) func(x, 4/x);
fminsearch(func_cstr, initial_x1)

If you cant make an explicit relationship between x(1) and x(2), then you can try a penalty method:

pen = 1e5;
constraint = @(x) (x(1)*x(2)-4)
func_cstr = @(x) func(x)+pen*constraint(x)^2;
[x,fval] = fminsearch(func_cstr, initial_x1_and_x2);

There is also a constrained optimization solver fmincon provided by MATLAB.