writing objective function for error optimization

4 Ansichten (letzte 30 Tage)
Navid
Navid am 1 Mär. 2014
Kommentiert: Navid am 2 Mär. 2014
I'm trying to write an objective function like:
function y = parameterfun(xin,sig)
a=trapz(sig);
b=sig;
y= xin(1)*b+xin(2)*a;
xin0=[1,1];
f = @(x)parameterfun(x,sig);
[xin,fval] = fminunc(f,xin0);
sig is a double data type,actually it is an error signal that I want to find out for which parameters xin(1),xin(2) would be minimum. I do not know why i'm getting this:
Error using parameterfun (line 2)
Not enough input arguments.
can anybody help me with that?I would really appreciate.
  2 Kommentare
Matt J
Matt J am 2 Mär. 2014
Bearbeitet: Matt J am 2 Mär. 2014
You seem to have mushed together the objective function with the optimization code that invokes it. Are these lines really part of the objective function, too?
xin0=[1,1];
f = @(x)parameterfun(x,sig);
[xin,fval] = fminunc(f,xin0);
Navid
Navid am 2 Mär. 2014
firstly thanks for your respond,actually I've updated the code with help of @Mischa , but it has still error,can you see that please?

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Mischa Kim
Mischa Kim am 1 Mär. 2014
Navid, I believe, that's what you would like to do:
function test() % setting up optim. problem
x0 = [1,1];
sig0 = 1;
xin0 = [x0 sig0]; % initial solution guess
[xin,fval] = fminunc(@parameterfun,xin0); % execute optimization
end
function y = parameterfun(xin)
xin1 = xin(1);
xin2 = xin(2);
sig = xin(3);
a = trapz(sig);
b = sig;
y = xin1*b + xin2*a;
end
Put the two functions in one function file (e.g., called test.m) and don't forget to assign a value to sig. The way you have set it up you are using parameterfun() recursively. Was this intended?
  3 Kommentare
Mischa Kim
Mischa Kim am 2 Mär. 2014
Bearbeitet: Mischa Kim am 2 Mär. 2014
You could just call the function with sig0 as an input argument
function test(sig0)
What exactly is sig? Can you paste in a comment?
Navid
Navid am 2 Mär. 2014
I did some changes to the code:
function test()
x0 = [1,1];
sig = evalin('base','sig'); %importing vector sig from workspace
xin0 = [x0];
[xin,fval] = fmincon(@parameterfun,xin0,[],[],[],[],[1,1],[100,100]);
end
function y = parameterfun(xin)
sig = evalin('base','sig');
xin1 = xin(1);
xin2 = xin(2);
a = trapz(sig);
b = sig;
y = xin1*b + xin2*a;
end
and now I'm getting this error:
Error using fmincon (line 708)
User supplied objective function must return a scalar value.
Error in test (line 5)
xin = fmincon(@parameterfun,xin0,[],[],[],[],[1,1],[100,100]); %
execute optimization
I've also attached the vector sig to make it more clear. I would appreciate any help.

Melden Sie sich an, um zu kommentieren.

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by