Why the if loop are getting the exact values of the Kf_LMax values not the approximated values in the different phase of the Relative ligand Concentration?

98 views (last 30 days)
Why the if loop are getting the exact values of the Kf_LMax values not the approximated values in the different phase of the Relative ligand Concentration?
% Define parameters
Kf_Max = 100; % maximum forward rate
RLC = [0.1, 0.5, 1, 5, 10, 0.1]; % RLC values
TauKf_ON = -0.1; % TauKf_ON
TauKf_OFF = -0.1; % TauKf_OFF
PhaseTimes = [0, 50, 100, 200, 400, 600, 1000]; % phase times (each row defines a phase)
% Generate time vector
t = 0:0.01:1000;
% Call the function to compute receptor concentrations and Kf
[ Kf_LMax] = Kf_Cal(Kf_Max, RLC, TauKf_ON, TauKf_OFF, PhaseTimes, t);
% Plotting
figure;
% Plot Kf_LMax
plot(t, Kf_LMax, '-', 'LineWidth', 2 );
title('Kf_LMax');
xlabel('Time');
ylabel('Kf_LMax');
grid on;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [Kf_LMax] = Kf_Cal(Kf_Max, RLC, TauKf_ON, TauKf_OFF, PhaseTimes, t)
% Compute Kf_LMax values based on RLC
Kf_LMax_values = Kf_Max * (RLC ./ (1 + RLC));
% Initialize output array
Kf_LMax = zeros(size(t));
% Number of phases
num_phases = numel(RLC);
% For each phase, calculate the corresponding Kf_LMax
for i = 1:num_phases
% Get start and end times for the phase
T_start = PhaseTimes(i);
if i < num_phases
T_end = PhaseTimes(i + 1);
else
T_end = inf; % Last phase continues indefinitely
end
% Logical indices for time points in the current phase
phase_idx = (t >= T_start) & (t < T_end);
if i == 1
% For the first phase, simply assign the maximum value
Kf_LMax(phase_idx) = Kf_LMax_values(i);
else
% For later phases, handle the dynamic response
prev_end = PhaseTimes(i - 1);
if RLC(i - 1) < RLC(i)
% Increasing RLC
Kf_LMax(phase_idx) = Kf_LMax_values(i) - (Kf_LMax_values(i) - Kf_LMax_values(i - 1)) * exp(TauKf_ON * (t(phase_idx) - prev_end));
else
% Decreasing RLC
Kf_LMax(phase_idx) = Kf_LMax_values(i) + (Kf_LMax_values(i) - Kf_LMax_values(i - 1)) * exp(TauKf_OFF * (t(phase_idx) - prev_end));
end
end
end
end
  5 Comments
Voss
Voss on 8 Oct 2024
"(Kf_LMax_values(i) - Kf_LMax_values(i - 1)) --> 0"
Not true.
% Define parameters
Kf_Max = 100; % maximum forward rate
RLC = [0.1, 0.5, 1, 5, 10, 0.1]; % RLC values
TauKf_ON = -0.1; % TauKf_ON
TauKf_OFF = -0.1; % TauKf_OFF
PhaseTimes = [0, 50, 100, 200, 400, 600, 1000]; % phase times (each row defines a phase)
% Generate time vector
t = 0:0.01:1000;
% Call the function to compute receptor concentrations and Kf
[ Kf_LMax] = Kf_Cal(Kf_Max, RLC, TauKf_ON, TauKf_OFF, PhaseTimes, t);
i=2: Kf_LMax_values(2) - Kf_LMax_values(1) = 24.242424 i=3: Kf_LMax_values(3) - Kf_LMax_values(2) = 16.666667 i=4: Kf_LMax_values(4) - Kf_LMax_values(3) = 33.333333 i=5: Kf_LMax_values(5) - Kf_LMax_values(4) = 7.575758 i=6: Kf_LMax_values(6) - Kf_LMax_values(5) = -81.818182
% Plotting
figure;
% Plot Kf_LMax
plot(t, Kf_LMax, '-', 'LineWidth', 2 );
title('Kf_LMax');
xlabel('Time');
ylabel('Kf_LMax');
grid on;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [Kf_LMax] = Kf_Cal(Kf_Max, RLC, TauKf_ON, TauKf_OFF, PhaseTimes, t)
% Compute Kf_LMax values based on RLC
Kf_LMax_values = Kf_Max * (RLC ./ (1 + RLC));
% Initialize output array
Kf_LMax = zeros(size(t));
% Number of phases
num_phases = numel(RLC);
% For each phase, calculate the corresponding Kf_LMax
for i = 1:num_phases
% Get start and end times for the phase
T_start = PhaseTimes(i);
if i < num_phases
T_end = PhaseTimes(i + 1);
else
T_end = inf; % Last phase continues indefinitely
end
% Logical indices for time points in the current phase
phase_idx = (t >= T_start) & (t < T_end);
if i == 1
% For the first phase, simply assign the maximum value
Kf_LMax(phase_idx) = Kf_LMax_values(i);
else
fprintf('i=%d: Kf_LMax_values(%d) - Kf_LMax_values(%d) = %f\n', ...
i,i,i-1,Kf_LMax_values(i) - Kf_LMax_values(i - 1))
% For later phases, handle the dynamic response
prev_end = PhaseTimes(i - 1);
if RLC(i - 1) < RLC(i)
% Increasing RLC
Kf_LMax(phase_idx) = Kf_LMax_values(i) - (Kf_LMax_values(i) - Kf_LMax_values(i - 1)) * exp(TauKf_ON * (t(phase_idx) - prev_end));
else
% Decreasing RLC
Kf_LMax(phase_idx) = Kf_LMax_values(i) + (Kf_LMax_values(i) - Kf_LMax_values(i - 1)) * exp(TauKf_OFF * (t(phase_idx) - prev_end));
end
end
end
end
Image Analyst
Image Analyst on 10 Oct 2024
To have underlines in the string be underlines and not cause a subscript, use the 'interpreter' 'none' option
title('Kf_LMax', 'Interpreter', 'none');
xlabel('Time', 'Interpreter', 'none');
ylabel('Kf_LMax', 'Interpreter', 'none');

Sign in to comment.

Accepted Answer

Voss
Voss on 8 Oct 2024
prev_end = PhaseTimes(i - 1);
That's the start of the previous phase, not the end of the previous phase.
Using the previous phase's start time causes the exponentials to appear to be approximately flat by the time the current phase starts, which is why the plot looks like a bunch of horizontal lines.
My guess is that you want to see exponential ramp up in each phase (except the first phase), and I guess for that you should use the end of the previous phase (= start of the current phase = PhaseTimes(i) = T_start).
% Define parameters
Kf_Max = 100; % maximum forward rate
RLC = [0.1, 0.5, 1, 5, 10, 0.1]; % RLC values
TauKf_ON = -0.1; % TauKf_ON
TauKf_OFF = -0.1; % TauKf_OFF
PhaseTimes = [0, 50, 100, 200, 400, 600, 1000]; % phase times (each row defines a phase)
% Generate time vector
t = 0:0.01:1000;
% Call the function to compute receptor concentrations and Kf
[ Kf_LMax] = Kf_Cal(Kf_Max, RLC, TauKf_ON, TauKf_OFF, PhaseTimes, t);
% Plotting
figure;
% Plot Kf_LMax
plot(t, Kf_LMax, '-', 'LineWidth', 2 );
title('Kf_LMax');
xlabel('Time');
ylabel('Kf_LMax');
grid on;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [Kf_LMax] = Kf_Cal(Kf_Max, RLC, TauKf_ON, TauKf_OFF, PhaseTimes, t)
% Compute Kf_LMax values based on RLC
Kf_LMax_values = Kf_Max * (RLC ./ (1 + RLC));
% Initialize output array
Kf_LMax = zeros(size(t));
% Number of phases
num_phases = numel(RLC);
% For each phase, calculate the corresponding Kf_LMax
for i = 1:num_phases
% Get start and end times for the phase
T_start = PhaseTimes(i);
if i < num_phases
T_end = PhaseTimes(i + 1);
else
T_end = inf; % Last phase continues indefinitely
end
% Logical indices for time points in the current phase
phase_idx = (t >= T_start) & (t < T_end);
if i == 1
% For the first phase, simply assign the maximum value
Kf_LMax(phase_idx) = Kf_LMax_values(i);
else
% For later phases, handle the dynamic response
if RLC(i - 1) < RLC(i)
% Increasing RLC
Kf_LMax(phase_idx) = Kf_LMax_values(i) - (Kf_LMax_values(i) - Kf_LMax_values(i - 1)) * exp(TauKf_ON * (t(phase_idx) - T_start));
else
% Decreasing RLC
Kf_LMax(phase_idx) = Kf_LMax_values(i) + (Kf_LMax_values(i) - Kf_LMax_values(i - 1)) * exp(TauKf_OFF * (t(phase_idx) - T_start));
end
end
end
end
  27 Comments
Walter Roberson
Walter Roberson on 11 Nov 2024 at 17:44
You initialize a variable
last_phase_valid = false;
You change
if i == 1
to
if ~last_phase_valid
After
last_phase_idx = find(phase_idx, 1, 'last');
you add
last_phase_valid = true;
Ehtisham
Ehtisham on 12 Nov 2024 at 8:03
This comment was flagged by Ehtisham
function [Kf_LMax] = Kf_Cal(t, PhaseTimes, Kf_Max, RLC, TauKf_ON, TauKf_OFF)
% Compute Kf_LMax values based on RLC
Kf_LMax_values = Kf_Max * (RLC ./ (1 + RLC)); % Normalized Kf_LMax values
% Initialize output array
Kf_LMax = zeros(size(t)); % Array to store calculated values
% Number of phases
num_phases = numel(RLC);
% Initialize the last phase index and validity flag for transitions
last_phase_idx = 1; % Initial index for the start of the first phase
last_phase_valid = false; % Validity flag for transitions
% Loop over each phase
for i = 1:num_phases
% Get start time for each phase
T_start = PhaseTimes(i);
% Get end time for the current phase, or set to infinity if it's the last phase
if i < num_phases
T_end = PhaseTimes(i + 1);
else
T_end = inf; % Last phase continues indefinitely
end
% Identify the indices for the current phase time range
phase_idx = (t >= T_start) & (t < T_end);
% Transition handling for first and subsequent phases
if ~last_phase_valid
% In the first phase, assign Kf_LMax as the calculated value directly
Kf_LMax(phase_idx) = Kf_LMax_values(i);
else
% Smooth transition based on the change in RLC value
if RLC(i - 1) < RLC(i)
% If RLC is increasing, apply TauKf_ON for a smooth transition
Kf_LMax(phase_idx) = Kf_LMax_values(i) - ...
(Kf_LMax_values(i) - Kf_LMax(last_phase_idx)) .* exp(TauKf_ON * (t(phase_idx) - T_start));
else
% If RLC is decreasing, apply TauKf_OFF for a smooth transition
Kf_LMax(phase_idx) = Kf_LMax_values(i) + ...
(Kf_LMax(last_phase_idx) - Kf_LMax_values(i)) .* exp(TauKf_OFF * (t(phase_idx) - T_start));
end
end
% Save the last valid index for smooth transitions between phases
last_phase_idx = find(phase_idx, 1, 'last');
last_phase_valid = true;
end
end
Unable to perform assignment because the left and right sides have a different number of elements.
Error in SimfileNPhase>Kf_Cal (line 119)
Kf_LMax(phase_idx) = Kf_LMax_values(i) - (Kf_LMax_values(i) - Kf_LMax(last_phase_idx)) .* exp(TauKf_ON .* (t(phase_idx) - T_start));
@Walter Roberson still getting the error

Sign in to comment.

More Answers (1)

dpb
dpb on 8 Oct 2024
Moved: dpb on 8 Oct 2024
Please format your code with the CODE button (or select and Ctrl-E)...
You're still multiplying the exponential portion by zero because the difference term is still there...probably what you're looking for is more like
Kf_LMax(phase_idx) = Kf_LMax_values(i-1)-Kf_LMax_values(i-1))*exp(TauKf_ON * (t(phase_idx) - prev_end));
which could be rewritten as
Kf_LMax(phase_idx)=Kf_LMax_values(i-1)*(1-exp(TauKf_ON*(t(phase_idx)-prev_end));

Products


Release

R2023a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!