t_g1s = (G1s - y_values_first[-1]) / gradient + x_values_first[-1]
num_points_second = FREQ
x_values_second = np.linspace(x_values_first[-1], t_g1s, num_points_second)
y_values_second = y_values_first[-1] + gradient * (
x_values_second - x_values_first[-1]
)
g1s_x = x_values_second[-1]
g1s_y = y_values_second[-1]
if g1s_y < dmax_80:
x_values = np.concatenate((x_values_first, []))
y_values = np.concatenate((y_values_first, []))
num_points_third = FREQ
x_values_third = np.linspace(g1s_x, g1s_x + 1, num_points_third)
k = gradient * 2
A = g1s_y
B = -g1s_y * np.exp(-k) / (1 - np.exp(-k))
y_values_third = A * np.exp(-k * np.linspace(0, 1, num_points_third)) + B. That if is not really correct, if the code gets' tehre the x_valeus_second and y_values_second should be concated but just the points before g1s_y/x
Assistant
Thank you for sharing your code and pointing out the issue. If I understand correctly, the issue arises when the condition if g1s_y < dmax_80 is triggered. Specifically, the concatenation step is incorrectly handled. Instead of concatenating full x_values_second and y_values_second, you want to include only the points before the point (g1s_x, g1s_y) (i.e., before g1s_y is reached in y_values_second).
Let’s fix that issue step by step:
Correct Behavior
When g1s_y < dmax_80, you want:
Concatenate the values of x_values_first and y_values_first.
Use only the subset of x_values_second and y_values_second that includes values before the (g1s_x, g1s_y) point.
Continue the third phase (x_values_third and y_values_third) as normal.
Fixed Code Snippet
Here’s the corrected code with the changes applied:
python
def stop_approach_phase(start_time, gradient):
t80_dmax = start_time + T80
dmax_80 = 0.8 * DMAX
num_points_first = FREQ
x_values_first = np.linspace(start_time, t80_dmax, num_points_first)
normalized_x = (x_values_first - start_time) / T80
y_values_first = DMAX - (DMAX - dmax_80) * (normalized_x**2.5)
t_g1s = (G1s - y_values_first[-1]) / gradient + x_values_first[-1]
num_points_second = FREQ
x_values_second = np.linspace(x_values_first[-1], t_g1s, num_points_second)
y_values_second = y_values_first[-1] + gradient * (
x_values_second - x_values_first[-1]
)
g1s_x = x_values_second[-1]
g1s_y = y_values_second[-1]
if g1s_y < dmax_80:
# Take only points before g1s_x from x_values_second and y_values_second
mask = y_values_second <= g1s_y # Points before or at g1s_y
x_values_second = x_values_second[mask]
y_values_second = y_values_second[mask]
# Concatenate first phase and modified second phase
x_values = np.concatenate((x_values_first, x_values_second))
y_values = np.concatenate((y_values_first, y_values_second))
# Continue with the third phase
num_points_third = FREQ
x_values_third = np.linspace(g1s_x, g1s_x + 1, num_points_third)
k = gradient * 2
A = g1s_y
B = -g1s_y * np.exp(-k) / (1 - np.exp(-k))
y_values_third = A * np.exp(-k * np.linspace(0, 1, num_points_third)) + B
# Concatenate third phase
x_values = np.concatenate((x_values, x_values_third))
y_values = np.concatenate((y_values, y_values_third))
return x_values, y_values
else:
# Normal case when g1s_y >= dmax_80
x_values = np.concatenate((x_values_first, x_values_second))
y_values = np.concatenate((y_values_first, y_values_second))
return x_values, y_values
Explanation of Fixes:
Subset of x_values_second and y_values_second:
The use of the mask = y_values_second <= g1s_y ensures that we only select points where y_values_second is less than or equal to g1s_y.
This ensures that only the "valid" range is concatenated.
Concatenation Order:
The selected subset of x_values_second and y_values_second is added after x_values_first and y_values_first.
Third Phase Continuation:
The concatenation now includes the two corrected phases (first + second) before starting the third phase (third).
Fallback Case for Normal Behavior:
In the else block, the concatenation remains straightforward when g1s_y >= dmax_80.