• API
• FAQ
• Tools
• Archive
SHARE
TWEET

# Untitled

a guest May 22nd, 2019 72 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. #include "mainwindow.h"
2. #include "ui_mainwindow.h"
3.
4. MainWindow::MainWindow(QWidget *parent) :
5.     QMainWindow(parent),
6.     ui(new Ui::MainWindow)
7. {
8.     ui->setupUi(this);
9. }
10.
11. MainWindow::~MainWindow()
12. {
13.     delete ui;
14. }
15.
16. int Factorial(int n)
17. {
18.  int result = n;
19.  for (int i = 1; i < n; i++)
20.  {
21.   result *= i;
22.  }
23.  return result;
24. }
25.
26. double Lagrange(const double *x, const double *y, const double x0)
27. {
28.  double result = 0;
29.  for(int i=0;i<10;i++)
30.  {
31.   double denum = 1, num = 1, temp=0;
32.   for(int j=0;j<10;j++)
33.   {
34.    if(i!=j)
35.    {
36.     num *= x0 - x[j];
37.     denum *= x[i] - x[j];
38.    }
39.   }
40.   result += num / denum * y[i];
41.  }
42.  return result;
43. }
44. //--------------------------------------------------------------------------------
45. double Newton(const double *x, const double *y, const double x0)
46. {
47.  double h = x[1] - x[0];
48.  double result = y[0];
49.  double dDeltaYArray[10][10] = { 0 };
50.  for (int i = 0; i < 10; i++)
51.  {
52.   dDeltaYArray[i][0] = y[i];
53.  }
54.
55.  for (int i = 1; i < 10; i++)
56.  {
57.   for (int j = 0; j < 10 - i; j++)
58.   {
59.    dDeltaYArray[j][i] = dDeltaYArray[j + 1][i - 1] - dDeltaYArray[j][i - 1];
60.   }
61.  }
62.
63.  for (int i = 1; i < 10; i++)
64.  {
65.   double num = dDeltaYArray[0][i], denum = Factorial(i), term = 0;
66.   for (int j = 0; j < i; j++)
67.   {
68.    num *= x0 - x[j];
69.    denum *= h;
70.   }
71.   term = num / denum;
72.   result += term;
73.  }
74.  return result;
75. }
76. //--------------------------------------------------------------------------------
77.
78. void MainWindow::on_pushButton_clicked()   // lagrange
79. {
80.     const double *arrayX = new double[10]{0.15, 0.20, 0.25, 0.30, 0.35, 0.40, 0.45, 0.50, 0.55,0.60};
81.     const double *arrayY = new double[10] {0.86, 0.819, 0.779, 0.741, 0.705, 0.670, 0.638, 0.606, 0.577, 0.549};
82.     const double x1 = 0.3119, x2=0.5031;
83.
84.    double result = Lagrange(arrayX,arrayY,x1);
85.     ui->textBrowser->append("Lagrange Polynom \n Interpolation = " + QString::number(result));
86. }
87.
88. void MainWindow::on_pushButton_2_clicked() //newtons
89. {
90.     const double *arrayX = new double[10]{0.15, 0.20, 0.25, 0.30, 0.35, 0.40, 0.45, 0.50, 0.55,0.60};
91.     const double *arrayY = new double[10] {0.86, 0.819, 0.779, 0.741, 0.705, 0.670, 0.638, 0.606, 0.577, 0.549};
92.  const double x1 = 0.3119, x2=0.5031;
93.  double result = Newton(arrayX,arrayY,x2);
94.
95.  ui->textBrowser->append("Newton Polynom \n Interpolation = " + QString::number(result));
96.
97. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy.

Top