您的位置:首页 > 其它

插值法之Lagrange插值

2013-07-16 22:15 218 查看
//Lagrange插值法

#include <iostream>

#include <fstream>

using namespace std;

class lagrange

{

private:

int i, j, n;

double mult, sum, z;

double* x, * y;

public:

void interpolation();

~lagrange()

{

delete[] x, y;

}

};

void main()

{

lagrange interp;

interp.interpolation();

}

void lagrange::interpolation()

{

ifstream fin ("lagrange.txt");

fin >> n;

x = new double
;

y = new double
;

for (i = 0; i < n; i++)

{

fin >> x[i] >> y[i];

}

fin.close();

cout << "\n输入需要插值的点:";

cin >> z;

sum = 0.0;

for (i = 0; i < n; i++)

{

mult = 1.0;

for (j = 0; j < n; j++)

{

if (j != i)

{

mult *= (z - x[j]) / (x[i] - x[j]);

}

}

sum += mult * y[i];

}

cout << "\n插补的值 = " << sum << endl;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: