Tips & Tricks: Getting started using optimization with MATLAB
by
Stuart Kozola
27 Aug 2008
(Updated
27 Aug 2008)
Demo files from the August 21, 2008 Webinar

peaksCon(x)

function [c,ceq] = peaksCon(x)
%PEAKSCON Constraint function for optimization with PEAKSOBJ
% PEAKSCON(X) is the constraint function for use with PEAKSOBJ. X is of
% size M x 2 or 2 x N.
%
% Sytnax
% [c,ceq] = peaksCon(x)
%
% See also peaksobj, peaks
% Check x size to pass data correctly to constraint definition
[m,n] = size(x);
if (m*n) < 2
error('peaksObj:inputMissing','Not enough inputs');
elseif (m*n) > 2 && (min(m,n) == 1)  (min(m,n) > 2)
error('peaksObj:inputError','Input must have dimension m x 2');
elseif n ~= 2
x = x';
end
% Set plot function to plot constraint boundary
try
mypref = 'peaksNonlinearPlot';
if ~ispref(mypref)
addpref(mypref,'doplot',true);
else
setpref(mypref,'doplot',true);
end
catch
end
% Define nonlinear equality constraint
ceq = [];
% Define nonlinear inequality constraint
% x1^2 + x^2 <= 3^2
c = x(:,1).^2 + x(:,2).^2  9;
% fmincon accepted input form is ceq <= 0


Contact us