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?

조회 수: 66 (최근 30일)
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
Voss
Voss 2024년 10월 8일
"(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 2024년 10월 10일
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');

댓글을 달려면 로그인하십시오.

채택된 답변

Voss
Voss 2024년 10월 8일
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
  댓글 수: 32
Ehtisham
Ehtisham 2024년 12월 3일 9:23
In Sim File my Kf code was giving the fixed values for app phases( like for RLC = 0.1 gives Kf values at 33.33 ) even when i change the Tau values so then but the code i put a question here and Torsten said your need to chnage the Kf_LMax_values(i-1) to Kf_LMax( last_phase_idx) that code work i give the graph that given above but when i put the Kf function in the Simfile function (main function ) its giving me assgiment error. after solving the assigmnet error again the Sime file (Kf function ) giving me fixed values like before.
Sam Chak
Sam Chak 2024년 12월 3일 11:05
You may have forgotten my previous comment, but this issue has persisted for more than three months. Please understand that eliminating the errors in the MATLAB code does not necessarily mean that your solution to the actual dynamical problem is correct. To address the problem effectively, you need to fully understand the mathematics behind it so that you can control the behavior of the system.
The exponential behaviors suggest that the system can be described by a 1st-order linear differential equation , in which the time constant of the system (possibly related to 'TauKf') influences the rate of exponential decay. As a researcher (not merely a programmer), you should have a single master mathematical model in the code, where you can flexibly set the value of the time constant.
Having too many nested 'If–Else' structures may hinder your ability to troubleshoot the dynamics easily, as each conditional statement can disrupt your visualization of the continuous time flow of the system. My suggestion is to apply a 'Divide and Conquer' approach by working through the time segments separately. If the exponential behaviors are correct, you can then combine them.
First and foremost, present the mathematical model, or, better yet, ask a Question. Do not simply accept a solution if it has not truly addressed your underlying problem.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

dpb
dpb 2024년 10월 8일
이동: dpb 2024년 10월 8일
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));

카테고리

Help CenterFile Exchange에서 Plot Customization에 대해 자세히 알아보기

제품


릴리스

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by