function [u_opt,fval,exitflag] = NLP4MATLAB()

clc
clear all
close all

global h;
global f;
global hyCase;
h = actxserver('UnisimDesign.Application');
hyCase = h.Activedocument;
f = hyCase.Flowsheet;

par=[];
lb=zeros(1,6);
ub=ones(1,6);
u0(1) = scaleInputs(f.Operations.Item('Inputs').Cell('D2').CellVariable.Value,lb,ub,1);
u0(2) = scaleInputs(f.Operations.Item('Inputs').Cell('D3').CellVariable.Value,lb,ub,1);
u0(3) = scaleInputs(f.Operations.Item('Inputs').Cell('D4').CellVariable.Value,lb,ub,1);
u0(4) = scaleInputs(f.Operations.Item('Inputs').Cell('D5').CellVariable.Value,lb,ub,1);
u0(5) = scaleInputs(f.Operations.Item('Inputs').Cell('D6').CellVariable.Value,lb,ub,1);
u0(6) = scaleInputs(f.Operations.Item('Inputs').Cell('D7').CellVariable.Value,lb,ub,1);
options = optimset('TolFun',10e-8,'TolCon',1e-4,'Display','iter','Algorithm','interior-point','Diagnostics','on', 'FinDiffType','central','ScaleProblem','obj-and-constr','FinDiffRelStep',1e-2);
tic
[u_opt,fval,exitflag]=fmincon(@(u)objFun(u,par),u0,[],[],[],[],lb,ub,@(u)nonLinConFun(u,par),options);
toc
end

function y = objFun(u,par)
global f;
global hyCase;
hyCase.Solver.CanSolve=0;
lb(1) = f.Operations.Item('Inputs').Cell('B2').CellVariable.Value;
lb(2) = f.Operations.Item('Inputs').Cell('B3').CellVariable.Value;
lb(3) = f.Operations.Item('Inputs').Cell('B4').CellVariable.Value;
lb(4) = f.Operations.Item('Inputs').Cell('B5').CellVariable.Value;
lb(5) = f.Operations.Item('Inputs').Cell('B6').CellVariable.Value;
lb(6) = f.Operations.Item('Inputs').Cell('B7').CellVariable.Value;
ub(1) = f.Operations.Item('Inputs').Cell('C2').CellVariable.Value;
ub(2) = f.Operations.Item('Inputs').Cell('C3').CellVariable.Value;
ub(3) = f.Operations.Item('Inputs').Cell('C4').CellVariable.Value;
ub(4) = f.Operations.Item('Inputs').Cell('C5').CellVariable.Value;
ub(5) = f.Operations.Item('Inputs').Cell('C6').CellVariable.Value;
ub(6) = f.Operations.Item('Inputs').Cell('C7').CellVariable.Value;
input2Units = f.Operations.Item('Inputs').Cell('E2').CellText;
input3Units = f.Operations.Item('Inputs').Cell('E3').CellText;
input4Units = f.Operations.Item('Inputs').Cell('E4').CellText;
input5Units = f.Operations.Item('Inputs').Cell('E5').CellText;
input6Units = f.Operations.Item('Inputs').Cell('E6').CellText;
input7Units = f.Operations.Item('Inputs').Cell('E7').CellText;
f.Operations.Item('Inputs').Cell('A2').CellVariable.SetValue(deScaleInputs(u,lb,ub,1),input2Units);
f.Operations.Item('Inputs').Cell('A3').CellVariable.SetValue(deScaleInputs(u,lb,ub,2),input3Units);
f.Operations.Item('Inputs').Cell('A4').CellVariable.SetValue(deScaleInputs(u,lb,ub,3),input4Units);
f.Operations.Item('Inputs').Cell('A5').CellVariable.SetValue(deScaleInputs(u,lb,ub,4),input5Units);
f.Operations.Item('Inputs').Cell('A6').CellVariable.SetValue(deScaleInputs(u,lb,ub,5),input6Units);
f.Operations.Item('Inputs').Cell('A7').CellVariable.SetValue(deScaleInputs(u,lb,ub,6),input7Units);
hyCase.Solver.CanSolve=1;
y = f.Operations.Item('Objective').Cell('A2').CellValue;
end

function [c,ceq] = nonLinConFun(u,par)
global f;
global hyCase;
hyCase.Solver.CanSolve=0;
lb(1) = f.Operations.Item('Inputs').Cell('B2').CellVariable.Value;
lb(2) = f.Operations.Item('Inputs').Cell('B3').CellVariable.Value;
lb(3) = f.Operations.Item('Inputs').Cell('B4').CellVariable.Value;
lb(4) = f.Operations.Item('Inputs').Cell('B5').CellVariable.Value;
lb(5) = f.Operations.Item('Inputs').Cell('B6').CellVariable.Value;
lb(6) = f.Operations.Item('Inputs').Cell('B7').CellVariable.Value;
ub(1) = f.Operations.Item('Inputs').Cell('C2').CellVariable.Value;
ub(2) = f.Operations.Item('Inputs').Cell('C3').CellVariable.Value;
ub(3) = f.Operations.Item('Inputs').Cell('C4').CellVariable.Value;
ub(4) = f.Operations.Item('Inputs').Cell('C5').CellVariable.Value;
ub(5) = f.Operations.Item('Inputs').Cell('C6').CellVariable.Value;
ub(6) = f.Operations.Item('Inputs').Cell('C7').CellVariable.Value;
input2Units = f.Operations.Item('Inputs').Cell('E2').CellText;
input3Units = f.Operations.Item('Inputs').Cell('E3').CellText;
input4Units = f.Operations.Item('Inputs').Cell('E4').CellText;
input5Units = f.Operations.Item('Inputs').Cell('E5').CellText;
input6Units = f.Operations.Item('Inputs').Cell('E6').CellText;
input7Units = f.Operations.Item('Inputs').Cell('E7').CellText;
f.Operations.Item('Inputs').Cell('A2').CellVariable.SetValue(deScaleInputs(u,lb,ub,1),input2Units);
f.Operations.Item('Inputs').Cell('A3').CellVariable.SetValue(deScaleInputs(u,lb,ub,2),input3Units);
f.Operations.Item('Inputs').Cell('A4').CellVariable.SetValue(deScaleInputs(u,lb,ub,3),input4Units);
f.Operations.Item('Inputs').Cell('A5').CellVariable.SetValue(deScaleInputs(u,lb,ub,4),input5Units);
f.Operations.Item('Inputs').Cell('A6').CellVariable.SetValue(deScaleInputs(u,lb,ub,5),input6Units);
f.Operations.Item('Inputs').Cell('A7').CellVariable.SetValue(deScaleInputs(u,lb,ub,6),input7Units);
hyCase.Solver.CanSolve=1;
c(2)=f.Operations.Item('Constraints').Cell('A2').CellValue;
c(3)=f.Operations.Item('Constraints').Cell('A3').CellValue;
c(4)=f.Operations.Item('Constraints').Cell('A4').CellValue;
c(5)=f.Operations.Item('Constraints').Cell('A5').CellValue;
c(6)=f.Operations.Item('Constraints').Cell('A6').CellValue;
c(7)=f.Operations.Item('Constraints').Cell('A7').CellValue;
c(8)=f.Operations.Item('Constraints').Cell('A8').CellValue;
ceq(2)=f.Operations.Item('Constraints').Cell('B2').CellValue;
end

function y = scaleInputs(u,lb,ub,index)
y=(u(index)-lb(index))/(ub(index)-lb(index));
end

function y = deScaleInputs(u,lb,ub,index)
y=lb(index)+u(index)*(ub(index)-lb(index));
end

