%% clear workspace
close all;
clear all;
clc;
%% set global variables (therefore they must not be handed over fom function to function)
global p0;
global tspan;
global init_conc;

%% set initial concentrations
% Initial Concentrations
Initial_Concentration_vector(1) = 100000 ;    % H_wf hair with lipids 
Initial_Concentration_vector(2) = 0 ;       % H_wof hair without lipids
Initial_Concentration_vector(3) = 0 ;       % H_s hair degraded with esterase
Initial_Concentration_vector(4) = 0 ;       % R hair degraded with keratinase
Initial_Concentration_vector(5) = 0 ;       % 1000-R old try for the cost_function
init_conc=Initial_Concentration_vector;

tspan = [0 12]; 
%% Enzyme concentrations (M)
% these are the parameters we want to optimise
K_l = 0.033; % concentraton of lipases
K_e = 0.033; % concentration of esterases
K_k = 0.033; % concentration of keratinases

p0 = [K_l,K_e,K_k]; % vector that contains the parameters to be optimised

%% optimise
% use fmincon for paramter optimisation
% Fmincon has the following algorithms
% 'interior-point' (default)
% 'trust-region-reflective'
% 'sqp'
% 'sqp-legacy'
% 'active-set'
% % set the options for fmincon
lb = [0, 0, 0]; % lower bound 0mM 
% -> constraint, there cannot be a negative concentration
ub = [0.1, 0.1, 0.1]; % upper bound = 100mM 
% this value is arbiraryly set an depends on the amount of enzyme that can be in the solution
A = [];
b = [];
Aeq = [];
beq = [];
% the above values are neded for the function, but not used. Therefore they
% are set emty
options = optimoptions(@fmincon,'Algorithm','interior-point','Display','off');
[Parameter_mod] = fmincon(@cost_function,p0,A,b,Aeq,beq,lb,ub);
% Parameter_mod is a vector with the optimsed parameters 
save(Parameter_mod.mat)
% saves the optimised parameters 