How i modifed this for loop so it get the last value Kf_LMax of previous iteration not from the computed values before the loop?

53 次查看(过去 30 天)
function [Kf_LMax] = Kf_Cal(Kf_Max, RLC, TauKf_ON, TauKf_OFF, PhaseTimes, t)
%compute the maximum Kf_L based on RLC values
Kf_LMax_values = Kf_Max * (RLC ./ (1 + RLC));
% Determine which phase each time step falls into
num_phases = numel(RLC);
phase_idx = zeros(size(t));
for j = 1:num_phases
T_start = PhaseTimes(j);
if j < num_phases
T_end = PhaseTimes(j + 1);
else
T_end = inf;
end
phase_idx(t >= T_start & t < T_end) = j;
end
% Calculate Kf_L for each time step based on the phase
Kf_LMax = zeros(size(t));
for i = 1:length(t)
j = phase_idx(i);
if j == 1
Kf_LMax(i) = Kf_LMax_values(j);
else
prev_end = PhaseTimes(j - 1);
if RLC(j - 1) < RLC(j)
Kf_LMax(i) = Kf_LMax_values(j) - (Kf_LMax_values(j) - Kf_LMax_values(j - 1)) * exp(TauKf_ON * (t(i) - prev_end));
else
Kf_LMax(i) = Kf_LMax_values(j) + (Kf_LMax_values(j) - Kf_LMax_values(j - 1)) * exp(TauKf_OFF * (t(i) - prev_end));
end
end
end
end
How i modifed this for loop so it get the last value Kf_LMax of previous iteration not from the computed values in bold before the loop?

回答(1 个)

nick
nick 2024-10-1,7:54
Hi Ehtisham,
I understand that you want to modify the given function so that each iteration of the loop uses the last computed value of 'Kf_LMax' from the previous iteration instead of 'Kf_LMax_values(j - 1)'.
However, it would be really helpful to have more background information regarding the objective of the given function and the variables and any related files or data involved to accurately reproduce the results. Nevertheless, here's a suggestion based on my understanding on how you can modify the loop:
Kf_LMax = zeros(size(t));
Kf_LMax(1) = Kf_LMax_values(1); % Start with the first phase value
% Calculate Kf_L for each time step based on the phase
for i = 2:length(t)
j = phase_idx(i);
if j == 1
Kf_LMax(i) = Kf_LMax_values(j);
else
prev_end = PhaseTimes(j - 1);
if RLC(j - 1) < RLC(j)
Kf_LMax(i) = Kf_LMax(i-1) - (Kf_LMax_values(j) - Kf_LMax(i-1)) * exp(TauKf_ON * (t(i) - prev_end));
else
Kf_LMax(i) = Kf_LMax(i-1) + (Kf_LMax_values(j) - Kf_LMax(i-1)) * exp(TauKf_OFF * (t(i) - prev_end));
end
end
end
You can refer to the following documentation to learn more about 'Array Indexing' :
  1 个评论
Ehtisham
Ehtisham about 23 hours 前
编辑:Ehtisham about 21 hours 前
@nick @Star Strider thanks for your response but my question was that Kf_LMax(i) = Kf_LMax_values(j) - (Kf_LMax_values(j) - Kf_LMax_values(j - 1)) * exp(TauKf_ON * (t(i) - prev_end));
else
Kf_LMax(i) = Kf_LMax_values(j) + (Kf_LMax_values(j) - Kf_LMax_values(j - 1)) * exp(TauKf_OFF * (t(i) - prev_end));
end
in this loop for each iteration as is using the time of the prev_end so for Kf_LMax values is used the last value of the array of Kf_LMax_value for the next iteration.
It given the exact calculation of the Kf_LMax values given in plot not the approximate value like (red line in graph ) like this

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Data Type Conversion 的更多信息

标签

产品


版本

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by