Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <iomanip>
- using namespace std;
- double f(double x){
- return 2*x*x - x + 3;
- }
- double df(double x){
- return 4*x - 1;
- }
- double L(double x, int n, double x_arr[], double y_arr[]){
- double sum = 0;
- for (int i = 0; i < n; ++i){
- double l = 1;
- for (int j = 0; j < n; ++j)
- if (j != i)
- l *= (x - x_arr[j]) / (x_arr[i] - x_arr[j]);
- sum += y_arr[i] * l;
- }
- return sum;
- }
- double dL(double x, int n, double x_arr[], double y_arr[]){
- double sum1 = 0;
- for (int i = 0; i < n; ++i){
- double sum2 = 0;
- for (int j = 0; j < n; ++j){
- if (j != i){
- double p = 1;
- for (int k = 0; k < n; ++k)
- if (k != i && k != j)
- p *= (x - x_arr[k]) / (x_arr[i] - x_arr[k]);
- sum2 += p/(x_arr[i] - x_arr[j]);
- }
- }
- sum1 += y_arr[i]*sum2;
- }
- return sum1;
- }
- int main(){
- const int N = 3;
- double x_arr[N], y_arr[N];
- for (int i = 0; i < N; ++i){
- x_arr[i] = 2*i;
- y_arr[i] = f(x_arr[i]);
- }
- cout << setw(3) << "x";
- cout << setw(10) << "f(x)";
- cout << setw(10) << "L(x)";
- cout << setw(10) << "df(x)";
- cout << setw(10) << "dL(x)" << endl;
- for (int i = 0; i < 20; ++i){
- double x = i;
- cout << setw(3) << x;
- cout << setw(10) << setprecision(6) << f(x);
- cout << setw(10) << setprecision(6) << L(x, N, x_arr, y_arr);
- cout << setw(10) << setprecision(6) << df(x);
- cout << setw(10) << setprecision(6) << dL(x, N, x_arr, y_arr);
- cout << endl;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement