1; # LS objective function function obj_value = model(args) theta = args{1}; y = args{2}; x = args{3}; a = theta(1,:); b = theta(2,:); c = theta(3,:); d = theta(4,:); errors = y - a*sin(b*x+c)-d; obj_value = errors'*errors; endfunction n = 100; # sample size # generate data x = randn(n,1); noise = randn(n,1); a = 1; b = 1; c = 1; d = 1; y = a*sin(b*x+c)+d + noise; theta = ones(4,1); # start with correct values, since we know them ;>) control = [100; 1; 1]; [theta, obj_value, iterations, convergence] = BFGSMin("model", {theta, y, x}, control);