0

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.
Elias S.
  • 221
  • 1
  • 3
  • 10

1 Answers1

2

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
  • Thanks for the answer! I dit not quite get the last part of unpacking the variables. – Elias S. Apr 16 '17 at 02:22
  • I will. I tried your solution, with no luck. Since the matrices x and b are of different sizes, i stored them in a cell. Like this; c1{1}=x and c1{2}=b. Then i ran the following script; func = @(c1) myfunction( c1, y, z, a ); fminsearch(func,[c1{1} c1{2}]);. I still get the same error message like before. – Elias S. Apr 16 '17 at 14:50
  • Let me see if I understand, are you trying to minimize the objetive function by using two matrix as variable? – Alessandro Trigilio Apr 16 '17 at 16:44
  • Yes I was. However, I figured it out. – Elias S. Apr 17 '17 at 19:51
  • Sure, I put together the matrixes to a single one, and attached it to a single variable. This solved my problem. – Elias S. Apr 18 '17 at 02:42