1pub fn interp1d(x_values: &[f64], y_values: &[f64], x: f64) -> f64 {
14 assert_eq!(
15 x_values.len(),
16 y_values.len(),
17 "x_values and y_values must have the same length"
18 );
19
20 if x <= x_values[0] {
21 return y_values[0];
22 }
23 if x >= x_values[x_values.len() - 1] {
24 return y_values[y_values.len() - 1];
25 }
26
27 for i in 0..x_values.len() - 1 {
28 let x0 = x_values[i];
29 let x1 = x_values[i + 1];
30 let y0 = y_values[i];
31 let y1 = y_values[i + 1];
32
33 if x >= x0 && x <= x1 {
34 return y0 + (y1 - y0) * (x - x0) / (x1 - x0);
35 }
36 }
37
38 panic!("Interpolation error: x-value out of interpolation range");
39}