計算方法 差商與牛頓插值

題目

在這裏插入圖片描述

牛頓插值

在這裏插入圖片描述

求差商代碼

#include <iostream>
#include <algorithm>
#include <vector>
#include <map>
#include <string>
#include <string.h>
#include <cstdio>
#include <cstdlib>
#include <queue>
using namespace std;
const int maxn = 1e2;

double ans[maxn][maxn];
struct node{
    double x, fx;
}num[maxn];

int main(){
    printf("個數:");
    int n;///5
    cin >> n;
    for(int i = 0; i < n; i++){
        cin >> num[i].x;
    }
    for(int i = 0; i < n; i++){
        cin >> num[i].fx;
        ans[i][0] = num[i].fx;
    }
    for(int i = 1; i <= n; i++){
        for(int j = i; j <= n; j++)
        {
            ans[j][i] = (ans[j - 1][i - 1] - ans[j][i - 1]) / (num[j - i].x - num[j].x);
        }
    }
    for(int i = 0; i < n; i++){
        for(int j = 0; j <= i; j++){
            printf("%f ", ans[i][j]);
        }
        printf("\n");
    }
    return 0;
}
/*
5
0 1 2 3 4
0 16 46 88 0
*/
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章