0
votes

I am trying to minimize a 5 variable function with fminsearch. I only want to minimize the function for two variables. I have tried the following, without luck:

func = @(x,b) myfunction( x, y, z, a, b ); 
fminsearch(func,[x0,b0]);

x is a matrix of NxM dimensions, and b with YxZ dimensions, so different dimensions. Same for the starting conditions x0 and b0.

I have seen some similar questions asked, but still I cant solve this problem.

I get the following output when running the script:

Error using horzcat
Dimensions of matrices being concatenated are not consistent.
1

1 Answers

2
votes

Usually the function fminsearch only allows three inputs: the function handle, the initial values vector and the options for the optimization, something like: fminsearch(@fun,x0,options)

Fortunatelly, there's a small hack that can be done, you can put the extra parameters after the options, like this: fminsearch(@fun,[x0 b0],options,z,a,b).

If you're not using any options, it should be like this: fminsearch(@fun,[x0 b0],[],z,a,b).

Remember that inside the function you should unpack your variables a and b, something like:

function[obj]=func(x0,z,a,b)

x=x0(1)
y=x0(2)

%rest of the function

end