使用 C++ 編寫的線性外推程式


在本文中,我們將討論一個實現線性外推的程式。

外推的定義是一個過程,在該過程中,某個函式所需的取值超出了函式定義的下限或上限。

在進行線性外推時,透過在函式圖形上作切線來找到超出範圍的取值,以確定所需取值。應用線性外推時,結果相當精確。

示例

#include <bits/stdc++.h>
using namespace std;
//structuring the values of x and y
struct Data {
   double x, y;
};
//calculating the linear extrapolation
double calc_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;
}
int main(){
   Data d[] = { { 1.2, 2.7 }, { 1.4, 3.1 } };
   double x = 2.1;
   cout << "Value of y (x = 2.1) : " << calc_extrapolate(d, x) << endl;
   return 0;
}

輸出

Value of y (x = 2.1) : 4.5

更新時間:2019-12-03

721 次瀏覽

開啟您的 職業征程

透過完成本課程獲得認證

開始學習
廣告
© . All rights reserved.