1. 程式人生 > >拉格朗日二次插值法C語言版

拉格朗日二次插值法C語言版

數值表是這樣的

X:0.46,0.47,0.48,,,,,,,

Y:0.4846555,0.4937452,0.5027498,,,,,,


由於是二次插值法,只需要三組XY資料


程式碼如下:

#include "stdafx.h"
#include "iostream"
using namespace std;

void main(int argc, char* argv[])
{
 double arr_x[3]={0.46,0.47,0.48};
 double arr_y[3]={0.4846555,0.4937452,0.5027498};
 double x=0.472;
 float y=0;
 for(int k=0;k<3;k++)
    {
        double t=1;
        for(int j=0;j<3;j++)
        {
           if(k!=j)
           {
              t=t*((x-arr_x[j])/(arr_x[k]-arr_x[j]));
           }   
        }
        y=y+arr_y[k]*t;
    }
    cout<<"y is:"<<y<<endl;
}