Facebook
From Toxic Parakeet, 6 Years ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 219
  1. #include<iostream>
  2. #include<stdio.h>
  3. #include<math.h>
  4. #include <stdlib.h>
  5. #include<iomanip>
  6. #include<fstream>
  7. #include <stdlib.h>
  8. #define MAX_ITERATION 10000
  9. #define EPS 1e-16
  10.  
  11. using namespace std;
  12.  
  13. long double f1(long double x) {
  14.     //return sin(x/4)*sin(x/4) - x;
  15.     //return x*x*x +/4*x - 20;
  16.      //return exp(5)*x*x*x +20;
  17. }
  18.  
  19. long double f2(long double x) {
  20.     return tan(2*x)-x-1;
  21.     //return (x-1)*x*x +8*x;
  22. }
  23.  
  24. long double isConcurrent(long double(*f)(long double), long double x) {
  25.     if (((f(x + EPS) - f(x)) / EPS ) > 1)
  26.         return false;
  27.     else
  28.         return true;
  29. }
  30.  
  31. long double derivative(long double(*f)(long double), long double x) {
  32.     return (f(x + EPS) - f(x)) / EPS;
  33. }
  34.  
  35. long double pickard_f1(long double x) {
  36.     return sin(x/4)*sin(x/4);
  37. }
  38.  
  39. long double pickard_f2(long double x) {
  40.     return atan(x+1)/2;
  41.  
  42. }
  43.  
  44. string show_function1() {
  45.     return "sin^2(x/4)- x = 0";
  46. }
  47.  
  48. string show_function2() {
  49.     return "tan(2x)- x - 1 = 0";
  50. }
  51.  
  52. void writeTitleToFile(ofstream &file) {
  53.         file << setw(5) << "Lp." << setw(20) << right << "Przyblizenie Xn" << setw(20) << right <<
  54.         "Estymator " << setw(20) << right << "Residuum" << endl << endl;
  55. }
  56.  
  57. void writeResultToFile(ofstream &file, int i, double result, double estimator, double residuum) {
  58.         file.width(5);
  59.         file << i <<".";
  60.         file.width(20);
  61.         file << right << result;
  62.         file.width(20);
  63.         file << right << estimator;
  64.         file.width(20);
  65.         file << right << residuum << endl;
  66. }
  67.  
  68. double Pickard_function(long double (*f)(long double), long double (*pickard_f)(long double), long double x0, ofstream &file) {
  69.     long double estimator = 0;
  70.     long double residuum = 0;
  71.     long double x;
  72.     writeTitleToFile(file);
  73.         for(int i = 1; i< MAX_ITERATION; i++) {
  74.             x = pickard_f(x0);
  75.             residuum = fabs(f(x0));
  76.             estimator = fabs(x - x0);
  77.             x0 = x;
  78.  
  79.             writeResultToFile(file, i, x, estimator, residuum);
  80.  
  81.             if(residuum < EPS) {
  82.                 break;
  83.             }
  84.             if(estimator < EPS) {
  85.                 break;
  86.             }
  87.         }
  88.     return x;
  89. }
  90.  
  91. double Bisekcja_function(long double (*f)(long double), long double a, long double b, ofstream &file) {
  92.     long double estimator = 0;
  93.     long double residuum = 0;
  94.     long double x;
  95.     writeTitleToFile(file);
  96.     for(int i = 1; i< MAX_ITERATION; i++) {
  97.         x = (a+b)/2;
  98.         estimator = fabs((b-a)/2);
  99.         residuum = fabs(f(x));
  100.  
  101.         writeResultToFile(file, i, x, estimator, residuum);
  102.  
  103.         if(residuum < EPS) {
  104.             break;
  105.         }
  106.         if(estimator < EPS) {
  107.             break;
  108.         }
  109.  
  110.         if(f(a)*f(x)<0)
  111.             b = x;
  112.         else
  113.             a = x;
  114.  
  115.     }
  116.     return x;
  117. }
  118.  
  119. double Newton_function(long double (*f)(long double), long double x0, ofstream &file){
  120.     long double estimator = 0;
  121.     long double residuum = 0;
  122.     long double x;
  123.     writeTitleToFile(file);
  124.  
  125.     for(int i = 1; i<MAX_ITERATION; i++) {
  126.         x = x0 - f(x0)/derivative(f,x0);
  127.         estimator = fabs(x0 - x);
  128.         residuum = fabs(f(x));
  129.         x0 = x;
  130.  
  131.         writeResultToFile(file, i, x, estimator, residuum);
  132.  
  133.         if(residuum < EPS) {
  134.             break;
  135.         }
  136.         if(estimator < EPS) {
  137.             break;
  138.         }
  139.     }
  140.     return x;
  141. }
  142.  
  143. double Sieczna_function(long double (*f)(long double), long double a, long double b, ofstream &file) {
  144.     long double estimator = 0;
  145.     long double residuum = 0;
  146.     long double x;
  147.     writeTitleToFile(file);
  148.  
  149.     for(int i = 1; i<MAX_ITERATION; i++) {
  150.         double tmp = b;
  151.         b = b - f(b)*(b-a)/(f(b) - f(a));
  152.         a = tmp ;
  153.         estimator = fabs(a-b);
  154.         residuum = fabs(f(b));
  155.  
  156.         writeResultToFile(file, i, x, estimator, residuum);
  157.  
  158.         if(residuum < EPS) {
  159.             break;
  160.         }
  161.         if(estimator < EPS) {
  162.             break;
  163.         }
  164.     }
  165.     return a;
  166.     }
  167.  
  168.  
  169. int main()
  170. {
  171.     cout << setprecision(10);
  172.     cout << setw(20) <<"========== " << show_function1() << " =========="  << endl << endl;
  173.     ofstream Pickard1;
  174.     Pickard1.open("pickard1.txt", ios::out);
  175.     double arg_p1 = -8;
  176.     if(isConcurrent(pickard_f1, arg_p1)) {
  177.         cout << setw(20) << "Pickard " << setw(5) << "x0 = " << Pickard_function(f1, pickard_f1, arg_p1, Pickard1) << endl;
  178.     } else {
  179.          cout << "Nie można policzyć Pickarda. Funkcja rozbiezna!" << endl;
  180.     }
  181.  
  182.     ofstream Bisekcja1;
  183.     Bisekcja1.open("bisekcja1.txt", ios::out);
  184.     double arg_b1 = -2;
  185.     double arg_b2 = 4;
  186.  
  187.     if(f1(arg_b1)*f1(arg_b2)> 0 || arg_b1> arg_b2) {
  188.         cout << "Nie można policzyć bisekcji. Podano zly przedzial!" << endl;
  189.     } else {
  190.         cout << setw(20) << "Bisekcja " << setw(5) << "x0 = " << Bisekcja_function(f1, arg_b1, arg_b2, Bisekcja1)<< endl;
  191.     }
  192.  
  193.     ofstream Newton1;
  194.     double arg_n1 = 8;
  195.     Newton1.open("newton1.txt", ios::out);
  196.     cout << setw(20) <<  "Newton " <<  setw(5) << "x0 = " << Newton_function(f1, arg_n1, Newton1) << endl;
  197.  
  198.     ofstream Sieczna1;
  199.     double arg_s1 = -4;
  200.     double arg_s2 = -2;
  201.     Sieczna1.open("sieczna1.txt", ios::out);
  202.     cout << setw(20) << "Sieczna " <<  setw(5) << "x0 = " <<Sieczna_function(f1, arg_s1, arg_s2, Sieczna1) << endl;
  203.  
  204.     /****************************************************************************************************************/
  205.     /****************************************************************************************************************/
  206.     cout << endl;
  207.     cout << setw(20) <<"========== " << show_function2() << " =========="  << endl << endl;
  208.     ofstream Pickard2;
  209.     Pickard2.open("pickard2.txt", ios::out);
  210.     double arg_p2 = -8;
  211.     if(isConcurrent(pickard_f2, arg_p2)) {
  212.         cout << setw(20) << "Pickard " << setw(5) << "x0 = " << Pickard_function(f2, pickard_f2, arg_p2, Pickard2) << endl;
  213.     } else {
  214.          cout << "Nie można policzyć Pickarda. Funkcja rozbiezna!" << endl;
  215.     }
  216.  
  217.     ofstream Bisekcja2;
  218.     Bisekcja2.open("bisekcja2.txt", ios::out);
  219.     double arg_b21 = 0;
  220.     double arg_b22 = 0.6;
  221.  
  222.     if(f2(arg_b21)*f2(arg_b22)> 0 || arg_b21> arg_b22) {
  223.         cout << "Nie można policzyć bisekcji. Podano zly przedzial!" << endl;
  224.     } else {
  225.         cout << setw(20) << "Bisekcja " << setw(5) << "x0 = " << Bisekcja_function(f2, arg_b21, arg_b22, Bisekcja2)<< endl;
  226.     }
  227.  
  228.     ofstream Newton2;
  229.     double arg_n2 = 8;
  230.     Newton2.open("newton2.txt", ios::out);
  231.     cout << setw(20) <<  "Newton " <<  setw(5) << "x0 = " << Newton_function(f2, arg_n2, Newton2) << endl;
  232.  
  233.     ofstream Sieczna2;
  234.     double arg_s21 = -5;
  235.     double arg_s22 = 18;
  236.     Sieczna2.open("sieczna2.txt", ios::out);
  237.     cout << setw(20) << "Sieczna " <<  setw(5) << "x0 = " <<Sieczna_function(f2, arg_s21, arg_s22, Sieczna2) << endl;
  238.  
  239.     return 0;
  240. }