xSol(t) =
Hi @Ehtisham
I am unfamiliar with your equation, but it appears that you did not "mathematically" impose the desired upper limit (100) on the 'Kf_L' variable. I selected a specific time at (where the peak approximately occurs) to compute the instantaneous value of 'Kf_L', which depends on the incremental computations of 'Kf_endA' and 'Kf_L'. The returned value is 102.6787, which clearly exceeds the desired upper limit.
In my opinion, a time-varying parameter like 'Kf_L', if governed by an exponentially decaying dynamic law, should be best formulated as a differential equation, and then solved either analytically or numerically.
% Define parameters
Kf_Max = 100; % maximum forward rate
RLC = [0.1, 0.5, 10, 5, 10, 1]; % RLC values
TauKf_ON = -0.01; % TauKf_ON
TauKf_OFF = -0.01; % TauKf_OFF
PhaseTimes = [0, 500, 1000, 2000, 3000, 4000, 5000]; % phase times (each row defines a phase)
% Generate time vector
% t = 0:0.01:5000;
t = 1125;
% Call the function to compute receptor concentrations and Kf
[Kf_LMax] = Kf_Cal(Kf_Max, RLC, TauKf_ON, TauKf_OFF, PhaseTimes, t)
%% TEST
% Kf_endA = Kf_LMax_values(j) - (Kf_LMax_values(j) - Kf_LMax_values(j - 1))*exp(TauKf_ON*(t_current - T_start))
Kf_endA = 90.9091 - (90.9091 - 33.3333 )*exp(-0.01 *(1125 - 1000))
% Kf_L = Kf_LMax_values(j) + (Kf_endA - Kf_LMax_values(j - 1))*exp(TauKf_OFF*(t_current - T_start));
Kf_L = 90.9091 + (74.4134 - 33.3333 )*exp(-0.01 *(1125 - 1000))
% Plotting
% figure;
% % Plot Kf_LMax
% plot(t, Kf_LMax, 'bo--', 'LineWidth', 1.5);
% title('Kf_LMax over Time');
% xlabel('Time');
% ylabel('Kf_LMax');
% grid on;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [Kf_LMax] = Kf_Cal(Kf_Max, RLC, TauKf_ON, TauKf_OFF, PhaseTimes, t)
% Initialize output variables
Kf_LMax = zeros(size(t));
% Calculate Kf_L for each time step
for i = 1:length(t)
Kf_LMax(i) = calculate_kf(t(i), PhaseTimes, Kf_Max, RLC, TauKf_ON, TauKf_OFF);
end
% Nested function for calculating Kf_L based on time
function Kf_L = calculate_kf(t_current, PhaseTimes, Kf_Max, RLC, TauKf_ON, TauKf_OFF)
% Calculate maximum Kf_L based on RLC values using Element wise
% division
Kf_LMax_values = Kf_Max * (RLC ./ (1 + RLC))
% Default Kf_L to the maximum value of the first phase
Kf_L = Kf_LMax_values(1);
% Number of phases
num_phases = numel(RLC);
% Iterate through each phase to determine the current phase based on time
for j = 1:num_phases
T_start = PhaseTimes(j); % Start time of the current phase
if j < num_phases
T_end = PhaseTimes(j + 1); % End time of the current phase
else
T_end = inf; % Handle last phase separately
end
% Check if the current time t_current is within the current phase
if t_current >= T_start && t_current < T_end
if j == 1
% For the first phase, use the maximum value directly
Kf_L = Kf_LMax_values(j);
else
% Time at the end of the previous phase
% prev_end = PhaseTimes(j - 1);
if j < num_phases
% Check RLC conditions and compute Kf_L
if RLC(j - 1) < RLC(j) && RLC(j) > RLC(j + 1)
% Peak condition
Kf_endA = Kf_LMax_values(j) - (Kf_LMax_values(j) - Kf_LMax_values(j - 1))*exp(TauKf_ON * (t_current - T_start))
Kf_L = Kf_LMax_values(j) + (Kf_endA - Kf_LMax_values(j - 1))*exp(TauKf_OFF*(t_current - T_start))
elseif RLC(j - 1) < RLC(j) && RLC(j) < RLC(j + 1)
% Increasing RLC condition
Kf_endB = Kf_LMax_values(j) - (Kf_LMax_values(j) - Kf_LMax_values(j - 1))*exp(TauKf_ON * (t_current - T_start));
Kf_L = Kf_LMax_values(j) + (Kf_endB - Kf_LMax_values(j - 1))*exp(TauKf_OFF*(t_current - T_start));
elseif RLC(j - 1) > RLC(j) && RLC(j) < RLC(j + 1)
% Decreasing RLC condition
Kf_endC = Kf_LMax_values(j) - (Kf_LMax_values(j) - Kf_LMax_values(j - 1))*exp(TauKf_ON * (t_current - T_start));
Kf_L = Kf_LMax_values(j) + (Kf_endC - Kf_LMax_values(j - 1))*exp(TauKf_OFF*(t_current - T_start ));
elseif RLC(j - 1) > RLC(j) && RLC(j) >= RLC(j + 1)
% Flat or decreasing RLC condition
Kf_endD = Kf_LMax_values(j) - (Kf_LMax_values(j) - Kf_LMax_values(j - 1))*exp(TauKf_ON * (t_current - T_start));
Kf_L = Kf_LMax_values(j) + (Kf_endD - Kf_LMax_values(j - 1))*exp(TauKf_OFF*(t_current - T_start ));
end
else
% % % Handling for the last phase
if RLC(j - 1) < RLC(j)
Kf_end1 = Kf_LMax_values(j) - (Kf_LMax_values(j) - Kf_LMax_values(j - 1)) * exp(TauKf_ON * (t_current - T_start));
Kf_L = Kf_LMax_values(j) + (Kf_end1 - Kf_LMax_values(j - 1)) * exp(TauKf_OFF * (t_current - T_start));
elseif RLC(j - 1) > RLC(j)
Kf_end2 = Kf_LMax_values(j) - (Kf_LMax_values(j) - Kf_LMax_values(j - 1)) * exp(TauKf_ON * (t_current - T_start));
Kf_L = Kf_LMax_values(j) + (Kf_end2 - Kf_LMax_values(j - 1)) * exp(TauKf_OFF * (t_current - T_start));
end
end
end
return; % Exit the function after finding the correct phase
end
end
end
end