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?

1 Ansicht (letzte 30 Tage)
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?

Antworten (1)

nick
nick am 1 Okt. 2024
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' :

Kategorien

Mehr zu Data Type Conversion finden Sie in Help Center und File Exchange

Tags

Produkte


Version

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by