MHP
MHP

Reputation: 45

passing two parameters to fminunc function

I use MATLAB optimization toolbox function fminunc to optimize two parameters with different lengths based on my objective function. I need to optimize both parameters. The algorithm needs to start with an initial points for these parameters:

x0 = randn(10,3);
y0 = rand(20,1);

options = optimoptions(@fminunc,'Algorithm','quasi-newton');
[Coeff_min, Error_min] = fminunc(@objectiveFunction, x0, options)

How can I pass both initial parameters to the function?

Upvotes: 2

Views: 492

Answers (1)

Shai
Shai

Reputation: 114786

What you can do is fool matlab to think you only have one parameter of numel 10*3 + 20:

function v = objective_wrapper( xy )
x = reshape(xy(1:30),10,3);
y = reshape(xy(31:end),20,1);
v = objectiveFunction(x,y);

Now you can optimize:

xy0 = [x0(:);y0(:)];
[Coeff_min, Error_min] = fminunc(@objective_wrapper, xy0, options);

Upvotes: 3

Related Questions