Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- using namespace std;
- double P(double x0, vector <double> x, vector <double> y){
- int N = x.size();
- double S = 0;
- for(int i = 0; i < N; ++i){
- double L = 1;
- for(int j = 0; j < N; ++j){
- if(i != j){
- L *= (x0 - x[j]) / (x[i] - x[j]);
- }
- }
- S += y[i] * L;
- }
- return S;
- }
- vector <double> get_coefficients(vector <double> x, vector <double> y){
- int N = x.size();
- vector <double> a(N);
- for(int i = 0; i < N; ++i){
- a[i] = y[i];
- for(int j = 0; j < N; ++j){
- if(i != j){
- a[i] /= x[i] - x[j];
- }
- }
- }
- return a;
- }
- int main(){
- int N;
- cin >> N;
- vector <double> x(N);
- vector <double> y(N);
- for(int i = 0; i < N; ++i){
- cin >> x[i] >> y[i];
- }
- for(auto a : get_coefficients(x, y)){
- cout << a << " ";
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement