使用 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
廣告
資料結構
網路
關係型資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
JavaScript
PHP