什么是外推?
外推是数学中的过程,其中所需值的估计超出给定变量范围的范围。外推法通常用于估计低于或高于给定范围的某些观测数据。外推法也称为数学预测,通过观察给定变量之间的关系来预测值。外推的过程很多,这里只讨论线性外推。 1959年,托马斯·克莱尔森( Thomas D. Clareson )在他的科学著作中首次描述了这一过程。他通过了解给定的数据将其称为有意义的预测。
如何计算线性外推?
当给出线性函数时,该方法很有用。通过绘制切线并将其扩展到极限以完成此操作。当要预测的点与其余点相距不远时,线性外推法会提供很好的结果。
外推公式:
这里和有两个给定的点,x是我们要预测y值的点流。
例子:
Input: , , x = 1.2
Output: y = 3.15
执行:
C++
// C++ code for the implementation
// of Linear extrapolation
#include
using namespace std;
// Consider a structure
// to keep each pair of x and y together
struct Data {
double x, y;
};
// Function to calculate
// the linear extrapolation
double extrapolate(Data d[], double x)
{
double y;
y = d[0].y
+ (x - d[0].x)
/ (d[1].x - d[0].x)
* (d[1].y - d[0].y);
return y;
}
// Driver Code
int main()
{
// Sample dataset
Data d[] = { { 1.2, 2.7 }, { 1.4, 3.1 } };
// Sample x value
double x = 2.1;
// Finding the extrapolation
cout << "Value of y at x = 2.1 : "
<< extrapolate(d, x);
return 0;
}
Java
// Java code for the implementation of
// Linear extrapolation
class GFG
{
// Function to calculate the linear
// extrapolation
static double extrapolate(double[][] d, double x)
{
double y = d[0][1] + (x - d[0][0]) /
(d[1][0] - d[0][0]) *
(d[1][1] - d[0][1]);
return y;
}
// Driver Code
public static void main (String[] args)
{
// Sample dataset
double[][] d = {{ 1.2, 2.7 },{ 1.4, 3.1 }};
// Sample x value
double x = 2.1;
// Finding the extrapolation
System.out.println("Value of y at x = 2.1 : " +
extrapolate(d, x));
}
}
// This code is contributed by chandan_jnu
Python3
# Python3 code for the implementation of
# Linear extrapolation
# Function to calculate the linear
# extrapolation
def extrapolate(d, x):
y = (d[0][1] + (x - d[0][0]) /
(d[1][0] - d[0][0]) *
(d[1][1] - d[0][1]));
return y;
# Driver Code
# Sample dataset
d = [[ 1.2, 2.7 ], [1.4, 3.1 ]];
# Sample x value
x = 2.1;
# Finding the extrapolation
print("Value of y at x = 2.1 :",
extrapolate(d, x));
# This code is contributed by mits
C#
// C# code for the implementation of
// Linear extrapolation
class GFG
{
// Function to calculate the linear
// extrapolation
static double extrapolate(double[,] d, double x)
{
double y = d[0,1] + (x - d[0,0]) /
(d[1,0] - d[0,0]) *
(d[1,1] - d[0,1]);
return y;
}
// Driver Code
static void Main()
{
// Sample dataset
double[,] d = {{ 1.2, 2.7 },{ 1.4, 3.1 }};
// Sample x value
double x = 2.1;
// Finding the extrapolation
System.Console.WriteLine("Value of y at x = 2.1 : " +
extrapolate(d, x));
}
}
// This code is contributed by chandan_jnu
PHP
输出:
Value of y at x = 2.1 : 4.5