Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <cmath>
- #include <string>
- #include <fstream>
- using namespace std;
- const double np = 150;
- struct wezly
- {
- public:
- wezly()
- {};
- vector <double> wezel;
- vector <double> wartosc;
- };
- vector<double>punkty;
- void wprowadz(double &a, double &b, double &n)
- {
- cout << "Poczatek przedzialu: ";
- cin >> a;
- cout << "Koniec przedzialu: ";
- cin >> b;
- cout << "ilosc wezlow: ";
- cin >> n;
- }
- wezly tablicuj(double a, double b, double n)
- {
- wezly x;
- double luka = abs((b - a) / n);
- for (double i = a; i <= b; i += luka)
- {
- x.wezel.push_back(i);
- }
- for (double i = 0; i <= n; i++)
- {
- x.wartosc.push_back(abs(cos(x.wezel[i])*x.wezel[i]));
- }
- return x;
- }
- void wypunktuj(double a, double b)
- {
- double luka = abs((b - a) / np);
- for (double i = a; i <= b; i += luka)
- {
- punkty.push_back(i);
- }
- }
- vector<double> wielomian(wezly x, double punkt, double n)
- {
- vector<double> wielomiany;
- double wielomian = 1.0;
- for (int i = 0; i <= n; i++)
- {
- wielomiany.push_back(wielomian);
- wielomian *= punkt - x.wezel[i];
- }
- return wielomiany;
- }
- vector<vector<double>> IR_Newtona(wezly x, int n)
- {
- vector<vector<double>> choinka;
- for (int i = 0; i <= n; i++)
- {
- vector<double>wnetrze;
- for (int j = 0; j <= n; j++)
- wnetrze.push_back(0);
- choinka.push_back(wnetrze);
- }
- for (int i = 0; i <= n; i++)
- {
- choinka[0][i] = x.wartosc[i];
- }
- int counter = 1;
- for (int i = 1; i <= n; i++)
- {
- for (int j = 0; j <= n-i; j++)
- {
- choinka[i][j] = (choinka[i - 1][j + 1] - choinka[i - 1][j]) / (x.wezel[j + counter] - x.wezel[j]);
- }
- counter++;
- }
- return choinka;
- }
- double Wzor_newtona(wezly x, double punkt, int n)
- {
- vector<vector<double>> choinka;
- vector<double>wielomiany;
- wielomiany = wielomian(x, punkt, n);
- choinka = IR_Newtona(x, n);
- double wartosc = 0.0;
- for (int i = 0; i <= n; i++)
- {
- wartosc += wielomiany[i] * choinka[i][0];
- }
- return wartosc;
- }
- void wypisz(wezly x, double n)
- {
- for (int i = 0; i < np; i++)
- {
- cout << punkty[i] << endl;
- cout << abs(cos(punkty[i])*punkty[i]) << endl;
- cout << Wzor_newtona(x, punkty[i], n) << endl;
- }
- }
- int main()
- {
- double a, b, n;
- wezly x;
- wprowadz(a, b, n);
- x = tablicuj(a, b, n);
- wypunktuj(a, b);
- wypisz(x, n);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement