Combining two optimization functions

3 Ansichten (letzte 30 Tage)
Paulo
Paulo am 4 Jan. 2016
Kommentiert: Brendan Hamm am 4 Jan. 2016
Hi,
I'm trying to combine two optimization functions: fminbnd and fminsearch. With fminbnd I want to minimize the result of one function for one variable. With fminsearch I want to minimize the same function, but for two variables. The problem I'm getting is: 'Not enough input arguments'. I think it has to do with the fact I use repmat in one function. The functions are shown below:
x=fminbnd(@(alpha) gaussian(x0,y0,alpha,xpositie,ypositie,zpositie),0,2*pi);
function [ x,fval ] = gaussian( x0,y0,alpha,xposition,yposition,zposition )
[x,fval]=fminsearch(@Objective, [x0 y0]);
Objective
function result = Objective(x0,y0)
cs = cos(alpha)^2;
sn = sin(alpha)^2;
sc = sin(2*alpha);
x0 = repmat(x0,size(xposition),1);
y0 = repmat(y0,size(xposition),1); %'Not enough input arguments' at this line
a1 = cs*(xposition-x0).^2 +(xposition-x0).*(yposition-y0)*sc...
+ sn.*(yposition-y0).^2;
a2 = sn*(xposition-x0).^2 +(xposition-x0).*(yposition-y0)*sc...
+ cs.*(yposition-y0).^2;
A = [-a1 -a2];
A(:,3)=ones(size(a1,1),1);
B = log(zposition);
xgauss=A\B;
result=norm(A*xgauss-B);
end
end
I'm aware that I can use fminsearch to minimize the function 'gaussian' for three variable, but I'm afraid that it wouldn't give accurate results (because it's a local minimizer). Is there a solution for using two tools or is there a better optimization tool that might solve this problem ?
With kind regards

Antworten (2)

Alan Weiss
Alan Weiss am 4 Jan. 2016
Without reading your code in detail, I can tell you that the syntax error is because you have defined Objective as a function of two variables, x0 and y0. Then you try to call fminsearch on the handle @Objective. The problem there is that fminsearch expects to address a function of one variable alone. Either change Objective to be a function of the vector x, where x = [x0,y0], or in some other way change things so fminsearch has a single vector of variables.
Alan Weiss
MATLAB mathematical toolbox documentation
  2 Kommentare
Paulo
Paulo am 4 Jan. 2016
I've changed the code to: [x,fval]=fminsearch(@(x) Objective(x), [x0 y0]); but I still get the same error. Although, thank you for your answer !
Brendan Hamm
Brendan Hamm am 4 Jan. 2016
Changing that line of code does not change how Objective is defined in the line:
function result = Objective(x0,y0)
which has 2 inputs.

Melden Sie sich an, um zu kommentieren.


Brendan Hamm
Brendan Hamm am 4 Jan. 2016
Your problem is simply that fminsearch is passing only 1 input to the objective function (this is common among optimization routines) and thus y0 is never passed to the function. What you need to do is modify the Objective function to take only one input: a vector of design variables. So assuming x0 and y0 are each scalar valued, it should be something like:
function result = Objective(designs)
x0 = designs(1);
y0 = designs(2);
cs = cos(alpha)^2;
sn = sin(alpha)^2;
sc = sin(2*alpha);
x0 = repmat(x0,size(xposition),1);
y0 = repmat(y0,size(xposition),1);
a1 = cs*(xposition-x0).^2 +(xposition-x0).*(yposition-y0)*sc...
+ sn.*(yposition-y0).^2;
a2 = sn*(xposition-x0).^2 +(xposition-x0).*(yposition-y0)*sc...
+ cs.*(yposition-y0).^2;
A = [-a1 -a2];
A(:,3)=ones(size(a1,1),1);
B = log(zposition);
xgauss=A\B;
result=norm(A*xgauss-B);
end
I would also suggest to not use a nested function (make this a local function ) and pass the extra parameters in using an anonymous function handle:
function [ x,fval ] = gaussian( x0,y0,alpha,xposition,yposition,zposition )
myObj = @(designs) Objective(designs,alpha,xposition,yposition,zposition);
[x,fval]=fminsearch(myObj, [x0 y0]);
end
function result = Objective(designs,alpha,xposition,yposition,zposition)
x0 = designs(1);
y0 = designs(2);
cs = cos(alpha)^2;
sn = sin(alpha)^2;
sc = sin(2*alpha);
x0 = repmat(x0,size(xposition),1);
y0 = repmat(y0,size(xposition),1);
a1 = cs*(xposition-x0).^2 +(xposition-x0).*(yposition-y0)*sc...
+ sn.*(yposition-y0).^2;
a2 = sn*(xposition-x0).^2 +(xposition-x0).*(yposition-y0)*sc...
+ cs.*(yposition-y0).^2;
A = [-a1 -a2];
A(:,3)=ones(size(a1,1),1);
B = log(zposition);
xgauss=A\B;
result=norm(A*xgauss-B);
end
Now for a shameless plug of our Optimization Training offerings: Optimization Techniques in MATLAB

Kategorien

Mehr zu Parameter Estimation finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by