clc
clear all
close all
%Grabbing Hysys object
    global hy,global hycase, global f
    hy=actxserver('Hysys.Application'); %Hysys handle w. actx
    hycase=hy.Activedocument;
    f=hycase.Flowsheet; %Grouping flowsheet in HYSYS-model
%Initial inputs, u0 is vector containing initial guess
    isprd=hyspread(hy,'Input');
%u0=[Comp.speed, HEXC128_Flow HEX C127 SpeedC160]
    u0=hyvalue(hycell(isprd,{'D2','D3','D4','D5'}));
%Define bounds for input (for MATLAB optimisation)
    global lb, global ub
    %Lower bounds
    lb=hyvalue(hycell(isprd,{'E2','E3','E4','E5'}));
    %Upper bounds
    ub=hyvalue(hycell(isprd,{'F2','F3','F4','F5'}));
%Nonlinear optimiziation w. fmincon
    %Scale inputs to bounds
    u0s=uscale(u0,lb,ub);
    %Input bounds for optimization
    lbm=zeros(size(lb));
    ubm=ones(size(ub));
    opt=optimset('TolFun',9e-8,...
             	 'TolCon',1e-4,...
                 'Display','iter',...
                 'Algorithm','interior-point',...
                 'Diagnostics','on',...
                 'FinDiffType','central',...
                 'ScaleProblem','obj-and-constr',...
                 'FinDiffRelStep',1e-2);
   tic
   [x,fval,exitflag,output,lambda] =fmincon(@(u)objfunct(u),u0s,[],[],[],...
                                        [],lbm,ubm,@(u)constraints(u),opt);
    elapsed_time=toc; 
         
    
    
    