Not enough input arguments Error in optimization

3 ビュー (過去 30 日間)
Armagan Ozbilge
Armagan Ozbilge 2015 年 3 月 24 日
回答済み: Alan Weiss 2015 年 3 月 25 日
Hello, I am a fresh user and having a trouble with the following code:
data = xlsread('ornek');
for i = 1:47
data(i,1) = i-1;
end
X = data(:,2);
N = length(data);
dt = 1; objfun = @(theta1, theta2) mlfornek(theta1, theta2, X, N, dt);
[theta, f] = fminunc(objfun,[-1;2]) gives error:
Error using @(theta1,theta2)mlfornek(theta1,theta2,X,N,dt)
Not enough input arguments.
Error in fminunc (line 254) f = feval(funfcn{3},x,varargin{:});
Caused by: Failure in initial user-supplied objective function evaluation. FMINUNC cannot continue. where,
function f = mlfornek(theta1, theta2, X, N, dt)
f = 0;
for j = 2:N % constructed MLE as a vector
f = f + .5*log(2*pi*dt*theta2*X(j-1)) + ...
(X(j)-X(j-1)-theta1*X(j-1)*dt)^2/(2*dt*theta2*X(j-1));
end
end
I guess, problem comes from the loop in the function; but I am not sure. If you have any idea please help me. Thanks in advance.

採用された回答

Alan Weiss
Alan Weiss 2015 年 3 月 25 日
You need to write your objective function in the documented syntax for Optimization Toolbox: as a function that takes a SINGLE input variable theta.
function f = mlfornek(theta, X, N, dt)
theta1 = theta(1);
theta2 = theta(2);
f = 0;
for j = 2:N % constructed MLE as a vector
f = f + .5*log(2*pi*dt*theta2*X(j-1)) + ...
(X(j)-X(j-1)-theta1*X(j-1)*dt)^2/(2*dt*theta2*X(j-1));
end
end
Include the objective as:
objfun = @(theta) mlfornek(theta, X, N, dt);
Alan Weiss
MATLAB mathematical toolbox documentation

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeProblem-Based Optimization Setup についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by