Our simple problem is:

subject to:

where
is a vector of length J and for all i : 0 <
< 1
This file actually runs Tomlab and computes the solution. We need additional Matlab files to define the objective function and the constraint function.
These extra files are: 'ofunc.m', 'ograd.m', and 'consfunc.m' and 'consjac.m'
J = 200; theta_guess = ones(J,1); % Define a lower bound very close to 0, but positive so we don't do log(0) theta_lbound = zeros(J,1) + 1e-8; theta_ubound = ones(J,1); % Define the problem in Tomlab. % This is a complex function, so refer closely to the 'conAssign' % documentation when writing your code. % prob = conAssign('ofunc', ... % Name of function to optimize 'ograd', ... % Name of function for gradient [], ... % Leave empty to let Tomlab compute Hessian [], ... % and Hessian pattern theta_lbound, ... theta_ubound, ... % Simple bounds on parameters 'Demo Cons', ... % Problem name theta_guess, ... [], [], ... % Unused parameters, leave empty [],[],[], ... % Linear constraints, which we don't have, so ... % leave empty 'consfunc',... % Name of our constraint function 'consjac', ... % Constraint Jacobian [],... % We have not implemented the Hessian of our [],... % constraints, so leave this empty 1, 1 ... % Require constraint == 1 ... % (We set the lower and upper constraint ... % bounds both to 1 to indicate equality ... % constraints) ); % Initialize MAD - the Matlab Automatic Differentiation % toolbox (from Tomlab) to compute derivatives for us madinitglobals; % Tell MAD to compute the Hessian of the objective function and the % constraints. Note that we still need to implement the gradient. prob.ADObj = -1; prob.ADCons = -1; % Actually run the Tomlab solver and print the results res = tomRun('knitro', ... % Use the KNITRO nonlinear solver prob, ... 2 ... % Print some information (change to 0 to print less) ); disp('Computed solution:'); disp(res.x_k); fprintf('Correct answer is: %g\n', 1 / sqrt(J) );