from convexchecker by Bapi Chatterjee
It checks real valued multidimensional function, whether it is convex.

convexchecker.m
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%The script convexchecker derives the gradient and Hessian of a given
%multidimensional real valued function f. It further calculates the values
%of gradient and Hessian at the point x0. Then it checkes whether the given
%function is convex globally by applying Sylvester's criterion of checking
%positive definiteness of the Hessian matrix. Please note that it checks 
%whether the function is globally convex and not at the given point x0,
%which can easily be checked by applying the same method to the calculated 
%Hessian or by calculating the eigenvalues of the Hessian matrix at that 
%point using eig(h). It is a well known result that if all the eigenvalues
%of a matrix are strictly positive then the matrix is positive definite
%and hence the function becomes convex.
%Input:
%n=Dimension of the real valued function f.
%f=The real valued function whose gradient and Hessian are needed.
%  Please ensure that the variables are x1,x2,etc.
%  For example: n=3
%               f=x1^2+sin(x2)-x1*x3.
%x0=The point at which values of gradient and Hessian are needed.
%   Please ensure that dimension of the point=n.
%Output:
%Gradient:The vector of first derivatives of the function w.r.to the
%          variables x1,x2,...,xn.
%Hessian:The Hessian matrix of the function.
%Value of gradient:The value of gradient at the point x0;
%Value of Hessian:The value of Hessian at the point x0;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Developed by Bapi Chatterjee at Indian Institute of Technology Delhi, New Delhi, India.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
disp('It is suggested to clear your workspace.To do so enter 1 any other number to continue without it.');
select=input('');
if select==1
    clc;clear;
end
a=2;mark=0;u=[];warning off all
n=input('Enter the dimension of function:');
for i=1:n
    syms (['x' num2str(i)]); 
end
try
    f=input('Enter the function in terms of variables x_i(e.g. x1,x2,etc.):');
catch
    disp('Kindly ensure that your variables are in the form of x1,x2,etc. and index is less than n!');
end
x0=input('Enter the point at which gradiant and Hessian are needed as row vector of variables:');
while a==2
if length(x0)~=n
    disp('The dimension of point is incorrect!');a=2;
    x0=input('Enter the point at which gradiant and Hessian are needed as row vector of variables:');
else
    a=1;
end
end
g=f;
try
    for i=1:n
        h(i)=diff(g,['x' num2str(i)]);
        for j=1:n
            k(i,j)=diff(h(i),['x' num2str(j)]);
        end
    end
catch
    fprintf('There are variables in your workspace clashing with the ones used here.\n');
    fprintf('So kindly clear it using command "clear" and then rerun the script!\n');
    return
end
for i=1:n
    for j=1:n
        if i==j
            d=det(k(1:i,1:j));
            SOL=solve(d);
            if str2num(char(d))<=0
                mark=mark+1;u=[u i];
            elseif isempty(SOL)==0
               for m=1:length(SOL)
                   if isreal(SOL(m))==1||isa(SOL(m),'sym')
                      mark=mark+1;
                      if (length(find(u==i))==0)
                          u=[u i];
                      end
                   else
                      mark=mark;
                   end                    
               end
            else
                mark=mark;
            end                
        end
    end
end    
disp('Gradient=')
disp(h)
disp('Hessian=')
disp(k)
for i=1:n
    h=subs(h,['x' num2str(i)],x0(i));
    k=subs(k,['x' num2str(i)],x0(i));
end
disp('Value of gradient=')
disp(h)
disp('Value of Hessian=')
disp(k)
if mark>0
    fprintf('The %gth principal minor of Hessian is not positive at all real x!\n',u);
    fprintf('So the function is not convex globally!\n');
else
    fprintf('All the principal minors of Hessian are positive so the function is convex globally!\n',u);
end

Contact us at files@mathworks.com