writing objective function for error optimization

4 ビュー (過去 30 日間)
Navid
Navid 2014 年 3 月 1 日
コメント済み: Navid 2014 年 3 月 2 日
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 件のコメント
Matt J
Matt J 2014 年 3 月 2 日
編集済み: Matt J 2014 年 3 月 2 日
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 2014 年 3 月 2 日
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?

サインインしてコメントする。

回答 (1 件)

Mischa Kim
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
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?
Navid
Navid 2014 年 3 月 2 日
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.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeControl Design in Simulink についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by