writing objective function for error optimization

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 Comments

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);
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?

Sign in to comment.

Answers (1)

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 Comments

Many thanks for your help,the point is that I have to use the sig as an input argument,because as I mentioned it is a double data type which I'm using itself and also it's integral in order to compute my system's error.
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?
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.

Sign in to comment.

Products

Asked:

on 1 Mar 2014

Commented:

on 2 Mar 2014

Community Treasure Hunt

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

Start Hunting!