拉格朗日插值法

作者在 2012-02-16 21:36:53 发布以下内容
#include <iostream>
#include <string.h>
#include <math.h>
#include <stdlib.h>

using namespace std;
#define N 4

double xx[N+1]={0.4,0.55,0.65,0.8,0.9};
double yy[N+1]={0.4175,0.57815,0.69657,0.88811,1.02652};

double lagrange(double *x,double *y,float k)
{
    double l,p;
    p=0.0;
    for(int i=0;i<=N;i++)
    {
        l=1;
        for(int j=0;j<=N;j++)
        {
            if(i!=j)
            {
                l=l*(k-x[j])/(x[i]-x[j]);
            }
        }
        p=p+l*y[i];
    }
    return p;
}

int main()
{
    int m;
    cout<<"请输入插值点的个数m=";
    cin>>m;
    for(int i=0;i<m;i++)
    {
        float temp;
        cout<<"请输入差指插值点的位置x"<<i<<"=";
        cin>>temp;
        cout<<"f("<<temp<<")="<<lagrange(xx,yy,temp)<<endl;
    }
    return 0;
}
文章评论,共0条
游客请输入验证码
浏览70041次