%%%% solve ODE model to get output time profile % Modified version (by Peter Halling) of Hui's program % This version tries to omit calculation of sensitivities, and focus just % on progress of state variables. % Also calls ODE_ProchiralDiol.m %clear all clear variables %following help guidance close all clc format long % Input values here may be changed showplot = false; S0 = 0.2; % M units Keq = 15; % Also M units Evalue = 39; % Next value here is with concn units, then first order const % with s-1, then rest are dimensionless KM = 0.01; % M units k4 = 1000; SCRrat = 5; k24rat = 1; k2rat = 1; k4rat = 0.2; %km34rat = 1; k34rat = 1; k13Rrat = 1; k13Srat = 1; if k4 < 0 dummy = DoOneRun (S0, Keq, Evalue, KM, k4, SCRrat, k24rat, k2rat, ... k4rat, k34rat, k13Rrat, k13Srat, false); % For test S0=0.001 dummy = DoOneRun (S0, Keq, Evalue, KM, k4, SCRrat, k24rat, k2rat, ... k4rat, k34rat, k13Rrat, k13Srat, false); % For test S0=0.2 KM=10 dummy = DoOneRun (S0, Keq, Evalue, KM, k4, SCRrat, k24rat, k2rat, ... k4rat, k34rat, k13Rrat, k13Srat, false); % For test end if k4 < 0 for prod = [0.005] % 0.01 0.02]% 0.05 0.1 0.2 0.5 1 2] %for KM = [0.001 0.002 0.005 0.01 0.02 0.05 0.1 0.2 0.5 1 2 5 10] %k4rat = 10*KM; for SCRrat = [0.5] % 5 50] KM = prod/SCRrat; for k4rat = [5] %[0.05 5] dummy = DoOneRun (S0, Keq, Evalue, KM, k4, SCRrat, k24rat, k2rat, ... k4rat, k34rat, k13Rrat, k13Srat, false); end end end KM = 0.01 SCRrat = 5 end if k4 < 0 for S0 = [0.02 0.05 0.1 0.2 0.5 1] for KM = [0.001 0.002 0.005 0.01 0.02 0.05 0.1 0.2 0.5 1 2 5 10] dummy = DoOneRun (S0, Keq, Evalue, KM, k4, SCRrat, k24rat, k2rat, ... k4rat, k34rat, k13Rrat, k13Srat, false); end end S0 = 0.2 KM = 0.01 end if k4 < 0 % never for S0 = [0.05 0.1 0.2 0.4 1] dummy = DoOneRun (S0, Keq, Evalue, KM, k4, SCRrat, k24rat, k2rat, ... k4rat, k34rat, k13Rrat, k13Srat, false); end S0 = 0.2 %end for Keq = [1 2 4 7.5 15 30 100] k4rat = 3/Keq; dummy = DoOneRun (S0, Keq, Evalue, KM, k4, SCRrat, k24rat, k2rat, ... k4rat, k34rat, k13Rrat, k13Srat, false); end Keq = 15 k4rat = 0.2 end if k4 < 0 % never for Evalue = [9 19 39 100] dummy = DoOneRun (S0, Keq, Evalue, KM, k4, SCRrat, k24rat, k2rat, ... k4rat, k34rat, k13Rrat, k13Srat, false); end Evalue = 39 %end %if k4 < 0 for KM = [0.001 0.005 0.01 0.02 0.1 0.2 0.4 1 10] dummy = DoOneRun (S0, Keq, Evalue, KM, k4, SCRrat, k24rat, k2rat, ... k4rat, k34rat, k13Rrat, k13Srat, false); end KM = 0.01 %end for k4 = [500 1000 2000] dummy = DoOneRun (S0, Keq, Evalue, KM, k4, SCRrat, k24rat, k2rat, ... k4rat, k34rat, k13Rrat, k13Srat, false); end k4 = 1000 for SCRrat = [1 2.5 5 10] dummy = DoOneRun (S0, Keq, Evalue, KM, k4, SCRrat, k24rat, k2rat, ... k4rat, k34rat, k13Rrat, k13Srat, false); end SCRrat = 5 for k24rat = [0.5 1 2] dummy = DoOneRun (S0, Keq, Evalue, KM, k4, SCRrat, k24rat, k2rat, ... k4rat, k34rat, k13Rrat, k13Srat, false); end k24rat = 1 for k2rat = [0.5 1 2] dummy = DoOneRun (S0, Keq, Evalue, KM, k4, SCRrat, k24rat, k2rat, ... k4rat, k34rat, k13Rrat, k13Srat, false); end k2rat = 1 for k4rat = [0.1 0.2 0.4 1] dummy = DoOneRun (S0, Keq, Evalue, KM, k4, SCRrat, k24rat, k2rat, ... k4rat, k34rat, k13Rrat, k13Srat, false); end k4rat = 0.2 %if k14rat < 0 for k34rat = [0.5 1 2] dummy = DoOneRun (S0, Keq, Evalue, KM, k4, SCRrat, k24rat, k2rat, ... k4rat, k34rat, k13Rrat, k13Srat, false); end k34rat = 1 %end %if k34Srat < 0 % i.e. never for k13Rrat = [0.5 1 2] dummy = DoOneRun (S0, Keq, Evalue, KM, k4, SCRrat, k24rat, k2rat, ... k4rat, k34rat, k13Rrat, k13Srat, false); end k13Rrat = 1 for k13Srat = [0.5 1 2] dummy = DoOneRun (S0, Keq, Evalue, KM, k4, SCRrat, k24rat, k2rat, ... k4rat, k34rat, k13Rrat, k13Srat, false); end k13Srat = 1 end if k4<0 for Keq = [1 2 5 10 20] for S0 = [0.05 0.1 0.2 0.5 1 2] dummy = DoOneRun (S0, Keq, Evalue, KM, k4, SCRrat, k24rat, k2rat, ... k4rat, k34rat, k13Rrat, k13Srat, false); end end KMD = 0.001 KMS = 0.001 %end k4=100000 SCRrat = 0.1 for KMS = [0.0005 0.001] dummy = DoOneRun (S0, Keq, Evalue, KM, k4, SCRrat, k24rat, k2rat, ... k4rat, k34rat, k13Rrat, k13Srat, false); end %end for SCRrat = [0.5 1 2 5 10 20] %[0.1 0.2 0.5 1 2 5 10] for k4rat = [0.05 0.1 0.2 0.5 1 2 5] %[0.0005 0.001 0.002 0.005 0.01 0.02 0.05 0.1 0.2 0.5 1 2 5] dummy = DoOneRun (S0, Keq, Evalue, KM, k4, SCRrat, k24rat, k2rat, ... k4rat, k34rat, k13Rrat, k13Srat, false); end end SCRrat = 5 k4rat = 0.2 end for KM = [0.2 0.1 0.05 0.02 0.01 0.005 0.002 0.001]; for k4rat = [0.05 0.1 0.2 0.5 1 2 5] %[0.0005 0.001 0.002 0.005 0.01 0.02 0.05 0.1 0.2 0.5 1 2 5] dummy = DoOneRun (S0, Keq, Evalue, KM, k4, SCRrat, k24rat, k2rat, ... k4rat, k34rat, k13Rrat, k13Srat, false); end end KM = 0.01 k4rat = 0.2 function dummy = DoOneRun (S0, Keq, Evalue, KM, k4, SCRrat, k24rat, ... k2rat, k4rat, k34rat, k13Rrat, k13Srat, showplot) initrate = 0.005; %sets time scale, used to calculate EobySo % Now calculate rate constants, at first all on dimensioned basis k2R = k24rat*k4; k2S = k2R/(k2rat*Evalue); k3w = k4*k34rat; km4 = k4rat*k4/Keq; km3 = SCRrat*k34rat*k4/k4rat; SCRf = 1/(KM*((1 + 1/Evalue)*(1/k3w + km3/(k3w*k4) + 1/k4) + 1/k2R + 1/(Evalue*k2S))); SCSf = SCRf/Evalue; SCRb = SCRf/SCRrat; SCSb = SCRb/Evalue; km1R = k13Rrat*km3; k1R = SCRf*(k2R + km1R)/k2R; km2R = SCRb*(k2R + km1R)/km1R; km1S = k13Srat*km3/Evalue; k1S = SCSf*(k2S + km1S)/k2S; km2S = SCSb*(k2S + km1S)/km1S; term1 =(1+1/Evalue)*(k4+km3+k3w)/(k3w*k4); term2 = 1/k2R + 1/(Evalue*k2S)+1/(SCRf*S0); EobySo = initrate*(term1 + term2); if EobySo > 0.01 toobig = true; %error ('EobySo > 0.01') else toobig = false; end % Remove dimensions k1Rn = S0*k1R; k1Sn = S0*k1S; km2Rn = S0*km2R; km2Sn = S0*km2S; km4n = S0*km4; Sn0 = 1; % By definition of non-dimensional basis En0 = 1; % Same % All other state variables start at 0 % Set up the values of state variables and simulation parameters % initial condition of state variables Xinit = [En0 0 0 0 0 Sn0 0 0 0]'; % nominal parameter values k = [k1Rn k2R k1Sn k2S k3w k4 km1R km2Rn km1S km2Sn km3 km4n]'; % set up the simulation time and sampling interval %tspan = 0:0.001:0.1; % start with transient %tspan = 0:0.4:20; % initial rate period %tspan = 0:20:1000; % further progress tspan = 0:100:5000; % for slow reactions if ~toobig % With normalised non-dimensional values for all variables and parameters, % probably similar AbsTol for all, so try keeping default 1e-6. AbsTol_value = 1e-6; options = odeset('AbsTol',AbsTol_value); % Run the ODE simulation %[t, x_s] = feval(@ode15s, @ODE_Prochiral, tspan, xs_init, options, k, EobySo); % This doesn't work any more when have extra parameter EobySo, but managed % to avoid feval call completely after viewing ode15s help %[t,y] = ode15s(@(t,y) odefcn(t,y,A,B), tspan, y0); Example from help [t, xout] = ode15s(@(t,x) ODE_ProchiralDiol(t,x,k,EobySo), tspan, Xinit, options); % Help gives "[t,y] = ode15s(odefun,tspan,y0). Each row in the solution array %y corresponds to a value returned in column vector t." %disp(xout) %Yes, xout has a column for each state variable, and a row for each time, so x = transpose(xout); % x has one row for each state variable, one col for each time PRn = x(7,:); PSn = x(8,:); diff = PRn - PSn; conversion = PRn + PSn; ees = diff./conversion; else % is toobig showplot = false; t = '*'; conversion = 'EobySo too big'; ees = 'EobySo too big'; end % Prepare for output names = ["S0", "Keq", "Eval", "KM", "k4", "SCRrat", ... "k24rat", "k2rat", "k4rat", "k34rat", "k13Rrat", "k13Srat"]; values = [S0, Keq, Evalue, KM, k4, SCRrat, k24rat, ... k2rat, k4rat, k34rat, k13Rrat, k13Srat]; nonames = length(names); if length(values) ~= nonames error ('Different length names and values') end if showplot nolines = fix((nonames-1)/5) + 1; for lineno = 1:nolines firstctr = 5*(lineno-1) + 1; lastctr = 5*(lineno-1)+ 5; if lastctr > nonames lastctr = nonames; end lbl(lineno)=""; for ctr = firstctr:lastctr lbl(lineno) = strcat(lbl(lineno), names(ctr), "=", num2str(values(ctr)), " "); %thislbl = strcat(lbl(lineno), names(ctr), "=", num2str(values(ctr)), " ") end end figure (1) %plot(t,x(1,:),'b--',t,x(2,:),'r-',t,x(3,:),'g-.',t,x(4,:),'m-',t,x(5,:),'c-.','LineWidth',2.5); %plot(t,x(6,:),'b--',t,x(7,:),'r-',t,x(8,:),'g-.',t,x(9,:),'m-',t,x(10,:),'c-.','LineWidth',2.5); plot(t,conversion,'b--',t,ees,'r-',t,x(1,:),'g-.','LineWidth',2.5); hold on; %plot(t(b),a,'g.','MarkerSize',24); %text(t(b),a+0.05,max_text);xlabel('\fontsize{16}time/s'); % From help %dim = [.2 .5 .3 .3]; %annotation('textbox',dim,'String',str,'FitBoxToText','on'); % By trial and error, x and y run from origin in bottom left for ctr = 1:nolines dim = [0.15 0.67-0.07*ctr .3 .3]; annotation('textbox',dim,'String',lbl(ctr), 'FitBoxToText', 'on'); end ylabel('\fontsize{16}non-dimensional concentrations'); %title('\fontsize{14}time profile of state variables'); %legend('En','EDn','EtRn','EtSn','EQn'); %legend('Dn','Qn','Sn','PRn','PSn'); legend('Conv','ee_P','En'); end % Output_progress filename = 'C:\Users\ph\My Documents\A Edited\ProgressCurves.xlsx'; %rowno = int8(5) startcol = 5; firstdatacol = startcol + nonames + 2; rowno = FindBlankRow(filename, 5, firstdatacol); for ctr = 1:nonames writematrix(names(ctr), filename, 'Sheet', 1, 'Range', MakeCellRef(rowno, startcol+ctr-1)); writematrix(values(ctr), filename, 'Sheet', 1, 'Range', MakeCellRef(rowno+ 1, startcol+ctr-1)); end writematrix('time',filename, 'Sheet', 1, 'Range', MakeCellRef(rowno, firstdatacol)); writematrix('conv',filename, 'Sheet', 1, 'Range', MakeCellRef(rowno+1, firstdatacol)); writematrix('ee_P',filename, 'Sheet', 1, 'Range', MakeCellRef(rowno+2, firstdatacol)); %nextref = MakeCellRef(rowno,startcol+15); nextref = MakeCellRef(rowno,firstdatacol+1); writematrix(transpose(t),filename,'Sheet',1,'Range',nextref); nextref = MakeCellRef(rowno+1, firstdatacol+1); writematrix(conversion,filename,'Sheet',1,'Range',nextref); nextref = MakeCellRef(rowno+2, firstdatacol+1); writematrix(ees,filename,'Sheet',1,'Range',nextref); dummy = 1; %to prevent warning % Could look for ee's at various conversions here, but not at the moment end function rowno = FindBlankRow(filename, startrow, colno) %rowno = startrow; %foundit = false; %while ~foundit % cellref = MakeCellRef(int8(rowno), int8(colno)) % entry = readmatrix(filename, 'Sheet', 1, 'Range', strcat(cellref, ':', cellref)) % Doesn't work, reads series of rows, then type mismatch on next % statement % if entry == '' % foundit = true % end % rowno = rowno + 1; cellref = MakeCellRef(int8(startrow), int8(colno)); entries = readmatrix(filename, 'Sheet', 1, 'Range', strcat(cellref, ':', cellref)); norows = length(entries); rowno = startrow + norows; end function cellref = MakeCellRef(rowno, colno) firstletno = idivide(colno - 1, int8(26)); secondletno = mod(colno - 1, int8(26)) + 1; if firstletno > 0 cellref = char(firstletno + 64); else cellref = ''; end cellref = strcat(cellref,char(secondletno+64)); cellref = strcat(cellref,num2str(rowno)); end