From: Verena Feirer on 2 Jun 2010 08:53 Hello! I am currently struggling with the Optimization Toolbox in combination with function handles and was hoping you might be able to help me. I am trying to optimize a function with fminunc and have written a myfunction.m file that takes three inputs (some vectors p, n and k) and returns one value of the function myfunction. (It's some kind of p.d.f. that returns a probability for given parameters p, n and k.) The vectors n and k have fixed/known/real values once I start the optimization. But I want to run various optimizations so I do not want to "hard-code" their values into my myfunction.m file. Instead, I want to supply them at the beginning of the specific optimization and then use a function handle on myfunction.m. Then, I want to optimize the function in a for the fixed values of n and k. So the whole setting looks like p = rand(10,1); % 10 vaules out of (0,1) n = 16*ones(10,1); % a vector of 10 '16'-entries k = binornd(n, p); % a vector of 10 (16,p_i) distributed binomials fh = @(p)myfunction(p, n, k); % for known/defined vectors n and k [new_p, fval, exitflag, output] = fminunc(fh, start_p, options); % for known values of start_p So far, so good. BUT: My problem now is that I want to supply a gradient as well. So I add the gradient to my myfunction.m which now returns two "values" f and g, changing the function to function [f, g] = myfunction(p, n, k) I then tell fminunc it will recieve a gradient as well. And that is where I fail to supply my known values n and k to the function and still persuade it to return both the function itself and its gradient, both in the variable p. When calling options = optimset('Display', 'iter', 'GradObj','on', 'LargeScale', 'on'); fh = @(p)myfunction(p, n, k); [new_p, fval, exitflag, output] = fminunc(fh, start_p, options); Matlab now tells me that ??? Error using ==> uminus Too many output arguments. Error in ==> @(p)myfunction(p,n,k) Error in ==> fminunc at 236 [f,GRAD(:)] = feval(funfcn{3},x,varargin{:}); Caused by: Failure in initial user-supplied objective function evaluation. FMINUNC cannot continue. I am really at a loss here since my knowledge of working with '@'s is limited to what I found in the Optimization Toolbox manual and Matlab's help on 'function_handle'. I hope I have provided all the important information you might need. Any help will be very appreciated. Verena
|
Pages: 1 Prev: MEAN AND FUCTION Next: how to get contours into an image |