ある関数

















// ver.041027.1
#ifndef __LAGRANGE_H
#define __LAGRANGE_H
#include <vector>
// x: 求めたい点
// xy: 既知の(x,y)の組 ただし (x,y)=(first,second)
double Lagrange(double x, std::vector<std::pair<double, double> > &xy)
{
double p = 0;
for(long i = 0; i < (long)xy.size(); i++){
double pi = 1;
for(long j = 0; j < (long)xy.size(); j++){
if(i != j){
pi *= (x - xy[j].first) / (xy[i].first - xy[j].first);
}
}
p += xy[i].second * pi;
}
return p;
}
#endif
#include <iostream>
#include <cmath>
#include <vector>
using namespace std;
#include "Lagrange.h"
main()
{
vector<pair<double, double> > xy;
double d = 0.1;
for(long i = 0; i < 10; i++){
xy.push_back(make_pair(i * d, sin(i * d)));
}
cout << Lagrange(0.05, xy) << endl;
cout << sin(0.05) << endl;
return 0;
}