writing objective function for error optimization
4 次查看(过去 30 天)
显示 更早的评论
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 个评论
回答(1 个)
Mischa Kim
2014-3-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 个评论
Mischa Kim
2014-3-2
编辑:Mischa Kim
2014-3-2
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?
另请参阅
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!