Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _CRT_SECURE_NO_WARNINGS
- #include <iostream>
- #include <math.h>
- #include <cmath>
- #include <vector>
- using namespace std;
- vector<pair<double, double>> func_arg;
- double f(double x){
- return log(15.3 * x * x);
- }
- double Lagrange(double point, double left, double right) {
- double L = 0;
- double P;
- for (int i = left; i <= right; i++) {
- P = 1;
- for (int j = left; j <= right; j++) {
- if (j == i) {
- continue;
- }
- P *= (point - func_arg[j].second) / (func_arg[i].second - func_arg[j].second);
- }
- L += P * func_arg[i].first;
- }
- return L;
- }
- vector<double> findCoefficients() {
- int m = 3;
- vector<vector<double>> matrix(3, vector<double>(3, 0));
- double* B = new double[m];
- B[0] = 0;
- B[1] = 0;
- B[2] = 0;
- for (int i = 0; i < 14; i++) {
- matrix[0][0] += pow(func_arg[i].second, 4);
- matrix[0][1] += pow(func_arg[i].second, 3);
- matrix[0][2] += pow(func_arg[i].second, 2);
- B[0] += func_arg[i].first * pow(func_arg[i].second, 2);
- matrix[1][0] += pow(func_arg[i].second, 3);
- matrix[1][1] += pow(func_arg[i].second, 2);
- matrix[1][2] += func_arg[i].second;
- B[1] += func_arg[i].first * func_arg[i].second;
- matrix[2][0] += pow(func_arg[i].second, 2);
- matrix[2][1] += func_arg[i].second;
- matrix[2][2] += 1;
- B[2] += func_arg[i].first;
- }
- cout << "Matrix: \n";
- for (int i = 0; i < 3; i++) {
- printf("a*%.4lf + b*%.4lf + c*%.4lf = %.4lf\n", matrix[i][0], matrix[i][1], matrix[i][2], B[i]);
- }
- for (int i = 0; i < 3; i++) {
- double n = matrix[i][i];
- for (int j = 0; j < 3; j++) {
- if (i == j) {
- matrix[i][j] = 0;
- continue;
- }
- matrix[i][j] = matrix[i][j] / (-n);
- B[i] /= n;
- }
- }
- double* Res_prev = new double[m];
- memcpy(Res_prev, B, sizeof(double) * 3 + 1);
- double* Res = new double[m];
- double inaccuracy;
- double eps = 0.01;
- while (true) {
- for (int i = 0; i < 3; i++)
- Res[i] = matrix[i][0] * Res_prev[0] + matrix[i][1] * Res_prev[1] + matrix[i][2] * Res_prev[2] + B[i];
- inaccuracy = 0;
- for (int i = 0; i < 3; i++) {
- inaccuracy += pow((Res[i] - Res_prev[i]), 2);
- }
- inaccuracy = sqrt(inaccuracy);
- if (inaccuracy < eps) {
- break;
- }
- Res_prev = Res;
- }
- printf("\nsearch completed...");
- printf("coefficients:\na = %.4lf, b = %.4lf, c = %.4lf", Res[0], Res[1], Res[2]);
- vector<double> ans;
- ans.push_back(Res[0]);
- ans.push_back(Res[1]);
- ans.push_back(Res[2]);
- return ans;
- }
- double f_apr(vector<double>coefficients, double x) {
- return coefficients[0] * pow(x, 2) + coefficients[1] * x + coefficients[2];
- }
- int main() {
- double xi;
- for (int i = 1; i <= 14; i++) {
- xi = 0.2 * i;
- func_arg.push_back(make_pair(f(xi), xi));
- printf("%d: x = %.2lf y = %.4lf\n", i-1, func_arg[i-1].second, func_arg[i-1].first);
- }
- vector<double> coefficients = findCoefficients();
- double left = 1;
- double right = 3;
- double point = 0.5;
- double node = 0.6;
- double z1 = func_arg[13].second + 1;
- double z2 = func_arg[13].second + 5;
- printf("\nApproximation/Prediction points:\n%.4lf and %.4lf", z1, z2);
- printf("\n\nnode:");
- printf("\nFunction(%.2lf) = %.4lf", node, f(node));
- printf("\nLagrange_polynom(%.2lf) = %.4lf", node, Lagrange(node, left, right));
- printf("\nApproximation(%.2lf) = %.4lf", node, f_apr(coefficients, node));
- printf("\n\npoint:");
- printf("\nFunction(%.2lf) = %.4lf", point, f(point));
- printf("\nLagrange_polynom(%.2lf) = %.4lf", point, Lagrange(point, left, right));
- printf("\nApproximation(%.2lf) = %.4lf", point, f_apr(coefficients, point));
- printf("\n\nx_apr1:");
- left = 0;
- right = 13;
- printf("\nFunction(%.2lf) = %.4lf", z1, f(z1));
- printf("\nLagrange_polynom(%.2lf) = %.4lf", z1, Lagrange(z1, left, right));
- printf("\nApproximation(%.2lf) = %.4lf", z1, f_apr(coefficients, z1));
- printf("\n\nx_apr2:");
- printf("\nFunction(%.2lf) = %.4lf", z2, f(z2));
- printf("\nLagrange_polynom(%.2lf) = %.4lf", z2, Lagrange(z2, left, right));
- printf("\nApproximation(%.2lf) = %.4lf", z2, f_apr(coefficients, z2));
- return 0;
- }
Add Comment
Please, Sign In to add comment