writing objective function for error optimization
    6 views (last 30 days)
  
       Show older comments
    
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
Answers (1)
  Mischa Kim
    
      
 on 1 Mar 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 Comments
  Mischa Kim
    
      
 on 2 Mar 2014
				
      Edited: Mischa Kim
    
      
 on 2 Mar 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?
See Also
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!