Xây dựng thư viện Interpolation như mộ hình UML như hình sau
Cách tính: tìm giá trị y gần với giá x nhất
Ví dụ
double[] x = { 0, 1, 2, 6 };
double[] y = { 3, 5, 9, 15 };
| Input (x) | Output (y) |
| 3 | 9 |
| 4 | 9 |
| 5 | 15 |
Công thức tính:
$$y=y_1+\frac{(x-x_0)(y_1-y_0)}{x_1-x_0}$$
Ví dụ:
double[] x = { 0, 3 };
double[] y = { 7, 15 };
| Input (x) | Output (y) |
| 1 | 9.666666666666666 |
| 2 | 12.333333333333332 |
Công thức tính
$$y=\frac{(x-x_1)(x-x_2)}{(x_0-x_1)(x_0-x_2)} y_0 + \frac{(x-x_0)(x-x_2)}{(x_1-x_0)(x_1-x_2)} y_1 + \frac{(x-x_0)(x-x_1)}{(x_2-x_0)(x_2-x_1)} y_2$$
Ví dụ:
double[] x = { 0, 1, 4 };
double[] y = { 7, 9, 15 };
| Input (x) | Output (y) |
| 2 | 11 |
| 3 | 13 |
Công thức tính
$$y = \frac{(x-x_1)(x-x_2)(x-x_3)}{(x_0-x_1)(x_0-x_2)(x_0-x_3)} y_0+\frac{(x-x_0)(x-x_2)(x-x_3)}{(x_1-x_0)(x_1-x_2)(x_1-x_3)} y_1+\frac{(x-x_0)(x-x_1)(x-x_3)}{(x_2-x_0)(x_2-x_1)(x_2-x_3)} y_2+\frac{(x-x_0)(x-x_1)(x-x_2)}{(x_3-x_0)(x_3-x_1)(x_3-x_2)} y_3$$
double[] x = { 0, 1, 4, 5 };
double[] y = { 7, 9, 15, 19 };
| Input (x) | Output (y) |
| 2 | 10.6 |
| 3 | 12.4 |