Minimize function with respect to multiple variables

120 views (last 30 days)
Hi,
I have a function f(b1,b2,b3,x,y1,y2,y3) that requires multiple inputs. How can I find the values of b1, b2, and b3 that minimize this function for given values of x, y1, y2, and y3?
Thanks for the help.

Accepted Answer

Jonathan
Jonathan on 10 Nov 2011
You can use the function fminsearch for this, which requires an initial guess. Here is how it might look for you.
x = 1;
y1 = 2;
y2 = 3;
y3 = 4;
fun = @(b) f(b(1), b(2), b(3), x, y1, y2, y3);
b_guess = [10 20 30];
b_min = fminsearch(fun, b_guess);
b1_min = b_min(1);
b2_min = b_min(2);
b2_min = b_min(3);
Let me know if this answers your question.
~Jonathan
  8 Comments
Vignesh
Vignesh on 3 Apr 2019
My function needs three arrays as input and hence the initial value (b_guess) has to be three arrays instead of three values. How do I setup this input for fminsearch? I tried to input b_guess as 3xn array but I get the error 'Not enough input arguments'
Walter Roberson
Walter Roberson on 3 Apr 2019
fminsearch() only accepts a single vector as input. You need to construct a 1 x (3*n) vector as your input. Your code can reshape and extract portions as needed.

Sign in to comment.

More Answers (1)

Mohamad Alsioufi
Mohamad Alsioufi on 9 Dec 2017
Edited: Mohamad Alsioufi on 9 Dec 2017
Hi there, I have the same problem, but when I tried to use 'fminsearch' function I had some problems, the following code is a part of my function myGP(x,y,x_star,y_hat):
segmaSE = 1;
lengthSE = 1;
theta0 =[segmaSE lengthSE];
kernelFunc = @(x1,x2,theta)(theta(1)^2)*exp(-0.5*(pdist2(x1/theta(2), x2/theta(2)).^2));
marginal_likelihood =@(y,x,theta,N) -0.5*y'*pinv(kernelFunc(x,x,theta))*y - 0.5*log(abs(kernelFunc(x,x,theta))) - N*.5 * log (2*pi);
fun = @(theta)marginal_likelihood(y,x,theta,N);
theta0 =[segmaSE lengthSE];
theta=fminsearch(fun,theta0);
However I got the following error:
Assignment has more non-singleton rhs dimensions than non-singleton subscripts
Error in fminsearch (line 200) fv(:,1) = funfcn(x,varargin{:});
Error in myGP (line 21) theta=fminsearch(fun,theta0);
Any idea about this error?
  1 Comment
Walter Roberson
Walter Roberson on 9 Dec 2017
You do not give us any information about the sizes of the variables, which makes it difficult to test.
I notice that you always call kernelFunc() with (x, x, theta). If x is scalar or row vector then the result of the pdist2() call will be 0. If x is N x M for N > 1 then the result of the pdist2() will be N x N. The exp() will not change that, and multiplying by the scalar will not change that.
pinv() of N x N will be N x N . In order for pinv()*y to work, y must be N x P for some P, with the * giving an N x P result. The y' * before that would be * of a P x N, so that would be P x N * N * P, giving a P x P result. You multiply that by -0.5 and you subtract 0.5*log(abs(kernelFunc(x,x,theta))) where we have already determined that the kernelFunc returns an N x N result. P x P minus N x N will work under the circumstance that P is 1 or P is N; either way the subtraction going to return a N x N result .
Now, the objective function return value for fminsearch needs to be a scalar. For N x N to be scalar, then x would have had to have been N x M = 1 x M -- but in that case the pdist2() would return 0 making it essentially useless to make the pdist2() call.
This suggests that your formula is either fundamentally incorrect (but it does not look to be wrong), or else that you are asking for a matrix minimization, which fminsearch() cannot handle.

Sign in to comment.

Categories

Find more on MATLAB in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!