Linear Extrapolation: ประมาณค่า y สำหรับ x ที่อยู่นอกช่วงข้อมูลที่รู้จัก โดยใช้เส้นตรงผ่านจุดสุดท้าย 2 จุด
สูตร
y = y₁ + (x - x₁) × (y₂ - y₁) / (x₂ - x₁)
ตัวอย่างโค้ด
#include <bits/stdc++.h>
using namespace std;
struct Data { double x, y; };
double extrapolate(Data d[], double x) {
// ใช้จุด d[0] และ d[1]
return d[0].y + (x - d[0].x) * (d[1].y - d[0].y) / (d[1].x - d[0].x);
}
int main() {
Data d[] = {{1.2, 2.7}, {1.4, 3.1}};
double x = 2.1;
cout << "Value of y (x = 2.1): " << extrapolate(d, x) << endl;
return 0;
}
ผลลัพธ์
Value of y (x = 2.1): 4.5
การตรวจสอบ
Slope = (3.1 - 2.7) / (1.4 - 1.2) = 0.4 / 0.2 = 2
y = 2.7 + (2.1 - 1.2) × 2 = 2.7 + 1.8 = 4.5 ✓
ข้อจำกัด
- แม่นยำก็ต่อเมื่อฟังก์ชันเป็นเชิงเส้นจริง หรือ x ใกล้ช่วงข้อมูล
- Extrapolation ไกลเกินไป Error จะใหญ่มาก (Runge's phenomenon)
- ควรใช้ Polynomial Regression หรือ Spline สำหรับข้อมูลซับซ้อน