% ===============================================================
% == Global Optimal Linearization of the nonlinear             ==
% == dynamics of Mode 3                                        ==
% ==                                                           ==
% ==   Supplement of the work:                                 ==
% ==                                                           ==
% ==     From Cardiac Cells to Genetic Regulatory Networks     ==
% ==                                                           ==
% ==   Description:                                            ==
% ==     Compute the linear interpolation for the input        ==
% ==     curve x,y. Output the coefficients error.             ==  
% ==                                                           ==
% ==   Output:                                                 ==
% ==         xt:    x intervals                                ==
% ==         a,b:   segment coefficients                       ==
% ==                                                           ==
% ==   Authors:                                                ==
% ==                                                           ==
% ==     R. Grosu, E. Bartocci                                 ==
% ==                                                           == 
% ==   Date:  11/05/10                                         ==
% ==                                                           ==
% ==   Free distribution with authors permission               ==
% ==                                                           ==
% ==   SUNY Stony Brook, Stony Brook, NY                       ==
% ==                                                           == 
% ===============================================================  
clear all;
EPI_TVP    =    1.4506; 
EPI_TV1M   =   60.;    
EPI_TV2M   = 1150.; 
EPI_TWP    =  200.0;   
EPI_TW1M   =   60.0;    
EPI_TW2M   =   15.;    
EPI_TS1    =    2.7342; 
EPI_TS2    =    6.;   
EPI_TFI    =    0.11;    
EPI_TO1    =  400.;   
EPI_TO2    =    6.;  
EPI_TSO1   =   30.0181; 
EPI_TSO2   =    0.9957;  
EPI_TSI    =    1.8875; 
EPI_TWINF  =    0.07;
EPI_THV    =    0.3;    
EPI_THVM   =    0.006;   
EPI_THVINF =    0.006;
EPI_THW    =    0.13;
EPI_THWINF =    0.006;  
EPI_THSO   =    0.13; 
EPI_THSI   =    0.13;  
EPI_THO    =    0.006;  
EPI_KWM    =   65.;     
EPI_KS     =    2.0994;  
EPI_KSO    =    2.0458; 
EPI_UWM    =    0.03;    
EPI_US     =    0.9087;  
EPI_UO     =    0.;     
EPI_UU     =    1.55;  
EPI_USO    =    0.65;  
EPI_SC     =    0.007;

EPI_WINFSTAR = 0.94;   % The same of Flavio's paper

EPI_TW2M_TW1M = -45.0;
EPI_TSO2_TSO1 = -29.0224;

EPI_TW2M_TW1M_DIVBY2 = -22.5;
EPI_TSO2_TSO1_DIVBY2 = -14.5112;


%% Range from 0.13 and 0.3
x = linspace(0.13, 0.3, 1000);


ds = ((1.+tanh(EPI_KS.*(x-EPI_US)))./2.);
jso = 1./(EPI_TSO1 + (EPI_TSO2_TSO1_DIVBY2).*(1.+tanh(EPI_KSO .*(x-EPI_USO))));


plot (x, [ds; jso]);

ns = 4;
[e,arange1,brange1,xrange1] = optimalLinearApproximation(x,[ds; jso],ns,1);
 
a(1,1:ns) = arange1(1,ns,:);
a(2,1:ns) = arange1(2,ns,:);

b(1,1:ns) = brange1(1,ns,:);
b(2,1:ns) = brange1(2,ns,:);

xt = xrange1(ns, :);
