Problems to load the parameters of the objective function in fmincon

Hi! I have a problem when I want to pass as an argument the parameters to implement the objective function in a different file from where I call the fmincon function:

If I don’t pass any parameters as arguments, I don’t have any problem.

Taking the example from the function description:
In this case, that the objective function does not need arguments, the operation is normal:

## Example for default optimization (Levenberg/Marquardt with
## BFGS), one non-linear equality constraint. Constrained optimum is
## at x = [0; 1].

pkg load optim
xin = [-2; 5];
## a=1;
constraint_function = @ (x) - (x(1)^2 + 1 - x(2));
[x, funj, cvg, outp] = fmincon(@fun, xin, [], [], [], [], [], [], @ (x) {[], constraint_function(x)}{:},options)

But if I uncomment variable ‘a’ and pass it as argument to ‘fun1’ an error occurs. In Matlab it can be done without problems.

[x, funj, cvg, outp] = fmincon(@fun1, xin, [], [], [], [], [], [], @ (x) {[], constraint_function(x)}{:},options,a)

Files fun.m and fun1.m

function f = fun(x)
  f = x(1)^2 + x(2)^2;

function f = fun1(x,a)
  f = a** *(x(1)^2) + x(2)^2;

My system

  • OS: Windows 10 (version 2019)
  • Octave version: Version 6.1.0

Same solution as for fminsearch and alike: use an anonymous function. For more explanation see a previous post:

1 Like

Thanks for the quick reply! I was able to solve my problem with your help.

1 Like