Advertisement
Esudesu

Lekcja 2(working)

Jan 20th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.41 KB | None | 0 0
  1. // ConsoleApplication3.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include <iostream>
  6. #include <math.h>
  7.  
  8. using namespace std;
  9.  
  10. double lagran(double *xa, double *ya, double poz);
  11.  
  12. const double FIRST = 0.3;
  13. const double SECOND = 1.5;
  14. const double THIRD = 2.8;
  15. const int ARR_SIZE = 6;
  16.  
  17. int main()
  18. {
  19.     double y[] = { 0.100167, 0.636654, 1.50946, 2.94217, 6.69473, 10.0179 };
  20.     double x[] = { 0.1, 0.6, 1.2, 1.8, 2.6, 3 };
  21.     cout << "Wynik dla " << FIRST << " to: " << lagran(x, y, FIRST) << endl;
  22.     cout << "Wynik dla " << SECOND << " to: " << lagran(x, y, SECOND) << endl;
  23.     cout << "Wynik dla " << THIRD << " to: " << lagran(x, y, THIRD) << endl;
  24.  
  25.     cout << "Wynik dla sinh " << FIRST << " to: " << sinh(FIRST) << " roznica: "  << abs(lagran(x, y, FIRST) - sinh(FIRST)) << endl;
  26.     cout << "Wynik dla sinh " << SECOND << " to: " << sinh(SECOND) << " roznica: " << abs(lagran(x, y, SECOND) - sinh(SECOND)) << endl;
  27.     cout << "Wynik dla sinh " << THIRD << " to: " << sinh(THIRD) << " roznica: " << abs(lagran(x, y, THIRD) - sinh(THIRD)) << endl;
  28.     system("PAUSE");
  29.     return 0;
  30. }
  31.  
  32. double lagran(double *xa, double *ya, double poz) {
  33.     double wynik = 0.0;
  34.     double tmp;
  35.  
  36.     for (int i(0); i < ARR_SIZE; i++) {
  37.         tmp = 1.0;
  38.         for (int j(0); j < ARR_SIZE; j++) {
  39.             if (j != i) {
  40.                 tmp = tmp*((poz - xa[j]) / (xa[i] - xa[j]));
  41.             }
  42.         }
  43.         wynik += tmp*ya[i];
  44.     }
  45.  
  46.     return wynik;
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement