%% Simulate Model of Glucose-Insulin Response with Different Initial Conditions
% This example shows how to simulate the glucose-insulin responses for the normal and diabetic subjects.
% Load the model of glucose-insulin response. For details about the model, see the Background section in Simulating the Glucose-Insulin Response.

sbioloadproject('insulindemo', 'm1')

%
% The model contains different initial conditions stored in various variants.

variants = getvariant(m1);

%
% Get the initial conditions for the type 2 diabetic patient.

type2 = variants(1)

%
% Suppress an informational warning that is issued during simulations.

warnSettings = warning('off','SimBiology:DimAnalysisNotDone_MatlabFcn_Dimensionless');

% Create SimFunction objects to simulate the glucose-insulin response for the normal and diabetic subjects.
% Specify an empty array {} for the second input argument to denote that the model will be simulated using the base parameter values (that is, no parameter scanning will be performed).
% Specify the plasma glucose and insulin concentrations as responses (outputs of the function to be plotted).
% Specify the species Dose as the dosed species. This species represents the initial concentration of glucose at the start of the simulation.
%

normSim = createSimFunction(m1,{},...
             {'[Plasma Glu Conc]','[Plasma Ins Conc]'},'Dose')

%
% For the diabetic patient, specify the initial conditions using the variant type2.

diabSim = createSimFunction(m1,{},...
             {'[Plasma Glu Conc]','[Plasma Ins Conc]'},'Dose',type2)

%
% Select a dose that represents a single meal of 78 grams of glucose at the start of the simulation.

singleMeal = sbioselect(m1,'Name','Single Meal');

%
% Convert the dosing information to the table format.

mealTable  = getTable(singleMeal);

%
% Simulate the glucose-insulin response for a normal subject for 24 hours.

sbioplot(normSim([],24,mealTable));

%
% Simulate the glucose-insulin response for a diabetic subject for 24 hours.

sbioplot(diabSim([],24,mealTable));

%
%% Perform a scan using variants
% Suppose you want to perform a parameter scan using an array of variants that contain different initial conditions for different insulin impairments. For example, the model m1 has variants that correspond to the low insulin sensitivity and high insulin sensitivity. You can simulate the model for both conditions via a single call to the SimFunction object.
% Select the variants to scan.

varToScan = sbioselect(m1,'Name',...
                    {'Low insulin sensitivity','High insulin sensitivity'});

%
% Check which model parameters are being stored in each variant.

varToScan(1)

%

varToScan(2)

% Both variants store alternate values for Vmx and kp3 parameters. You need to specify them as input parameters when you create a SimFunction object.
%
% Create a SimFunction object to scan the variants.

variantScan = createSimFunction(m1,{'Vmx','kp3'},...
          {'[Plasma Glu Conc]','[Plasma Ins Conc]'},'Dose');

%
% Simulate the model and plot the results. Run 1 include simulation results for the low insulin sensitivity and Run 2 for the high insulin sensitivity.

sbioplot(variantScan(varToScan,24,mealTable));

% Low insulin sensitivity lead to increased and prolonged plasma glucose concentration.
%
% Restore warning settings.

warning(warnSettings);

% Copyright 2012 The MathWorks, Inc.