Advertisement
Kocyk

newton

Nov 8th, 2019
295
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.89 KB | None | 0 0
  1. #include <iostream>
  2. #include <stdio.h>
  3. #include <sstream>
  4. #include <iomanip>
  5. using namespace std;
  6. void wczytaj_dane(float *xi, float *fi, int n)
  7. {
  8.     cout << "Podaj wezly w ciaglu rosnacym" << endl;
  9.     cout << "podaj wezel nr" << 0 <<endl;
  10.     cin >> xi[0];
  11.     for(int i = 1; i <= n-1; i++)
  12.     {
  13.         cout << "podaj wezel nr" << i <<endl;
  14.         cin >> xi[i];
  15.         while (xi[i-1] >= xi[i])
  16.         {
  17.             cout << "Wezel jest mniejszy lub rowny poprzedniego" <<endl;
  18.             cin >> xi[i];
  19.         }
  20.     }
  21.     cout << "Podaj fi" << endl;
  22.     for(int i = 0; i <= n-1; i++)
  23.     {
  24.         cout << "podaj f1 nr" << i <<endl;
  25.         cin >> fi[i];
  26.     }
  27. }
  28. float wielomian_newtona(float *xi, float *fi, int n)
  29. {
  30.     float *l = new float[n];
  31.     float xp;
  32.     int od=1;
  33.     int zm=0;
  34.     int k=n-1;
  35.     float wynik = 0;
  36.     cout << "Podaj punkt interpolacji" <<endl;
  37.     cin >> xp;
  38.     while(xp < xi[0] || xp > xi[n])
  39.     {
  40.         cout << "Punkt nie zawiera sie w przedziale interpolacji" << endl << endl << "Wprowadz nowe xp" <<endl;
  41.         cin >> xp;
  42.     }
  43.     for(int j=0;j<n-1;j++)
  44.     {
  45.         for(int i=k;i>zm;i--)
  46.         {
  47.         fi[i]= (fi[i] - fi[i-od]) /(xi[i] - xi[i-od]);
  48.  
  49.         }
  50.         od+=1;
  51.         k-=1;
  52.         zm+=1;
  53.         for(int z=0;z<n;z++)cout<<setw(8)<< xi[z] <<setw(8) << fi[z] << setw(8)<<endl;
  54.         cout <<endl<<endl;
  55.     }
  56. }
  57. int main()
  58. {
  59.     int n;
  60.  
  61.     cout << "Podaj stopien wielomianu" << endl;
  62.     cin >> n;
  63.     float *xi = new float[n];
  64.     float *fi = new float[n];
  65.     wczytaj_dane(xi, fi, n);
  66.  
  67.     cout<<endl<<"Wprowadzone dane: "<<endl;
  68.     for(int i=0;i<n;i++)
  69.     {
  70.  
  71.         cout<<setw(8)<< xi[i] <<setw(8) << fi[i] << setw(8)<<endl;
  72.  
  73.     }
  74.     wielomian_newtona(xi, fi, n);
  75.     cout <<endl;
  76.     for(int i=0;i<n;i++)cout<<setw(8)<< xi[i] <<setw(8) << fi[i] << setw(8)<<endl;
  77.     return 0;
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement