数值分析---牛顿插值


#include<iostream>
#include<bits/stdc++.h>
using namespace std;
float m[100][100];
float Newton(float x[],float y[],int n,float t)
{
    int r=1;
    memset(m,0.0,sizeof(m));
    float value=y[0];
    for(int i=0;i<n;i++)
        m[i][0]=y[i];
    for(int j=1;j<n;j++)
    {
        for(int i=j;i<n;i++)
        {
            m[i][j]=(m[i][j-1]-m[i-1][j-1])/(x[i]-x[i-r]);
            r=r+1;
        }
    }
    float w=0.0;
    for(int i=1;i<n;i++)
    {
        w=w*(t-x[i-1]);
        value=value+m[i][i]*w;
    }
    return value;
}

int main()
{
    float x[1000],y[1000];
    int n;
    cout<<"输入插值节点的个数"<<endl;
    cin>>n;
    cout<<"接下来输入这些插值节点"<<endl;
    for(int i=0; i<n; i++)
    {
        cin>>x[i]>>y[i];
    }
    float t;
    cout<<"请输入待求解的插值节点的t值"<<endl;
    cin>>t;
    float result=Newton(x,y,n,t);
    cout<<"输出差值矩阵"<<endl;
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<=i;j++)
            cout<<m[i][j]<<" ";
        cout<<endl;
    }
    cout<<"输出拉格朗日的值 "<<result<<endl;
    return 0;
}

6
0.0 0.39894
0.1 0.39695
0.195 0.39142
0.3 0.38138
0.401 0.36812
0.5 0.35206

0.15

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值